diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-02-19 16:48:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-19 16:48:59 +0100 |
commit | b9eb50c09947ec35838051e1bcfe5bc09a839f8e (patch) | |
tree | 514ddc5f97d12d5d0582f081cbdfc8a318aa9d17 /application | |
parent | 65e56cbe49a7eb2a0cb09dda85daab3b921472ee (diff) | |
parent | 16e3d006e9e9386001881053f610657525feb188 (diff) | |
download | Shaarli-b9eb50c09947ec35838051e1bcfe5bc09a839f8e.tar.gz Shaarli-b9eb50c09947ec35838051e1bcfe5bc09a839f8e.tar.zst Shaarli-b9eb50c09947ec35838051e1bcfe5bc09a839f8e.zip |
Merge pull request #728 from ArthurHoaro/api/getLink
REST API: implements getLink by ID service
Diffstat (limited to 'application')
-rw-r--r-- | application/api/controllers/Links.php | 25 | ||||
-rw-r--r-- | application/api/exceptions/ApiLinkNotFoundException.php | 32 |
2 files changed, 55 insertions, 2 deletions
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; | |||
4 | 4 | ||
5 | use Shaarli\Api\ApiUtils; | 5 | use Shaarli\Api\ApiUtils; |
6 | use Shaarli\Api\Exceptions\ApiBadParametersException; | 6 | use Shaarli\Api\Exceptions\ApiBadParametersException; |
7 | use Shaarli\Api\Exceptions\ApiLinkNotFoundException; | ||
7 | use Slim\Http\Request; | 8 | use Slim\Http\Request; |
8 | use Slim\Http\Response; | 9 | use Slim\Http\Response; |
9 | 10 | ||
@@ -58,8 +59,7 @@ class Links extends ApiController | |||
58 | $limit = $request->getParam('limit'); | 59 | $limit = $request->getParam('limit'); |
59 | if (empty($limit)) { | 60 | if (empty($limit)) { |
60 | $limit = self::$DEFAULT_LIMIT; | 61 | $limit = self::$DEFAULT_LIMIT; |
61 | } | 62 | } else if (ctype_digit($limit)) { |
62 | else if (ctype_digit($limit)) { | ||
63 | $limit = intval($limit); | 63 | $limit = intval($limit); |
64 | } else if ($limit === 'all') { | 64 | } else if ($limit === 'all') { |
65 | $limit = count($links); | 65 | $limit = count($links); |
@@ -83,4 +83,25 @@ class Links extends ApiController | |||
83 | 83 | ||
84 | return $response->withJson($out, 200, $this->jsonStyle); | 84 | return $response->withJson($out, 200, $this->jsonStyle); |
85 | } | 85 | } |
86 | |||
87 | /** | ||
88 | * Return a single formatted link by its ID. | ||
89 | * | ||
90 | * @param Request $request Slim request. | ||
91 | * @param Response $response Slim response. | ||
92 | * @param array $args Path parameters. including the ID. | ||
93 | * | ||
94 | * @return Response containing the link array. | ||
95 | * | ||
96 | * @throws ApiLinkNotFoundException generating a 404 error. | ||
97 | */ | ||
98 | public function getLink($request, $response, $args) | ||
99 | { | ||
100 | if (! isset($this->linkDb[$args['id']])) { | ||
101 | throw new ApiLinkNotFoundException(); | ||
102 | } | ||
103 | $index = index_url($this->ci['environment']); | ||
104 | $out = ApiUtils::formatLink($this->linkDb[$args['id']], $index); | ||
105 | return $response->withJson($out, 200, $this->jsonStyle); | ||
106 | } | ||
86 | } | 107 | } |
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 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Api\Exceptions; | ||
4 | |||
5 | |||
6 | use Slim\Http\Response; | ||
7 | |||
8 | /** | ||
9 | * Class ApiLinkNotFoundException | ||
10 | * | ||
11 | * Link selected by ID couldn't be found, results in a 404 error. | ||
12 | * | ||
13 | * @package Shaarli\Api\Exceptions | ||
14 | */ | ||
15 | class ApiLinkNotFoundException extends ApiException | ||
16 | { | ||
17 | /** | ||
18 | * ApiLinkNotFoundException constructor. | ||
19 | */ | ||
20 | public function __construct() | ||
21 | { | ||
22 | $this->message = 'Link not found'; | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * {@inheritdoc} | ||
27 | */ | ||
28 | public function getApiResponse() | ||
29 | { | ||
30 | return $this->buildApiResponse(404); | ||
31 | } | ||
32 | } | ||