1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This software may be modified and distributed under the terms |
||
7 | * of the MIT license. See the LICENSE file for details. |
||
8 | */ |
||
9 | |||
10 | namespace FAPI\Localise\Api; |
||
11 | |||
12 | use FAPI\Localise\Exception\Domain as DomainExceptions; |
||
13 | use FAPI\Localise\Exception\DomainException; |
||
14 | use FAPI\Localise\Hydrator\NoopHydrator; |
||
15 | use Http\Client\HttpClient; |
||
16 | use FAPI\Localise\Hydrator\Hydrator; |
||
17 | use FAPI\Localise\RequestBuilder; |
||
18 | use Psr\Http\Message\ResponseInterface; |
||
19 | |||
20 | /** |
||
21 | * @author Tobias Nyholm <[email protected]> |
||
22 | */ |
||
23 | abstract class HttpApi |
||
24 | { |
||
25 | /** |
||
26 | * @var HttpClient |
||
27 | */ |
||
28 | protected $httpClient; |
||
29 | |||
30 | /** |
||
31 | * @var Hydrator |
||
32 | */ |
||
33 | protected $hydrator; |
||
34 | |||
35 | /** |
||
36 | * @var RequestBuilder |
||
37 | */ |
||
38 | protected $requestBuilder; |
||
39 | |||
40 | /** |
||
41 | * @param HttpClient $httpClient |
||
42 | * @param RequestBuilder $requestBuilder |
||
43 | * @param Hydrator $hydrator |
||
44 | */ |
||
45 | public function __construct(HttpClient $httpClient, Hydrator $hydrator, RequestBuilder $requestBuilder) |
||
46 | { |
||
47 | $this->httpClient = $httpClient; |
||
48 | $this->requestBuilder = $requestBuilder; |
||
49 | if (!$hydrator instanceof NoopHydrator) { |
||
50 | $this->hydrator = $hydrator; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Send a GET request with query parameters. |
||
56 | * |
||
57 | * @param string $path Request path |
||
58 | * @param array $params GET parameters |
||
59 | * @param array $requestHeaders Request Headers |
||
60 | * |
||
61 | * @return ResponseInterface |
||
62 | */ |
||
63 | protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
||
64 | { |
||
65 | if (count($params) > 0) { |
||
66 | $path .= '?'.http_build_query($params); |
||
67 | } |
||
68 | |||
69 | return $this->httpClient->sendRequest( |
||
70 | $this->requestBuilder->create('GET', $path, $requestHeaders) |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Send a POST request with JSON-encoded parameters. |
||
76 | * |
||
77 | * @param string $path Request path |
||
78 | * @param array $params POST parameters to be JSON encoded |
||
79 | * @param array $requestHeaders Request headers |
||
80 | * |
||
81 | * @return ResponseInterface |
||
82 | */ |
||
83 | protected function httpPost(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
||
84 | { |
||
85 | $requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; |
||
86 | |||
87 | return $this->httpPostRaw($path, http_build_query($params), $requestHeaders); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Send a POST request with raw data. |
||
92 | * |
||
93 | * @param string $path Request path |
||
94 | * @param array|string $body Request body |
||
95 | * @param array $requestHeaders Request headers |
||
96 | * |
||
97 | * @return ResponseInterface |
||
98 | */ |
||
99 | protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface |
||
100 | { |
||
101 | return $response = $this->httpClient->sendRequest( |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
102 | $this->requestBuilder->create('POST', $path, $requestHeaders, $body) |
||
103 | ); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Send a PUT request with JSON-encoded parameters. |
||
108 | * |
||
109 | * @param string $path Request path |
||
110 | * @param array $params POST parameters to be JSON encoded |
||
111 | * @param array $requestHeaders Request headers |
||
112 | * |
||
113 | * @return ResponseInterface |
||
114 | */ |
||
115 | protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
||
116 | { |
||
117 | $requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; |
||
118 | |||
119 | return $this->httpClient->sendRequest( |
||
120 | $this->requestBuilder->create('PUT', $path, $requestHeaders, http_build_query($params)) |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Send a PATCH request with JSON-encoded parameters. |
||
126 | * |
||
127 | * @param string $path Request path |
||
128 | * @param array $params PATCH parameters to be JSON encoded |
||
129 | * @param array $requestHeaders Request headers |
||
130 | * |
||
131 | * @return ResponseInterface |
||
132 | */ |
||
133 | protected function httpPatch(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
||
134 | { |
||
135 | $requestHeaders['Content-Type'] = 'application/json'; |
||
136 | |||
137 | return $this->httpClient->sendRequest( |
||
138 | $this->requestBuilder->create('PATCH', $path, $requestHeaders, json_encode($params)) |
||
139 | ); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Send a DELETE request with JSON-encoded parameters. |
||
144 | * |
||
145 | * @param string $path Request path |
||
146 | * @param array $params POST parameters to be JSON encoded |
||
147 | * @param array $requestHeaders Request headers |
||
148 | * |
||
149 | * @return ResponseInterface |
||
150 | */ |
||
151 | protected function httpDelete(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
||
152 | { |
||
153 | $requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; |
||
154 | |||
155 | return $this->httpClient->sendRequest( |
||
156 | $this->requestBuilder->create('DELETE', $path, $requestHeaders, http_build_query($params)) |
||
157 | ); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Handle HTTP errors. |
||
162 | * |
||
163 | * Call is controlled by the specific API methods. |
||
164 | * |
||
165 | * @param ResponseInterface $response |
||
166 | * |
||
167 | * @throws DomainException |
||
168 | */ |
||
169 | protected function handleErrors(ResponseInterface $response) |
||
170 | { |
||
171 | $body = $response->getBody()->__toString(); |
||
172 | |||
173 | $content = json_decode($body, true); |
||
174 | $message = ''; |
||
175 | if (JSON_ERROR_NONE === json_last_error()) { |
||
176 | $message = $content['error']; |
||
177 | } |
||
178 | |||
179 | switch ($response->getStatusCode()) { |
||
180 | case 401: |
||
181 | throw new DomainExceptions\InvalidApiKeyException($message); |
||
182 | case 403: |
||
183 | throw new DomainExceptions\InsufficientPrivilegesException($message); |
||
184 | case 404: |
||
185 | throw new DomainExceptions\NotFoundException($message); |
||
186 | default: |
||
187 | throw new DomainExceptions\UnknownErrorException($message); |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 |