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