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 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'application/api/controllers') 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); + } } -- cgit v1.2.3