|
1
|
|
|
<?php |
|
2
|
|
|
namespace Akkroo; |
|
3
|
|
|
|
|
4
|
|
|
use Http\Client\HttpClient; |
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use Psr\Log\NullLogger; |
|
7
|
|
|
use Http\Message\RequestFactory; |
|
8
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
9
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
|
10
|
|
|
use Http\Message\UriFactory; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
|
|
14
|
|
|
class Client |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Public API scope |
|
18
|
|
|
*/ |
|
19
|
|
|
const SCOPE_PUBLIC = 'public'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Widget scope |
|
23
|
|
|
*/ |
|
24
|
|
|
const SCOPE_WIDGET = 'widget'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var HttpClient |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $httpClient; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $options = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $defaults = [ |
|
40
|
|
|
'endpoint' => 'https://api.integrate-events.com/v2.1', |
|
41
|
|
|
'auth_endpoint' => 'https://integrate-unified-dev.auth0.com/oauth/token', |
|
42
|
|
|
'auth_audience' => 'https://api.integrate-events.com/', |
|
43
|
|
|
'version' => '2.0.0', |
|
44
|
|
|
'scope' => self::SCOPE_PUBLIC |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var LoggerInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $logger = null; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $clientId = ''; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $clientSecret = ''; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $authToken = ''; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var int |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $authTokenExpiration = 0; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Creates PSR-7 HTTP Requests |
|
74
|
|
|
* |
|
75
|
|
|
* @var RequestFactory |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $requestFactory = null; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Creates PSR-7 URIS |
|
81
|
|
|
* |
|
82
|
|
|
* @var UriFactory |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $uriFactory = null; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Create a new Akkroo client |
|
88
|
|
|
* |
|
89
|
|
|
* Currently supported options are 'version' and 'endpoint'. |
|
90
|
|
|
* |
|
91
|
|
|
* @param HttpClient $httpClient Client to do HTTP requests |
|
92
|
|
|
* @param string $clientId Your Akkroo API username (i.e. company username) |
|
93
|
|
|
* @param string $clientSecret Your Akkroo API key |
|
94
|
|
|
* @param array $options |
|
95
|
|
|
*/ |
|
96
|
29 |
|
public function __construct(HttpClient $httpClient, string $clientId, string $clientSecret, array $options = []) |
|
97
|
|
|
{ |
|
98
|
29 |
|
$this->httpClient = $httpClient; |
|
99
|
29 |
|
$this->options = array_merge($this->defaults, $options); |
|
100
|
29 |
|
$this->clientId = $clientId; |
|
101
|
29 |
|
$this->clientSecret = $clientSecret; |
|
102
|
29 |
|
$this->logger = new NullLogger; |
|
103
|
29 |
|
$this->requestFactory = MessageFactoryDiscovery::find(); |
|
104
|
29 |
|
$this->uriFactory = UriFactoryDiscovery::find(); |
|
105
|
29 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Inject a logger object |
|
109
|
|
|
* |
|
110
|
|
|
* @param LoggerInterface $logger A PSR-3 compatible logger |
|
111
|
|
|
* @return Client |
|
112
|
|
|
*/ |
|
113
|
29 |
|
public function setLogger(LoggerInterface $logger) |
|
114
|
|
|
{ |
|
115
|
29 |
|
$this->logger = $logger; |
|
116
|
29 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Authenticate with Akkroo API and get a token |
|
121
|
|
|
* |
|
122
|
|
|
* Note: current API version returns HTTP 400 when wrong credentials are supplied |
|
123
|
|
|
* |
|
124
|
|
|
* @param string|null $username |
|
125
|
|
|
* |
|
126
|
|
|
* @return Client |
|
127
|
|
|
* |
|
128
|
|
|
* @throws Error\Authentication |
|
129
|
|
|
* @throws Error\Generic |
|
130
|
|
|
* @throws Error\NotFound |
|
131
|
|
|
*/ |
|
132
|
3 |
|
public function login(string $username = null) |
|
133
|
|
|
{ |
|
134
|
|
|
$headers = [ |
|
135
|
3 |
|
'Content-Type' => 'application/json' |
|
136
|
|
|
]; |
|
137
|
|
|
|
|
138
|
|
|
$body = [ |
|
139
|
3 |
|
'grant_type' => 'client_credentials', |
|
140
|
3 |
|
'client_id' => $this->clientId, |
|
141
|
3 |
|
'client_secret' => $this->clientSecret, |
|
142
|
3 |
|
'audience' => $this->options['auth_audience'], |
|
143
|
|
|
]; |
|
144
|
|
|
|
|
145
|
3 |
|
if ($username !== null) { |
|
146
|
|
|
$body['company'] = $username; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
3 |
|
$request = $this->requestFactory->createRequest('POST', $this->options['auth_endpoint'], $headers, json_encode($body)); |
|
150
|
3 |
|
$response = $this->httpClient->sendRequest($request); |
|
151
|
3 |
|
$result = $this->parseResponse($response); |
|
152
|
2 |
|
$login = new Result($result['data']); |
|
153
|
|
|
|
|
154
|
2 |
|
if ($login->access_token) { |
|
155
|
1 |
|
$this->authToken = $login->access_token; |
|
156
|
1 |
|
$this->authTokenExpiration = time() + (int) $login->expires_in; |
|
157
|
|
|
|
|
158
|
1 |
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
throw new Error\Generic('Unable to login, no access token returned'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns credentials from last authentication |
|
166
|
|
|
* |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
2 |
|
public function getCredentials() |
|
170
|
|
|
{ |
|
171
|
|
|
return [ |
|
172
|
2 |
|
'authToken' => $this->authToken, |
|
173
|
2 |
|
'authTokenExpiration' => $this->authTokenExpiration, |
|
174
|
|
|
]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Fetch one or more resources |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $resource Resource name (i.e. events, registrations) |
|
181
|
|
|
* @param array $params Search parameters (i.e. id, event_id, search query, range, fields, sort) |
|
182
|
|
|
* @param array $headers Additional headers |
|
183
|
|
|
* |
|
184
|
|
|
* @return Collection | Resource |
|
185
|
|
|
* |
|
186
|
|
|
* @throws Error\Authentication |
|
187
|
|
|
* @throws Error\NotFound |
|
188
|
|
|
* @throws Error\Generic |
|
189
|
|
|
*/ |
|
190
|
12 |
|
public function get($resource, array $params = [], array $headers = []) |
|
191
|
|
|
{ |
|
192
|
12 |
|
$path = $this->buildPath($resource, $params); |
|
193
|
11 |
|
$result = $this->request('GET', $path, $headers, $params); |
|
194
|
9 |
|
$resourceMeta = []; |
|
195
|
9 |
|
if (!empty($result['headers']['X-Total-Count'])) { |
|
196
|
1 |
|
$contentRange = $this->parseContentRange($result['headers']); |
|
197
|
1 |
|
$resourceMeta['contentRange'] = $contentRange; |
|
198
|
|
|
} |
|
199
|
9 |
|
return Resource::create($resource, $result['data'], $params, $resourceMeta) |
|
200
|
9 |
|
->withRequestID($result['requestID']); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Create a new resource |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $resource Resource name (i.e. events, registrations) |
|
207
|
|
|
* @param array $data Resource data |
|
208
|
|
|
* @param array $params Search parameters (i.e. id, event_id, search query, range, fields, sort) |
|
209
|
|
|
* @param array $headers Additional headers |
|
210
|
|
|
* |
|
211
|
|
|
* @return Resource |
|
212
|
|
|
* |
|
213
|
|
|
* @throws Error\Authentication |
|
214
|
|
|
* @throws Error\NotFound |
|
215
|
|
|
* @throws Error\Generic |
|
216
|
|
|
*/ |
|
217
|
2 |
|
public function post($resource, array $data, array $params = [], array $headers = []) |
|
218
|
|
|
{ |
|
219
|
2 |
|
$path = $this->buildPath($resource, $params); |
|
220
|
2 |
|
$result = $this->request('POST', $path, $headers, $params, $data); |
|
221
|
|
|
// Store temporary resource containing only ID |
|
222
|
1 |
|
$tmp = Resource::create($resource, $result['data'], $params)->withRequestID($result['requestID']); |
|
223
|
|
|
// Return minimal object if called by Webforms, avoiding errors |
|
224
|
1 |
|
if ($this->options['scope'] === self::SCOPE_WIDGET) { |
|
225
|
|
|
return $tmp; |
|
226
|
|
|
} |
|
227
|
|
|
// Fetch data for inserted resource: use same request ID, so the server could avoid |
|
228
|
|
|
// inserting a duplicate |
|
229
|
1 |
|
return $this->get($resource, array_merge($params, ['id' => $tmp->id]), ['Request-ID' => $tmp->requestID]); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Update a resource fully or partially |
|
234
|
|
|
* |
|
235
|
|
|
* If using PUT method, the $data parameter must be the full resource data |
|
236
|
|
|
* |
|
237
|
|
|
* @param string $method Must be PUT or PATCH |
|
238
|
|
|
* @param string $resource Resource name (i.e. events, registrations) |
|
239
|
|
|
* @param array $params URL parameters (i.e. id, event_id) |
|
240
|
|
|
* @param array $data Resource data |
|
241
|
|
|
* @param array $headers Additional headers |
|
242
|
|
|
* |
|
243
|
|
|
* @return Resource |
|
244
|
|
|
* |
|
245
|
|
|
* @throws Error\Authentication |
|
246
|
|
|
* @throws Error\NotFound |
|
247
|
|
|
* @throws Error\Generic |
|
248
|
|
|
*/ |
|
249
|
2 |
|
protected function update($method, $resource, array $params, array $data, array $headers = []) |
|
250
|
|
|
{ |
|
251
|
2 |
|
$path = $this->buildPath($resource, $params); |
|
252
|
|
|
// Take care of modified header, but let it overridable |
|
253
|
2 |
|
if (empty($headers['If-Unmodified-Since']) && !empty($data['lastModified'])) { |
|
254
|
1 |
|
$headers['If-Unmodified-Since'] = $data['lastModified']; |
|
255
|
|
|
} |
|
256
|
2 |
|
$result = $this->request($method, $path, $headers, $params, $data); |
|
257
|
|
|
// If we don't have an exception here it's all right, we can fetch the updated resource |
|
258
|
|
|
// using the original Request-ID |
|
259
|
2 |
|
$headers['Request-ID'] = $result['requestID']; |
|
260
|
2 |
|
return $this->get($resource, $params, $headers); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Update a resource |
|
265
|
|
|
* |
|
266
|
|
|
* The $data parameter must be the full resource data |
|
267
|
|
|
* |
|
268
|
|
|
* @param string $resource Resource name (i.e. events, registrations) |
|
269
|
|
|
* @param array $params URL parameters (i.e. id, event_id) |
|
270
|
|
|
* @param array $data Resource data |
|
271
|
|
|
* @param array $headers Additional headers |
|
272
|
|
|
* |
|
273
|
|
|
* @return Resource |
|
274
|
|
|
* |
|
275
|
|
|
* @throws Error\Authentication |
|
276
|
|
|
* @throws Error\NotFound |
|
277
|
|
|
* @throws Error\Generic |
|
278
|
|
|
*/ |
|
279
|
1 |
|
public function put($resource, array $params, array $data, array $headers = []) |
|
280
|
|
|
{ |
|
281
|
1 |
|
return $this->update('PUT', $resource, $params, $data, $headers); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Partially update a resource |
|
286
|
|
|
* |
|
287
|
|
|
* @param string $resource Resource name (i.e. events, registrations) |
|
288
|
|
|
* @param array $params URL parameters (i.e. id, event_id) |
|
289
|
|
|
* @param array $data Resource data |
|
290
|
|
|
* @param array $headers Additional headers |
|
291
|
|
|
* |
|
292
|
|
|
* @return Resource |
|
293
|
|
|
* @throws Error\Authentication |
|
294
|
|
|
* @throws Error\NotFound |
|
295
|
|
|
* @throws Error\Generic |
|
296
|
|
|
*/ |
|
297
|
1 |
|
public function patch($resource, array $params, array $data, array $headers = []) |
|
298
|
|
|
{ |
|
299
|
1 |
|
return $this->update('PATCH', $resource, $params, $data, $headers); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Delete a resource |
|
304
|
|
|
* |
|
305
|
|
|
* @param string $resource Resource name (i.e. events, registrations) |
|
306
|
|
|
* @param array $params URL parameters (i.e. id, event_id) |
|
307
|
|
|
* |
|
308
|
|
|
* @return Result |
|
309
|
|
|
* |
|
310
|
|
|
* @throws Error\Authentication |
|
311
|
|
|
* @throws Error\NotFound |
|
312
|
|
|
* @throws Error\Generic |
|
313
|
|
|
*/ |
|
314
|
3 |
|
public function delete($resource, array $params = []) |
|
315
|
|
|
{ |
|
316
|
3 |
|
$path = $this->buildPath($resource, $params); |
|
317
|
3 |
|
$result = $this->request('DELETE', $path); |
|
318
|
2 |
|
return (new Result(['success' => true]))->withRequestID($result['requestID']); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Count resources that satisfy the query |
|
323
|
|
|
* |
|
324
|
|
|
* @param string $resource Resource name (i.e. events, registrations) |
|
325
|
|
|
* @param array $params URL parameters (i.e. id, event_id) |
|
326
|
|
|
* |
|
327
|
|
|
* @return Result |
|
328
|
|
|
* |
|
329
|
|
|
* @throws Error\Authentication |
|
330
|
|
|
* @throws Error\NotFound |
|
331
|
|
|
* @throws Error\Generic |
|
332
|
|
|
*/ |
|
333
|
1 |
|
public function count($resource, $params = []) |
|
334
|
|
|
{ |
|
335
|
1 |
|
$path = $this->buildPath($resource, $params); |
|
336
|
1 |
|
$result = $this->request('HEAD', $path, [], $params); |
|
337
|
1 |
|
$contentRange = $this->parseContentRange($result['headers']); |
|
338
|
1 |
|
return (new Result(['count' => $contentRange['total']]))->withRequestID($result['requestID']); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Get allowed HTTP methods for a resource |
|
343
|
|
|
* |
|
344
|
|
|
* @param string $resource Resource name (i.e. events, registrations) |
|
345
|
|
|
* @param array $params URL parameters (i.e. id, event_id) |
|
346
|
|
|
* |
|
347
|
|
|
* @return Result |
|
348
|
|
|
* |
|
349
|
|
|
* @throws Error\Authentication |
|
350
|
|
|
* @throws Error\NotFound |
|
351
|
|
|
* @throws Error\Generic |
|
352
|
|
|
*/ |
|
353
|
2 |
|
public function options($resource, $params = []) |
|
354
|
|
|
{ |
|
355
|
2 |
|
$path = $this->buildPath($resource, $params); |
|
356
|
2 |
|
$result = $this->request('OPTIONS', $path); |
|
357
|
2 |
|
if (empty($result['headers']['Allow'])) { |
|
358
|
1 |
|
throw new Error\Generic('Missing allow header'); |
|
359
|
|
|
} |
|
360
|
1 |
|
$allow = array_map(function ($item) { |
|
361
|
1 |
|
return trim($item); |
|
362
|
1 |
|
}, explode(',', $result['headers']['Allow'][0])); |
|
363
|
1 |
|
return (new Result(['success' => true, 'allow' => $allow]))->withRequestID($result['requestID']); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
/** |
|
367
|
|
|
* Send a test API request |
|
368
|
|
|
* |
|
369
|
|
|
* @param array $headers Additional headers |
|
370
|
|
|
* |
|
371
|
|
|
* @throws Error\Generic |
|
372
|
|
|
*/ |
|
373
|
2 |
|
public function test($headers = []) |
|
374
|
|
|
{ |
|
375
|
2 |
|
$endpoint = $this->uriFactory->createUri($this->options['endpoint']); |
|
376
|
2 |
|
$uri = $endpoint->withPath('/health'); |
|
377
|
|
|
|
|
378
|
2 |
|
$request = $this->requestFactory->createRequest('GET', $uri, $headers); |
|
379
|
2 |
|
$response = $this->httpClient->sendRequest($request); |
|
380
|
|
|
|
|
381
|
2 |
|
$result = $this->parseResponse($response); |
|
382
|
|
|
|
|
383
|
2 |
|
return new Result($result['data']); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
/** |
|
387
|
|
|
* Send an /authTest API request |
|
388
|
|
|
* |
|
389
|
|
|
* If the token is not supplied. it will try with the current internal token. |
|
390
|
|
|
* |
|
391
|
|
|
* This method does not throw exceptions, it logs server errors with the |
|
392
|
|
|
* internal logger, if provided |
|
393
|
|
|
* |
|
394
|
|
|
* @param string $token An auth token to test for |
|
395
|
|
|
* |
|
396
|
|
|
* @return Result |
|
397
|
|
|
*/ |
|
398
|
5 |
|
public function authTest($token = null) |
|
399
|
|
|
{ |
|
400
|
|
|
$headers = [ |
|
401
|
5 |
|
'Content-Type' => 'application/json' |
|
402
|
|
|
]; |
|
403
|
5 |
|
if (!empty($token)) { |
|
404
|
3 |
|
$headers['Authorization'] = 'Bearer ' . $token; |
|
405
|
3 |
|
$this->authToken = $token; |
|
406
|
2 |
|
} elseif (!empty($this->authToken)) { |
|
407
|
1 |
|
$headers['Authorization'] = 'Bearer ' . $this->authToken; |
|
408
|
|
|
} |
|
409
|
|
|
try { |
|
410
|
5 |
|
$result = $this->request('GET', '/auth/test', $headers); |
|
411
|
2 |
|
return (new Result($result['data']))->withRequestID($result['requestID']); |
|
412
|
3 |
|
} catch (Error\Generic $e) { |
|
413
|
3 |
|
$this->logger->error( |
|
414
|
3 |
|
'Auth Test failed', |
|
415
|
3 |
|
['code' => $e->getCode(), 'message' => $e->getMessage(), 'requestID' => $e->getRequestID()] |
|
416
|
|
|
); |
|
417
|
3 |
|
return (new Result(['success' => false]))->withRequestID($e->getRequestID()); |
|
418
|
|
|
} |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* Process response status |
|
423
|
|
|
* |
|
424
|
|
|
* @param ResponseInterface $response |
|
425
|
|
|
* @param string $requestID Unique linked request |
|
426
|
|
|
* |
|
427
|
|
|
* @return array The parsed JSON body |
|
428
|
|
|
* |
|
429
|
|
|
* @throws Error\Authentication |
|
430
|
|
|
* @throws Error\NotFound |
|
431
|
|
|
* @throws Error\Generic |
|
432
|
|
|
*/ |
|
433
|
26 |
|
protected function parseResponse($response, $requestID = null) |
|
434
|
|
|
{ |
|
435
|
26 |
|
$status = $response->getStatusCode(); |
|
436
|
26 |
|
$reason = $response->getReasonPhrase(); |
|
437
|
|
|
$body = [ |
|
438
|
26 |
|
'data' => $this->parseResponseBody($response), |
|
439
|
26 |
|
'headers' => $response->getHeaders() |
|
440
|
|
|
]; |
|
441
|
26 |
|
if (!empty($requestID)) { |
|
442
|
22 |
|
$body['requestID'] = $requestID; |
|
443
|
|
|
} |
|
444
|
26 |
|
$this->logger->debug('Parsed response', ['status' => $status, 'reason' => $reason, 'body' => $body]); |
|
445
|
|
|
switch ($status) { |
|
446
|
26 |
|
case 401: |
|
447
|
24 |
|
case 403: |
|
448
|
2 |
|
throw new Error\Authentication($reason, $status, $body); |
|
449
|
|
|
break; |
|
450
|
|
|
|
|
451
|
24 |
|
case 404: |
|
452
|
2 |
|
throw new Error\NotFound('Resource Not Found', $status); |
|
453
|
|
|
break; |
|
454
|
|
|
|
|
455
|
|
|
default: |
|
456
|
|
|
// 3xx redirect status must be managed by the HTTP Client |
|
457
|
|
|
// Statuses other that what we define success are automatic errors |
|
458
|
22 |
|
if (!in_array($status, [200, 201, 202, 203, 204, 205, 206])) { |
|
459
|
3 |
|
if (isset($body['data']['error']['data'])) { |
|
460
|
1 |
|
throw new Error\Validation('Validation Error', $status, $body); |
|
461
|
|
|
} |
|
462
|
2 |
|
if ($body['data']['error'] === 'uniqueConflict') { |
|
463
|
|
|
throw new Error\UniqueConflict('Unique Conflict', $status, $body); |
|
464
|
|
|
} |
|
465
|
2 |
|
throw new Error\Generic($reason, $status, $body); |
|
466
|
|
|
} |
|
467
|
19 |
|
break; |
|
468
|
|
|
} |
|
469
|
19 |
|
return $body; |
|
470
|
|
|
} |
|
471
|
|
|
|
|
472
|
|
|
/** |
|
473
|
|
|
* Parse response body to JSON |
|
474
|
|
|
* |
|
475
|
|
|
* @param ResponseInterface $response |
|
476
|
|
|
* @return array |
|
477
|
|
|
*/ |
|
478
|
26 |
|
protected function parseResponseBody($response) |
|
479
|
|
|
{ |
|
480
|
26 |
|
$body = (string) $response->getBody(); |
|
481
|
26 |
|
return json_decode($body, true); |
|
482
|
|
|
} |
|
483
|
|
|
|
|
484
|
|
|
/** |
|
485
|
|
|
* Parse the Content-Range header to readable data |
|
486
|
|
|
* |
|
487
|
|
|
* @param array $headers |
|
488
|
|
|
* @return array |
|
489
|
|
|
*/ |
|
490
|
2 |
|
protected function parseContentRange(array $headers) |
|
491
|
|
|
{ |
|
492
|
2 |
|
$totalCount = (int) $headers['X-Total-Count'][0]; |
|
493
|
2 |
|
$contentRange = ['from' => 1, 'to' => $totalCount, 'total' => $totalCount]; |
|
494
|
2 |
|
if (!empty($headers['Link'])) { |
|
495
|
1 |
|
preg_match_all('/\<.+?>\;\srel\=\".+?\"/', $headers['Link'][0], $links); |
|
496
|
|
|
|
|
497
|
1 |
|
foreach ($links[0] as $link) { |
|
498
|
1 |
|
$link = explode(' ', $link); |
|
499
|
1 |
|
$matches = []; |
|
500
|
1 |
|
$linkRel = preg_match('/rel="(.*)"/', $link[1], $matches) ? $matches[1] : 'unknown'; |
|
501
|
1 |
|
$matches = []; |
|
502
|
1 |
|
$linkURI = preg_match('/<\/v2(.*)>/', $link[0], $matches) ? $matches[1] : 'unknown'; |
|
503
|
1 |
|
$linkURIParts = explode('?', $linkURI); |
|
504
|
1 |
|
$linkResource = trim($linkURIParts[0], '/'); |
|
505
|
1 |
|
parse_str($linkURIParts[1], $linkURIParams); |
|
506
|
1 |
|
$contentRange['links'][$linkRel] = [ |
|
507
|
1 |
|
'uri' => $linkURI, |
|
508
|
1 |
|
'resource' => $linkResource, |
|
509
|
1 |
|
'params' => $linkURIParams |
|
510
|
|
|
]; |
|
511
|
|
|
} |
|
512
|
1 |
|
$contentRange['page'] = $contentRange['links']['self']['params']['page']; |
|
513
|
1 |
|
$contentRange['pages'] = $contentRange['links']['last']['params']['page'] ?? 1; |
|
514
|
1 |
|
$contentRange['per_page'] = $contentRange['links']['self']['params']['per_page']; |
|
515
|
1 |
|
$contentRange['from'] = $contentRange['per_page'] * ($contentRange['page'] -1) + 1; |
|
516
|
1 |
|
$contentRange['to'] = $contentRange['per_page'] * $contentRange['page']; |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
2 |
|
return $contentRange; |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
|
|
/** |
|
523
|
|
|
* @param string $resource Main resource path |
|
524
|
|
|
* @param array $params URL and querystring parameters |
|
525
|
|
|
* @return string |
|
526
|
|
|
*/ |
|
527
|
19 |
|
protected function buildPath($resource, array $params = []) |
|
528
|
|
|
{ |
|
529
|
19 |
|
$path = '/' . $resource; |
|
530
|
19 |
|
switch ($resource) { |
|
531
|
19 |
|
case 'events': |
|
532
|
10 |
|
if (!empty($params['id'])) { |
|
533
|
8 |
|
$path .= '/' . $params['id']; |
|
534
|
|
|
} |
|
535
|
10 |
|
break; |
|
536
|
9 |
|
case 'records': |
|
537
|
8 |
|
if (empty($params['event_id'])) { |
|
538
|
1 |
|
throw new InvalidArgumentException('An event ID is required for records'); |
|
539
|
|
|
} |
|
540
|
7 |
|
$path = '/events/' . $params['event_id'] . '/' . $resource; |
|
541
|
7 |
|
if (!empty($params['id'])) { |
|
542
|
2 |
|
$path .= '/' . $params['id']; |
|
543
|
|
|
} |
|
544
|
7 |
|
break; |
|
545
|
|
|
} |
|
546
|
18 |
|
return $path; |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
|
|
/** |
|
550
|
|
|
* @param array $params URL and querystring parameters |
|
551
|
|
|
* @return string |
|
552
|
|
|
*/ |
|
553
|
23 |
|
protected function buildQuery(array $params) |
|
554
|
|
|
{ |
|
555
|
|
|
// Add querystring parameters |
|
556
|
23 |
|
$query = []; |
|
557
|
23 |
|
foreach ($params as $key => $value) { |
|
558
|
|
|
// Exclude URL and range values |
|
559
|
11 |
|
if (in_array($key, ['id', 'event_id', 'range'])) { |
|
560
|
10 |
|
continue; |
|
561
|
|
|
} |
|
562
|
2 |
|
if ($key === 'fields' && is_array($value)) { |
|
563
|
1 |
|
$query[$key] = implode(',', $value); |
|
564
|
1 |
|
continue; |
|
565
|
|
|
} |
|
566
|
2 |
|
if ($value === true) { |
|
567
|
1 |
|
$query[$key] = 'true'; |
|
568
|
1 |
|
continue; |
|
569
|
|
|
} |
|
570
|
2 |
|
if ($value === false) { |
|
571
|
1 |
|
$query[$key] = 'false'; |
|
572
|
1 |
|
continue; |
|
573
|
|
|
} |
|
574
|
2 |
|
$query[$key] = $value; |
|
575
|
|
|
} |
|
576
|
|
|
// Decode unreserved characters adding ',' to the list |
|
577
|
|
|
// see https://github.com/guzzle/psr7/blob/master/README.md#guzzlehttppsr7urinormalizernormalize |
|
578
|
23 |
|
return preg_replace_callback( |
|
579
|
23 |
|
'/%(?:2C|2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i', |
|
580
|
23 |
|
function (array $match) { |
|
581
|
1 |
|
return rawurldecode($match[0]); |
|
582
|
23 |
|
}, |
|
583
|
23 |
|
http_build_query($query) |
|
584
|
|
|
); |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
/** |
|
588
|
|
|
* Send a request to the API endpoint |
|
589
|
|
|
* |
|
590
|
|
|
* @param string $method HTTP method |
|
591
|
|
|
* @param string $path Relative URL path (without query string) |
|
592
|
|
|
* @param array $headers Additional headers |
|
593
|
|
|
* @param array $params Query string parameters |
|
594
|
|
|
* @param array $data Request body |
|
595
|
|
|
* |
|
596
|
|
|
* @return array JSON-decoded associative array from server response |
|
597
|
|
|
* |
|
598
|
|
|
* @throws Error\Authentication |
|
599
|
|
|
* @throws Error\NotFound |
|
600
|
|
|
* @throws Error\Generic |
|
601
|
|
|
*/ |
|
602
|
23 |
|
protected function request($method, $path, $headers = [], $params = [], $data = []) |
|
603
|
|
|
{ |
|
604
|
|
|
// Minimal default header |
|
605
|
23 |
|
$acceptContentType = 'application/json'; |
|
606
|
|
|
|
|
607
|
|
|
// Unique request ID |
|
608
|
23 |
|
$requestID = uniqid('', true); |
|
609
|
|
|
|
|
610
|
|
|
// Adding custom headers |
|
611
|
23 |
|
$requestHeaders = array_merge([ |
|
612
|
23 |
|
'Accept' => $acceptContentType, |
|
613
|
23 |
|
'X-Request-ID' => $requestID |
|
614
|
|
|
], $headers); |
|
615
|
|
|
|
|
616
|
|
|
// Add credentials |
|
617
|
23 |
|
if (!empty($this->authToken) && empty($requestHeaders['Authorization'])) { |
|
618
|
|
|
$requestHeaders['Authorization'] = 'Bearer ' . $this->authToken; |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
// Add content-type header (currently required GET requests) |
|
622
|
23 |
|
if (empty($requestHeaders['Content-Type'])) { |
|
623
|
18 |
|
$requestHeaders['Content-Type'] = $acceptContentType; |
|
624
|
|
|
} |
|
625
|
|
|
|
|
626
|
|
|
// Creating URI: URI params must be already provided by the calling method |
|
627
|
23 |
|
$endpoint = $this->uriFactory->createUri($this->options['endpoint']); |
|
628
|
23 |
|
$uri = $endpoint->withPath($endpoint->getPath() . $path) |
|
629
|
23 |
|
->withQuery($this->buildQuery($params)); |
|
630
|
|
|
|
|
631
|
|
|
// Create body, if provided |
|
632
|
23 |
|
$body = (!empty($data)) ? json_encode($data) : null; |
|
633
|
|
|
|
|
634
|
|
|
// Create and send a request |
|
635
|
23 |
|
$this->logger->debug('Sending request', [ |
|
636
|
23 |
|
'method' => $method, |
|
637
|
23 |
|
'uri' => (string) $uri, |
|
638
|
23 |
|
'headers' => $requestHeaders, |
|
639
|
23 |
|
'body' => $body |
|
640
|
|
|
]); |
|
641
|
23 |
|
$request = $this->requestFactory->createRequest($method, $uri, $requestHeaders, $body); |
|
642
|
23 |
|
$response = $this->httpClient->sendRequest($request); |
|
643
|
23 |
|
$this->logger->debug('Received response', [ |
|
644
|
23 |
|
'status' => $response->getStatusCode(), |
|
645
|
23 |
|
'reason' => $response->getReasonPhrase(), |
|
646
|
23 |
|
'headers' => $response->getHeaders(), |
|
647
|
23 |
|
'body' => (string) $response->getBody() |
|
648
|
|
|
]); |
|
649
|
|
|
// Check response content type match |
|
650
|
23 |
|
$contentType = $response->getHeaderLine('Content-Type'); |
|
651
|
23 |
|
if (204 !== $response->getStatusCode() && $contentType !== $acceptContentType) { |
|
652
|
1 |
|
throw new Error\Generic(sprintf("Invalid response content type: %s", $contentType)); |
|
653
|
|
|
} |
|
654
|
|
|
|
|
655
|
|
|
// Return the decoded JSON and let the caller create the appropriate result format |
|
656
|
22 |
|
return $this->parseResponse($response, $requestHeaders['X-Request-ID']); |
|
657
|
|
|
} |
|
658
|
|
|
} |
|
659
|
|
|
|