Skip to content

Commit

Permalink
ocs controllers response types fixes (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey18106 authored Jul 24, 2023
1 parent 3ff978e commit 8c24c40
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 125 deletions.
22 changes: 9 additions & 13 deletions lib/Controller/AppConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
Expand All @@ -66,22 +65,21 @@ public function __construct(
*
* @param string $configKey
* @param mixed $configValue
* @param string $format
*
* @throws OCSBadRequestException
* @return Response
* @return DataResponse
*/
#[AppEcosystemAuth]
#[PublicPage]
#[NoCSRFRequired]
public function setAppConfigValue(string $configKey, mixed $configValue, string $format = 'json'): Response {
public function setAppConfigValue(string $configKey, mixed $configValue): DataResponse {
if ($configKey === '') {
throw new OCSBadRequestException('Config key cannot be empty');
}
$appId = $this->request->getHeader('EX-APP-ID');
$result = $this->exAppConfigService->setAppConfigValue($appId, $configKey, $configValue);
if ($result instanceof ExAppConfig) {
return $this->buildResponse(new DataResponse(1, Http::STATUS_OK), $format);
return new DataResponse(1, Http::STATUS_OK);
}
throw new OCSBadRequestException('Error setting app config value');
}
Expand All @@ -91,34 +89,32 @@ public function setAppConfigValue(string $configKey, mixed $configValue, string
* @NoCSRFRequired
*
* @param array $configKeys
* @param string $format
*
* @return Response
* @return DataResponse
*/
#[AppEcosystemAuth]
#[PublicPage]
#[NoCSRFRequired]
public function getAppConfigValues(array $configKeys, string $format = 'json'): Response {
public function getAppConfigValues(array $configKeys): DataResponse {
$appId = $this->request->getHeader('EX-APP-ID');
$result = $this->exAppConfigService->getAppConfigValues($appId, $configKeys);
return $this->buildResponse(new DataResponse($result, !empty($result) ? Http::STATUS_OK : Http::STATUS_NOT_FOUND), $format);
return new DataResponse($result, Http::STATUS_OK);
}

/**
* @PublicPage
* @NoCSRFRequired
*
* @param array $configKeys
* @param string $format
*
* @throws OCSBadRequestException
* @throws OCSNotFoundException
* @return Response
* @return DataResponse
*/
#[AppEcosystemAuth]
#[PublicPage]
#[NoCSRFRequired]
public function deleteAppConfigValues(array $configKeys, string $format = 'json'): Response {
public function deleteAppConfigValues(array $configKeys): DataResponse {
$appId = $this->request->getHeader('EX-APP-ID');
$result = $this->exAppConfigService->deleteAppConfigValues($configKeys, $appId);
if ($result === -1) {
Expand All @@ -127,6 +123,6 @@ public function deleteAppConfigValues(array $configKeys, string $format = 'json'
if ($result === 0) {
throw new OCSNotFoundException('No appconfig_ex values deleted');
}
return $this->buildResponse(new DataResponse($result, Http::STATUS_OK), $format);
return new DataResponse($result, Http::STATUS_OK);
}
}
8 changes: 3 additions & 5 deletions lib/Controller/ExAppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\OCSController;
use OCP\IRequest;

Expand All @@ -57,12 +56,11 @@ public function __construct(
* @NoCSRFRequired
*
* @param bool $extended
* @param string $format
*
* @return Response
* @return DataResponse
*/
#[NoCSRFRequired]
public function getExApps(bool $extended = false, string $format = 'json'): Response {
return $this->buildResponse(new DataResponse($this->service->getExAppsList($extended), Http::STATUS_OK), $format);
public function getExApps(bool $extended = false): DataResponse {
return new DataResponse($this->service->getExAppsList($extended), Http::STATUS_OK);
}
}
112 changes: 18 additions & 94 deletions lib/Controller/OCSApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
Expand Down Expand Up @@ -87,20 +86,15 @@ public function __construct(
*
* @param int $level
* @param string $message
* @param string $format
*
* @throws OCSBadRequestException
* @return Response
* @return DataResponse
*/
#[AppEcosystemAuth]
#[PublicPage]
#[NoAdminRequired]
#[NoCSRFRequired]
public function log(
int $level,
string $message,
string $format = 'json',
): Response {
public function log(int $level, string $message): DataResponse {
try {
$appId = $this->request->getHeader('EX-APP-ID');
$exApp = $this->service->getExApp($appId);
Expand All @@ -116,97 +110,31 @@ public function log(
$this->logger->log($level, $message, [
'app' => $appId,
]);
return $this->buildResponse(new DataResponse(1, Http::STATUS_OK), $format);
return new DataResponse();
} catch (\Psr\Log\InvalidArgumentException) {
$this->logger->error('Invalid log level');
throw new OCSBadRequestException('Invalid log level');
}
}

/**
* @NoCSRFRequired
*
* @param string $appId
* @param array $appData
* @param string $format
*
* @return Response
*/
#[NoCSRFRequired]
public function registerExternalApp(string $appId, array $appData, string $format = 'json'): Response {
// TODO: Sync logic with OCC command
$result = $this->service->registerExApp($appId, $appData);
return $this->buildResponse(new DataResponse([
'success' => $result !== null,
'registeredExApp' => $result,
], Http::STATUS_OK), $format);
}

/**
* @NoCSRFRequired
*
* @param string $appId
* @param string $format
*
* @return Response
*/
#[NoCSRFRequired]
public function unregisterExternalApp(string $appId, string $format = 'json'): Response {
// TODO: Sync logic with OCC command
$deletedExApp = $this->service->unregisterExApp($appId);
if ($deletedExApp === null) {
return $this->buildResponse(new DataResponse([
'success' => false,
'error' => $this->l->t('ExApp not found'),
], Http::STATUS_NOT_FOUND), $format);
}
return $this->buildResponse(new DataResponse([
'success' => $deletedExApp->getAppid() === $appId,
'deletedExApp' => $deletedExApp,
], Http::STATUS_OK), $format);
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $appId
* @param string $format
*
* @return Response
*/
#[NoAdminRequired]
#[NoCSRFRequired]
public function getAppStatus(string $appId, string $format = 'json'): Response {
$appStatus = $this->service->getAppStatus($appId);
return $this->buildResponse(new DataResponse([
'success' => $appStatus !== null,
'appStatus' => [
'appId' => $appId,
'status' => $appStatus,
],
], Http::STATUS_OK), $format);
}

/**
* @PublicPage
* @NoCSRFRequired
*
* @param array $fileActionMenuParams [name, display_name, mime, permissions, order, icon, icon_class, action_handler]
* @param string $format
*
* @return Response
* @return DataResponse
*/
#[AppEcosystemAuth]
#[PublicPage]
#[NoCSRFRequired]
public function registerFileActionMenu(array $fileActionMenuParams, string $format = 'json'): Response {
public function registerFileActionMenu(array $fileActionMenuParams): DataResponse {
$appId = $this->request->getHeader('EX-APP-ID');
$registeredFileActionMenu = $this->exFilesActionsMenuService->registerFileActionMenu($appId, $fileActionMenuParams);
return $this->buildResponse(new DataResponse([
return new DataResponse([
'success' => $registeredFileActionMenu !== null,
'registeredFileActionMenu' => $registeredFileActionMenu,
], Http::STATUS_OK), $format);
], Http::STATUS_OK);
}

/**
Expand Down Expand Up @@ -238,18 +166,17 @@ public function unregisterFileActionMenu(string $fileActionMenuName): DataRespon
* @param string $actionName
* @param array $actionFile
* @param string $actionHandler
* @param string $format
*
* @return Response
* @return DataResponse
*/
#[NoAdminRequired]
#[NoCSRFRequired]
public function handleFileAction(string $appId, string $actionName, array $actionFile, string $actionHandler, string $format = 'json'): Response {
public function handleFileAction(string $appId, string $actionName, array $actionFile, string $actionHandler): DataResponse {
$result = $this->exFilesActionsMenuService->handleFileAction($this->userId, $appId, $actionName, $actionHandler, $actionFile);
return $this->buildResponse(new DataResponse([
return new DataResponse([
'success' => $result,
'handleFileActionSent' => $result,
], Http::STATUS_OK), $format);
], Http::STATUS_OK);
}

/**
Expand All @@ -271,7 +198,7 @@ public function loadFileActionIcon(string $appId, string $exFileActionName): Dat
Http::STATUS_OK,
['Content-Type' => $icon['headers']['Content-Type'][0] ?? 'image/svg+xml']
);
$response->cacheFor(Application::ICON_CACHE_TTL, false, true);
$response->cacheFor(ExFilesActionsMenuService::ICON_CACHE_TTL, false, true);
return $response;
}
return new DataDisplayResponse('', 400);
Expand All @@ -281,15 +208,13 @@ public function loadFileActionIcon(string $appId, string $exFileActionName): Dat
* @PublicPage
* @NoCSRFRequired
*
* @param string $format
*
* @return Response
* @return DataResponse
*/
#[AppEcosystemAuth]
#[PublicPage]
#[NoCSRFRequired]
public function getExAppUsers(string $format = 'json'): Response {
return $this->buildResponse(new DataResponse($this->service->getNCUsersList(), Http::STATUS_OK), $format);
public function getExAppUsers(): DataResponse {
return new DataResponse($this->service->getNCUsersList(), Http::STATUS_OK);
}

/**
Expand All @@ -299,19 +224,18 @@ public function getExAppUsers(string $format = 'json'): Response {
* @param string $apiRoute
* @param int $scopeGroup
* @param string $name
* @param string $format
*
* @throws OCSBadRequestException
* @return Response
* @return DataResponse
*/
#[AppEcosystemAuth]
#[PublicPage]
#[NoCSRFRequired]
public function registerApiScope(string $apiRoute, int $scopeGroup, string $name, string $format): Response {
public function registerApiScope(string $apiRoute, int $scopeGroup, string $name): DataResponse {
$apiScope = $this->exAppApiScopeService->registerApiScope($apiRoute, $scopeGroup, $name);
if ($apiScope === null) {
throw new OCSBadRequestException('Failed to register API scope');
}
return $this->buildResponse(new DataResponse(1, Http::STATUS_OK), $format);
return new DataResponse(1, Http::STATUS_OK);
}
}
23 changes: 10 additions & 13 deletions lib/Controller/PreferencesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
Expand Down Expand Up @@ -70,23 +69,22 @@ public function __construct(
*
* @param string $configKey
* @param mixed $configValue
* @param string $format
*
* @throws OCSBadRequestException
* @return Response
* @return DataResponse
*/
#[AppEcosystemAuth]
#[PublicPage]
#[NoCSRFRequired]
public function setUserConfigValue(string $configKey, mixed $configValue, string $format = 'json'): Response {
public function setUserConfigValue(string $configKey, mixed $configValue): DataResponse {
if ($configKey === '') {
throw new OCSBadRequestException('Config key cannot be empty');
}
$userId = $this->userSession->getUser()->getUID();
$appId = $this->request->getHeader('EX-APP-ID');
$result = $this->exAppPreferenceService->setUserConfigValue($userId, $appId, $configKey, $configValue);
if ($result instanceof ExAppPreference) {
return $this->buildResponse(new DataResponse(1, Http::STATUS_OK), $format);
return new DataResponse(1, Http::STATUS_OK);
}
throw new OCSBadRequestException('Failed to set user config value');
}
Expand All @@ -96,34 +94,33 @@ public function setUserConfigValue(string $configKey, mixed $configValue, string
* @NoCSRFRequired
*
* @param array $configKeys
* @param string $format
*
* @return Response
* @return DataResponse
*/
#[AppEcosystemAuth]
#[PublicPage]
#[NoCSRFRequired]
public function getUserConfigValues(array $configKeys, string $format = 'json'): Response {
public function getUserConfigValues(array $configKeys): DataResponse {
$userId = $this->userSession->getUser()->getUID();
$appId = $this->request->getHeader('EX-APP-ID');
$result = $this->exAppPreferenceService->getUserConfigValues($userId, $appId, $configKeys);
return $this->buildResponse(new DataResponse($result, !empty($result) ? Http::STATUS_OK : Http::STATUS_NOT_FOUND), $format);
return new DataResponse($result, Http::STATUS_OK);
}

/**
* @PublicPage
* @NoCSRFRequired
*
* @param array $configKeys
* @param string $format
*
* @throws OCSBadRequestException
* @return Response
* @throws OCSNotFoundException
* @return DataResponse
*/
#[AppEcosystemAuth]
#[PublicPage]
#[NoCSRFRequired]
public function deleteUserConfigValues(array $configKeys, string $format = 'json'): Response {
public function deleteUserConfigValues(array $configKeys): DataResponse {
$userId = $this->userSession->getUser()->getUID();
$appId = $this->request->getHeader('EX-APP-ID');
$result = $this->exAppPreferenceService->deleteUserConfigValues($configKeys, $userId, $appId);
Expand All @@ -133,6 +130,6 @@ public function deleteUserConfigValues(array $configKeys, string $format = 'json
if ($result === 0) {
throw new OCSNotFoundException('No preferences_ex values deleted');
}
return $this->buildResponse(new DataResponse($result, Http::STATUS_OK), $format);
return new DataResponse($result, Http::STATUS_OK);
}
}
Loading

0 comments on commit 8c24c40

Please sign in to comment.