From 0d7d3cdfb4af9e5f369e229a0eb05d506f801da2 Mon Sep 17 00:00:00 2001 From: Paul Tirk Date: Sat, 9 Jan 2021 19:05:40 +0100 Subject: move v2 api responses into existing php traits Signed-off-by: Paul Tirk --- lib/Controller/ApiPayloadTrait.php | 37 ++++++++++++++++- lib/Controller/ApiV2ResponseTrait.php | 69 -------------------------------- lib/Controller/FolderApiV2Controller.php | 25 ++++++------ lib/Controller/JSONHttpErrorTrait.php | 15 +++++++ 4 files changed, 64 insertions(+), 82 deletions(-) delete mode 100644 lib/Controller/ApiV2ResponseTrait.php (limited to 'lib/Controller') diff --git a/lib/Controller/ApiPayloadTrait.php b/lib/Controller/ApiPayloadTrait.php index 2bb31784e..c40ec9c25 100644 --- a/lib/Controller/ApiPayloadTrait.php +++ b/lib/Controller/ApiPayloadTrait.php @@ -1,8 +1,10 @@ toAPI2($reduced); + } + + /** + * Serialize array of entities + * + * @param array $data + * + * @return array + */ + public function serializeEntitiesV2($data, bool $reduced = false): array + { + $return = []; + foreach ($data as $entity) { + $return[] = $entity->toAPI2($reduced); + } + return $return; + } + + public function responseV2($data, $code = Http::STATUS_OK) + { + return new JSONResponse($data, $code); + } } diff --git a/lib/Controller/ApiV2ResponseTrait.php b/lib/Controller/ApiV2ResponseTrait.php deleted file mode 100644 index 3d803b28f..000000000 --- a/lib/Controller/ApiV2ResponseTrait.php +++ /dev/null @@ -1,69 +0,0 @@ - - * @copyright 2020 Paul Tirk - */ - -namespace OCA\News\Controller; - -use \OCP\AppFramework\Http; -use \OCP\AppFramework\Http\JSONResponse; - -use \OCA\News\Db\IAPI; - -trait ApiV2ResponseTrait -{ - /** - * Serialize an entity - * - * @param IAPI $data - * - * @return array - */ - public function serializeEntity($data, bool $reduced = false): array - { - return $data->toAPI2($reduced); - } - - /** - * Serialize array of entities - * - * @param array $data - * - * @return array - */ - public function serializeEntities($data, bool $reduced = false): array - { - $return = []; - foreach ($data as $entity) { - $return[] = $entity->toAPI2($reduced); - } - return $return; - } - - public function response($data, $code = Http::STATUS_OK) - { - return new JSONResponse($data, $code); - } - - /** - * @param \Exception $exception - * @param int $code - * @return \OCP\AppFramework\Http\JSONResponse - */ - public function errorResponse(\Exception $exception, $code) - { - return new JSONResponse([ - 'error' => [ - 'code' => $exception->getCode(), - 'message' => $exception->getMessage() - ] - ], $code); - } -} diff --git a/lib/Controller/FolderApiV2Controller.php b/lib/Controller/FolderApiV2Controller.php index 683169006..b4ba8fcda 100644 --- a/lib/Controller/FolderApiV2Controller.php +++ b/lib/Controller/FolderApiV2Controller.php @@ -23,7 +23,8 @@ use \OCA\News\Service\Exceptions\ServiceValidationException; class FolderApiV2Controller extends ApiController { - use ApiV2ResponseTrait; + use ApiPayloadTrait; + use JSONHttpErrorTrait; private $folderService; private $itemService; @@ -52,16 +53,16 @@ class FolderApiV2Controller extends ApiController { try { $this->folderService->purgeDeleted($this->getUserId(), false); - $responseData = $this->serializeEntity( + $responseData = $this->serializeEntityV2( $this->folderService->create($this->getUserId(), $name) ); - return $this->response([ + return $this->responseV2([ 'folder' => $responseData ]); } catch (ServiceValidationException $ex) { - return $this->errorResponse($ex, Http::STATUS_BAD_REQUEST); + return $this->errorResponseV2($ex, Http::STATUS_BAD_REQUEST); } catch (ServiceConflictException $ex) { - return $this->errorResponse($ex, Http::STATUS_CONFLICT); + return $this->errorResponseV2($ex, Http::STATUS_CONFLICT); } } @@ -79,14 +80,14 @@ class FolderApiV2Controller extends ApiController try { $response = $this->folderService->rename($this->getUserId(), $folderId, $name); } catch (ServiceValidationException $ex) { - return $this->errorResponse($ex, Http::STATUS_UNPROCESSABLE_ENTITY); + return $this->errorResponseV2($ex, Http::STATUS_UNPROCESSABLE_ENTITY); } catch (ServiceConflictException $ex) { - return $this->errorResponse($ex, Http::STATUS_CONFLICT); + return $this->errorResponseV2($ex, Http::STATUS_CONFLICT); } catch (ServiceNotFoundException $ex) { - return $this->errorResponse($ex, Http::STATUS_NOT_FOUND); + return $this->errorResponseV2($ex, Http::STATUS_NOT_FOUND); } - return $this->response([ + return $this->responseV2([ 'folder' => $response ]); } @@ -103,14 +104,14 @@ class FolderApiV2Controller extends ApiController public function delete($folderId) { try { - $responseData = $this->serializeEntity( + $responseData = $this->serializeEntityV2( $this->folderService->delete($this->getUserId(), $folderId) ); - return $this->response([ + return $this->responseV2([ 'folder' => $responseData ]); } catch (ServiceNotFoundException $ex) { - return $this->errorResponse($ex, Http::STATUS_NOT_FOUND); + return $this->errorResponseV2($ex, Http::STATUS_NOT_FOUND); } } } diff --git a/lib/Controller/JSONHttpErrorTrait.php b/lib/Controller/JSONHttpErrorTrait.php index e3e343f16..5c5d7a8cf 100644 --- a/lib/Controller/JSONHttpErrorTrait.php +++ b/lib/Controller/JSONHttpErrorTrait.php @@ -24,4 +24,19 @@ trait JSONHttpErrorTrait { return new JSONResponse(['message' => $exception->getMessage()], $code); } + + /** + * @param \Exception $exception + * @param int $code + * @return \OCP\AppFramework\Http\JSONResponse + */ + public function errorResponseV2(\Exception $exception, $code) + { + return new JSONResponse([ + 'error' => [ + 'code' => $exception->getCode(), + 'message' => $exception->getMessage() + ] + ], $code); + } } -- cgit v1.2.3