1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mapado\RestClientSdk; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
use GuzzleHttp\Exception\TransferException; |
8
|
|
|
use Mapado\RestClientSdk\Exception\RestClientException; |
9
|
|
|
use Mapado\RestClientSdk\Exception\RestException; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class RestClient |
15
|
|
|
* |
16
|
|
|
* @author Julien Deniau <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class RestClient |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* httpClient |
22
|
|
|
* |
23
|
|
|
* @var ClientInterface |
24
|
|
|
*/ |
25
|
|
|
private $httpClient; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* baseUrl |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $baseUrl; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* logHistory |
36
|
|
|
* |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
private $logHistory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* requestHistory |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $requestHistory; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* currentRequest |
50
|
|
|
* |
51
|
|
|
* @var Request |
52
|
|
|
*/ |
53
|
|
|
private $currentRequest; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ClientInterface $httpClient |
57
|
|
|
* @param string|null $baseUrl |
58
|
|
|
*/ |
59
|
|
|
public function __construct(ClientInterface $httpClient, $baseUrl = null) |
60
|
|
|
{ |
61
|
1 |
|
$this->httpClient = $httpClient; |
62
|
1 |
|
$this->baseUrl = '/' === substr($baseUrl, -1) |
63
|
|
|
? substr($baseUrl, 0, -1) |
64
|
1 |
|
: $baseUrl; |
65
|
1 |
|
$this->logHistory = false; |
66
|
1 |
|
$this->requestHistory = []; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function isHistoryLogged() |
73
|
|
|
{ |
74
|
1 |
|
return $this->logHistory; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* setCurrentRequest |
79
|
|
|
* |
80
|
|
|
* @param Request $currentRequest |
81
|
|
|
* |
82
|
|
|
* @return RestClient |
83
|
|
|
*/ |
84
|
|
|
public function setCurrentRequest(Request $currentRequest) |
85
|
|
|
{ |
86
|
|
|
$this->currentRequest = $currentRequest; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* setLogHistory |
93
|
|
|
* |
94
|
|
|
* @param bool $logHistory |
95
|
|
|
* |
96
|
|
|
* @return RestClient |
97
|
|
|
*/ |
98
|
|
|
public function setLogHistory($logHistory) |
99
|
|
|
{ |
100
|
1 |
|
$this->logHistory = $logHistory; |
101
|
|
|
|
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function getRequestHistory() |
109
|
|
|
{ |
110
|
1 |
|
return $this->requestHistory; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* get a path |
115
|
|
|
* |
116
|
|
|
* @param string $path |
117
|
|
|
* @param array $parameters |
118
|
|
|
* |
119
|
|
|
* @return array|ResponseInterface|null |
120
|
|
|
* |
121
|
|
|
* @throws RestException |
122
|
|
|
*/ |
123
|
|
|
public function get($path, $parameters = []) |
124
|
|
|
{ |
125
|
1 |
|
$requestUrl = $this->baseUrl . $path; |
126
|
|
|
try { |
127
|
1 |
|
return $this->executeRequest('GET', $requestUrl, $parameters); |
128
|
1 |
|
} catch (ClientException $e) { |
129
|
1 |
|
if (404 === $e->getResponse()->getStatusCode()) { |
130
|
1 |
|
return null; |
131
|
|
|
} |
132
|
1 |
|
throw new RestClientException( |
133
|
1 |
|
'Error while getting resource', |
134
|
1 |
|
$path, |
135
|
1 |
|
[], |
136
|
1 |
|
7, |
137
|
1 |
|
$e |
138
|
|
|
); |
139
|
1 |
|
} catch (TransferException $e) { |
140
|
1 |
|
throw new RestException( |
141
|
1 |
|
'Error while getting resource', |
142
|
1 |
|
$path, |
143
|
1 |
|
[], |
144
|
1 |
|
1, |
145
|
1 |
|
$e |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* delete |
152
|
|
|
* |
153
|
|
|
* @param string $path |
154
|
|
|
* |
155
|
|
|
* @throws RestException |
156
|
|
|
*/ |
157
|
|
|
public function delete($path) |
158
|
|
|
{ |
159
|
|
|
try { |
160
|
1 |
|
$this->executeRequest('DELETE', $this->baseUrl . $path); |
161
|
1 |
|
} catch (ClientException $e) { |
162
|
1 |
|
return; |
163
|
1 |
|
} catch (TransferException $e) { |
164
|
1 |
|
throw new RestException( |
165
|
1 |
|
'Error while deleting resource', |
166
|
1 |
|
$path, |
167
|
1 |
|
[], |
168
|
1 |
|
2, |
169
|
1 |
|
$e |
170
|
|
|
); |
171
|
|
|
} |
172
|
1 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $path |
176
|
|
|
* @param mixed $data |
177
|
|
|
* @param array $parameters |
178
|
|
|
* |
179
|
|
|
* @return array|ResponseInterface |
180
|
|
|
* |
181
|
|
|
* @throws RestClientException |
182
|
|
|
* @throws RestException |
183
|
|
|
*/ |
184
|
|
|
public function post($path, $data, $parameters = []) |
185
|
|
|
{ |
186
|
1 |
|
$parameters['json'] = $data; |
187
|
|
|
try { |
188
|
1 |
|
return $this->executeRequest( |
189
|
1 |
|
'POST', |
190
|
1 |
|
$this->baseUrl . $path, |
191
|
1 |
|
$parameters |
192
|
|
|
); |
193
|
1 |
|
} catch (ClientException $e) { |
194
|
1 |
|
throw new RestClientException( |
195
|
1 |
|
'Cannot create resource', |
196
|
1 |
|
$path, |
197
|
1 |
|
[], |
198
|
1 |
|
3, |
199
|
1 |
|
$e |
200
|
|
|
); |
201
|
1 |
|
} catch (TransferException $e) { |
202
|
1 |
|
throw new RestException( |
203
|
1 |
|
'Error while posting resource', |
204
|
1 |
|
$path, |
205
|
1 |
|
[], |
206
|
1 |
|
4, |
207
|
1 |
|
$e |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string $path |
214
|
|
|
* @param mixed $data |
215
|
|
|
* @param array $parameters |
216
|
|
|
* |
217
|
|
|
* @return array|ResponseInterface |
218
|
|
|
* |
219
|
|
|
* @throws RestClientException |
220
|
|
|
* @throws RestException |
221
|
|
|
*/ |
222
|
|
|
public function put($path, $data, $parameters = []) |
223
|
|
|
{ |
224
|
1 |
|
$parameters['json'] = $data; |
225
|
|
|
|
226
|
|
|
try { |
227
|
1 |
|
return $this->executeRequest( |
228
|
1 |
|
'PUT', |
229
|
1 |
|
$this->baseUrl . $path, |
230
|
1 |
|
$parameters |
231
|
|
|
); |
232
|
1 |
|
} catch (ClientException $e) { |
233
|
1 |
|
throw new RestClientException( |
234
|
1 |
|
'Cannot update resource', |
235
|
1 |
|
$path, |
236
|
1 |
|
[], |
237
|
1 |
|
5, |
238
|
1 |
|
$e |
239
|
|
|
); |
240
|
1 |
|
} catch (TransferException $e) { |
241
|
1 |
|
throw new RestException( |
242
|
1 |
|
'Error while puting resource', |
243
|
1 |
|
$path, |
244
|
1 |
|
[], |
245
|
1 |
|
6, |
246
|
1 |
|
$e |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Merge default parameters. |
253
|
|
|
* |
254
|
|
|
* @param array $parameters |
255
|
|
|
* |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
|
|
protected function mergeDefaultParameters(array $parameters) |
259
|
|
|
{ |
260
|
1 |
|
$request = $this->getCurrentRequest(); |
261
|
|
|
|
262
|
1 |
|
$defaultParameters = ['version' => '1.0']; |
263
|
|
|
|
264
|
1 |
|
if ($request) { |
|
|
|
|
265
|
1 |
|
$defaultParameters['headers'] = ['Referer' => $request->getUri()]; |
266
|
|
|
} |
267
|
|
|
|
268
|
1 |
|
return array_replace_recursive($defaultParameters, $parameters); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* getCurrentRequest |
273
|
|
|
* |
274
|
|
|
* @return Request |
275
|
|
|
*/ |
276
|
|
|
protected function getCurrentRequest() |
277
|
|
|
{ |
278
|
1 |
|
if (!$this->currentRequest) { |
279
|
1 |
|
$this->currentRequest = Request::createFromGlobals(); |
280
|
|
|
} |
281
|
|
|
|
282
|
1 |
|
return $this->currentRequest; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Executes request. |
287
|
|
|
* |
288
|
|
|
* @param string $method |
289
|
|
|
* @param string $url |
290
|
|
|
* @param array $parameters |
291
|
|
|
* |
292
|
|
|
* @return ResponseInterface|array |
293
|
|
|
* |
294
|
|
|
* @throws TransferException |
295
|
|
|
*/ |
296
|
|
|
private function executeRequest($method, $url, $parameters = []) |
297
|
|
|
{ |
298
|
1 |
|
$parameters = $this->mergeDefaultParameters($parameters); |
299
|
|
|
|
300
|
1 |
|
$startTime = null; |
301
|
1 |
|
if ($this->isHistoryLogged()) { |
302
|
1 |
|
$startTime = microtime(true); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
try { |
306
|
1 |
|
$response = $this->httpClient->request($method, $url, $parameters); |
307
|
1 |
|
$this->logRequest( |
308
|
1 |
|
$startTime, |
309
|
1 |
|
$method, |
310
|
1 |
|
$url, |
311
|
1 |
|
$parameters, |
312
|
1 |
|
$response |
313
|
|
|
); |
314
|
1 |
|
} catch (TransferException $e) { |
315
|
1 |
|
$this->logRequest($startTime, $method, $url, $parameters); |
316
|
1 |
|
throw $e; |
317
|
|
|
} |
318
|
|
|
|
319
|
1 |
|
$headers = $response->getHeaders(); |
320
|
1 |
|
$jsonContentTypeList = ['application/ld+json', 'application/json']; |
321
|
|
|
|
322
|
1 |
|
$requestIsJson = false; |
323
|
|
|
|
324
|
1 |
|
if (isset($headers['Content-Type'])) { |
325
|
1 |
|
foreach ($jsonContentTypeList as $contentType) { |
326
|
|
|
if ( |
327
|
1 |
|
false !== stripos($headers['Content-Type'][0], $contentType) |
328
|
|
|
) { |
329
|
1 |
|
$requestIsJson = true; |
330
|
1 |
|
break; |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
335
|
1 |
|
if ($requestIsJson) { |
336
|
1 |
|
return json_decode($response->getBody(), true); |
337
|
|
|
} else { |
338
|
1 |
|
return $response; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Logs request. |
344
|
|
|
* |
345
|
|
|
* @param float|null $startTime |
346
|
|
|
* @param string $method |
347
|
|
|
* @param string $url |
348
|
|
|
* @param array $parameters |
349
|
|
|
* @param ResponseInterface|null $response |
350
|
|
|
*/ |
351
|
|
|
private function logRequest( |
352
|
|
|
$startTime, |
353
|
|
|
$method, |
354
|
|
|
$url, |
355
|
|
|
$parameters, |
356
|
|
|
ResponseInterface $response = null |
357
|
|
|
) { |
358
|
1 |
|
if ($this->isHistoryLogged()) { |
359
|
1 |
|
$queryTime = microtime(true) - $startTime; |
360
|
|
|
|
361
|
1 |
|
$this->requestHistory[] = [ |
362
|
1 |
|
'method' => $method, |
363
|
1 |
|
'url' => $url, |
364
|
1 |
|
'parameters' => $parameters, |
365
|
1 |
|
'response' => $response, |
366
|
1 |
|
'responseBody' => $response |
367
|
1 |
|
? json_decode($response->getBody(), true) |
368
|
|
|
: null, |
369
|
1 |
|
'queryTime' => $queryTime, |
370
|
1 |
|
'backtrace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), |
371
|
|
|
]; |
372
|
|
|
} |
373
|
1 |
|
} |
374
|
|
|
} |
375
|
|
|
|