From 16e3d006e9e9386001881053f610657525feb188 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 24 Dec 2016 10:30:21 +0100 Subject: REST API: implements getLink by ID service See http://shaarli.github.io/api-documentation/#links-link-get --- application/api/controllers/Links.php | 25 +++++++++++++++-- .../api/exceptions/ApiLinkNotFoundException.php | 32 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 application/api/exceptions/ApiLinkNotFoundException.php (limited to 'application') diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php index 0a7968e3..d4f1a09c 100644 --- a/application/api/controllers/Links.php +++ b/application/api/controllers/Links.php @@ -4,6 +4,7 @@ namespace Shaarli\Api\Controllers; use Shaarli\Api\ApiUtils; use Shaarli\Api\Exceptions\ApiBadParametersException; +use Shaarli\Api\Exceptions\ApiLinkNotFoundException; use Slim\Http\Request; use Slim\Http\Response; @@ -58,8 +59,7 @@ class Links extends ApiController $limit = $request->getParam('limit'); if (empty($limit)) { $limit = self::$DEFAULT_LIMIT; - } - else if (ctype_digit($limit)) { + } else if (ctype_digit($limit)) { $limit = intval($limit); } else if ($limit === 'all') { $limit = count($links); @@ -83,4 +83,25 @@ class Links extends ApiController return $response->withJson($out, 200, $this->jsonStyle); } + + /** + * Return a single formatted link by its ID. + * + * @param Request $request Slim request. + * @param Response $response Slim response. + * @param array $args Path parameters. including the ID. + * + * @return Response containing the link array. + * + * @throws ApiLinkNotFoundException generating a 404 error. + */ + public function getLink($request, $response, $args) + { + if (! isset($this->linkDb[$args['id']])) { + throw new ApiLinkNotFoundException(); + } + $index = index_url($this->ci['environment']); + $out = ApiUtils::formatLink($this->linkDb[$args['id']], $index); + return $response->withJson($out, 200, $this->jsonStyle); + } } diff --git a/application/api/exceptions/ApiLinkNotFoundException.php b/application/api/exceptions/ApiLinkNotFoundException.php new file mode 100644 index 00000000..de7e14f5 --- /dev/null +++ b/application/api/exceptions/ApiLinkNotFoundException.php @@ -0,0 +1,32 @@ +message = 'Link not found'; + } + + /** + * {@inheritdoc} + */ + public function getApiResponse() + { + return $this->buildApiResponse(404); + } +} -- cgit v1.2.3