Skip to content

Commit

Permalink
add internal methods for requests to ExApps by appid (#112)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Borysenko <andrey18106x@gmail.com>
Co-authored-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
andrey18106 and bigcat88 authored Nov 8, 2023
1 parent 39bf43a commit 5495320
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 20 deletions.
100 changes: 81 additions & 19 deletions lib/Service/AppAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function enableExApp(ExApp $exApp): bool {
$exApp->setEnabled(1);
$this->cache->set($cacheKey, $exApp, self::CACHE_TTL);

$exAppEnabled = $this->requestToExApp(null, null, $exApp, '/enabled?enabled=1', 'PUT');
$exAppEnabled = $this->requestToExApp($exApp, '/enabled?enabled=1', null, 'PUT');
if ($exAppEnabled instanceof IResponse) {
$response = json_decode($exAppEnabled->getBody(), true);
if (isset($response['error']) && strlen($response['error']) === 0) {
Expand Down Expand Up @@ -249,7 +249,7 @@ public function enableExApp(ExApp $exApp): bool {
*/
public function disableExApp(ExApp $exApp): bool {
try {
$exAppDisabled = $this->requestToExApp(null, null, $exApp, '/enabled?enabled=0', 'PUT');
$exAppDisabled = $this->requestToExApp($exApp, '/enabled?enabled=0', null, 'PUT');
if ($exAppDisabled instanceof IResponse) {
$response = json_decode($exAppDisabled->getBody(), true);
if (isset($response['error']) && strlen($response['error']) !== 0) {
Expand Down Expand Up @@ -416,8 +416,6 @@ public function dispatchExAppInit(ExApp $exApp): void {
) . '/init';

$options = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'headers' => $this->buildAppAPIAuthHeaders(null, null, $exApp),
'nextcloud' => [
'allow_local_address' => true,
Expand Down Expand Up @@ -505,51 +503,113 @@ public function getLatestExAppInfoFromAppstore(string $appId): ?\SimpleXMLElemen
/**
* Request to ExApp with AppAPI auth headers and ExApp user initialization
*
* @param IRequest|null $request
* @param string $userId
* @param ExApp $exApp
* @param string $route
* @param string $userId
* @param string $method
* @param array $params
* @param array $options
* @param IRequest|null $request
*
* @return array|IResponse
*/
public function aeRequestToExApp(
?IRequest $request,
string $userId,
ExApp $exApp,
string $route,
string $userId,
string $method = 'POST',
array $params = []
array $params = [],
array $options = [],
?IRequest $request = null,
): array|IResponse {
try {
$this->exAppUsersService->setupExAppUser($exApp, $userId);
} catch (\Exception $e) {
$this->logger->error(sprintf('Error while inserting ExApp %s user. Error: %s', $exApp->getAppid(), $e->getMessage()), ['exception' => $e]);
return ['error' => 'Error while inserting ExApp user: ' . $e->getMessage()];
}
return $this->requestToExApp($request, $userId, $exApp, $route, $method, $params);
return $this->requestToExApp($exApp, $userId, $route, $method, $params, $options, $request);
}

/**
* Request to ExApp with AppAPI auth headers
* Request to ExApp by appId with AppAPI auth headers and ExApp user initialization
*
* @param string $appId
* @param string $route
* @param string $userId
* @param string $method
* @param array $params
* @param array $options
* @param IRequest|null $request
*
* @return array|IResponse
*/
public function aeRequestToExAppById(
string $appId,
string $route,
string $userId,
string $method = 'POST',
array $params = [],
array $options = [],
?IRequest $request = null,
): array|IResponse {
$exApp = $this->getExApp($appId);
if ($exApp === null) {
return ['error' => 'ExApp not found'];
}
return $this->aeRequestToExApp($exApp, $route, $userId, $method, $params, $options, $request);
}

/**
* Request to ExApp by appId with AppAPI auth headers
*
* @param string $appId
* @param string $route
* @param string|null $userId
* @param string $method
* @param array $params
* @param array $options
* @param IRequest|null $request
*
* @return array|IResponse
*/
public function requestToExAppById(
string $appId,
string $route,
?string $userId = null,
string $method = 'POST',
array $params = [],
array $options = [],
?IRequest $request = null,
): array|IResponse {
$exApp = $this->getExApp($appId);
if ($exApp === null) {
return ['error' => 'ExApp not found'];
}
return $this->requestToExApp($exApp, $route, $userId, $method, $params, $options, $request);
}

/**
* Request to ExApp with AppAPI auth headers
*
* @param ExApp $exApp
* @param string $route
* @param string|null $userId
* @param string $method
* @param array $params
* @param array $options
* @param IRequest|null $request
*
* @return array|IResponse
*/
public function requestToExApp(
?IRequest $request,
?string $userId,
ExApp $exApp,
string $route,
?string $userId = null,
string $method = 'POST',
array $params = []
array $params = [],
array $options = [],
?IRequest $request = null,
): array|IResponse {
$this->handleExAppDebug($exApp, $request, true);
try {
Expand All @@ -558,11 +618,13 @@ public function requestToExApp(
$exApp->getHost(),
$exApp->getPort()) . $route;

$options = [
'headers' => $this->buildAppAPIAuthHeaders($request, $userId, $exApp),
'nextcloud' => [
'allow_local_address' => true, // it's required as we are using ExApp appid as hostname (usually local)
],
if (is_array($options['headers'])) {
$options['headers'] = [...$options['headers'], ...$this->buildAppAPIAuthHeaders($request, $userId, $exApp)];
} else {
$options['headers'] = $this->buildAppAPIAuthHeaders($request, $userId, $exApp);
}
$options['nextcloud'] = [
'allow_local_address' => true, // it's required as we are using ExApp appid as hostname (usually local)
];

if (count($params) > 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/ExFilesActionsMenuService.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function handleFileAction(string $userId, string $appId, string $fileActi
];
$exApp = $this->appAPIService->getExApp($appId);
if ($exApp !== null) {
$result = $this->appAPIService->aeRequestToExApp($this->request, $userId, $exApp, $handler, 'POST', $params);
$result = $this->appAPIService->aeRequestToExApp($exApp, $handler, $userId, 'POST', $params, [], $this->request);
if ($result instanceof IResponse) {
return $result->getStatusCode() === 200;
}
Expand Down

0 comments on commit 5495320

Please sign in to comment.