1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SmartCall Restful API (v3) HTTP Client. |
4
|
|
|
* |
5
|
|
|
* PLEASE NOTE: The interface is very fluid while the intial integration |
6
|
|
|
* is taking place. It will be refactored in the near future. |
7
|
|
|
* |
8
|
|
|
* @author Jacques Marneweck <[email protected]> |
9
|
|
|
* @copyright 2017-2018 Jacques Marneweck. All rights strictly reserved. |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Jacques\Smartcall\HttpClient; |
14
|
|
|
|
15
|
|
|
class Client extends \GuzzleHttp\Client |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @const string Version number |
19
|
|
|
*/ |
20
|
|
|
const VERSION = '0.0.1'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Defaults to expecting that Apache Tomcat runs on port 8080 on localhost |
24
|
|
|
* (127.0.0.1). |
25
|
|
|
* |
26
|
|
|
* @var array[] |
27
|
|
|
*/ |
28
|
|
|
protected $options = [ |
29
|
|
|
'scheme' => 'https', |
30
|
|
|
'hostname' => 'localhost', |
31
|
|
|
'port' => '8080', |
32
|
|
|
'token' => null, |
33
|
|
|
'username' => null, |
34
|
|
|
'password' => null, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $options array |
39
|
|
|
*/ |
40
|
17 |
|
public function __construct($options = []) |
41
|
|
|
{ |
42
|
|
|
/* |
43
|
|
|
* Allow on instantiation to overwrite the defaults |
44
|
|
|
*/ |
45
|
17 |
|
$this->options = array_merge( |
|
|
|
|
46
|
17 |
|
$this->options, |
47
|
17 |
|
$options |
48
|
|
|
); |
49
|
|
|
$config = [ |
50
|
17 |
|
'base_uri' => sprintf( |
51
|
17 |
|
'%s://%s:%s/', |
52
|
17 |
|
$this->options['scheme'], |
53
|
17 |
|
$this->options['hostname'], |
54
|
17 |
|
$this->options['port'] |
55
|
|
|
), |
56
|
|
|
'verify' => false, |
57
|
|
|
'headers' => [ |
58
|
17 |
|
'User-Agent' => 'SmartcallRestfulAPIClient-PHP/'.self::VERSION.' '.\GuzzleHttp\default_user_agent(), |
59
|
|
|
], |
60
|
|
|
]; |
61
|
17 |
|
parent::__construct($config); |
62
|
17 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the bearer token. |
66
|
|
|
* |
67
|
|
|
* @param string $token Bearer Token from Auth request |
68
|
|
|
*/ |
69
|
9 |
|
public function setBearerToken($token) |
70
|
|
|
{ |
71
|
9 |
|
$this->options['token'] = $token; |
72
|
9 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Authenticate and get Bearer token from SmartCall. |
76
|
|
|
* |
77
|
|
|
* @param string $username |
78
|
|
|
* @param string $password |
79
|
|
|
* |
80
|
|
|
* @throws Exception |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
3 |
View Code Duplication |
public function auth($username, $password) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
try { |
87
|
3 |
|
$response = $this->post( |
88
|
3 |
|
'/webservice/auth', |
89
|
|
|
[ |
90
|
|
|
'headers' => [ |
91
|
3 |
|
'Authorization' => sprintf( |
92
|
3 |
|
'Basic %s', |
93
|
3 |
|
base64_encode( |
94
|
3 |
|
sprintf( |
95
|
3 |
|
'%s:%s', |
96
|
3 |
|
$username, |
97
|
3 |
|
$password |
98
|
|
|
) |
99
|
|
|
) |
100
|
|
|
), |
101
|
|
|
], |
102
|
|
|
] |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
return [ |
106
|
1 |
|
'status' => 'ok', |
107
|
1 |
|
'http_code' => $response->getStatusCode(), |
108
|
1 |
|
'body' => (string) $response->getBody(), |
109
|
|
|
]; |
110
|
2 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
111
|
2 |
|
return $this->clientError($e); |
112
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
113
|
|
|
return $this->parseError($e); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Authenticate and invalidates all the user allocated tokens. |
119
|
|
|
* |
120
|
|
|
* @throws Exception |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
2 |
View Code Duplication |
public function authDelete() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
try { |
127
|
2 |
|
$response = $this->delete( |
128
|
2 |
|
'/webservice/auth', |
129
|
|
|
[ |
130
|
|
|
'headers' => [ |
131
|
2 |
|
'Authorization' => sprintf( |
132
|
2 |
|
'Bearer %s', |
133
|
2 |
|
$this->options['token'] |
134
|
|
|
), |
135
|
|
|
], |
136
|
|
|
] |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
return [ |
140
|
1 |
|
'status' => 'ok', |
141
|
1 |
|
'http_code' => $response->getStatusCode(), |
142
|
1 |
|
'body' => (string) $response->getBody(), |
143
|
|
|
]; |
144
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
145
|
1 |
|
return $this->clientError($e); |
146
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
147
|
|
|
return $this->parseError($e); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Authenticate and invalidates all the user allocated tokens. |
153
|
|
|
* |
154
|
|
|
* @param string $username |
155
|
|
|
* @param string $password |
156
|
|
|
* |
157
|
|
|
* @throws Exception |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function authFlush($username, $password) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
try { |
164
|
|
|
$response = $this->delete( |
165
|
|
|
'/webservice/auth/token', |
166
|
|
|
[ |
167
|
|
|
'headers' => [ |
168
|
|
|
'Authorization' => sprintf( |
169
|
|
|
'Basic %s', |
170
|
|
|
base64_encode( |
171
|
|
|
sprintf( |
172
|
|
|
'%s:%s', |
173
|
|
|
$username, |
174
|
|
|
$password |
175
|
|
|
) |
176
|
|
|
) |
177
|
|
|
), |
178
|
|
|
], |
179
|
|
|
] |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
return [ |
183
|
|
|
'status' => 'ok', |
184
|
|
|
'http_code' => $response->getStatusCode(), |
185
|
|
|
'body' => (string) $response->getBody(), |
186
|
|
|
]; |
187
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
188
|
|
|
return $this->clientError($e); |
189
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
190
|
|
|
return $this->parseError($e); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Authenticate and gets the number of available session tokens. |
196
|
|
|
* |
197
|
|
|
* @param string $username |
198
|
|
|
* @param string $password |
199
|
|
|
* |
200
|
|
|
* @throws Exception |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
3 |
View Code Duplication |
public function authToken($username, $password) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
try { |
207
|
3 |
|
$response = $this->get( |
208
|
3 |
|
'/webservice/auth/token', |
209
|
|
|
[ |
210
|
|
|
'headers' => [ |
211
|
3 |
|
'Authorization' => sprintf( |
212
|
3 |
|
'Basic %s', |
213
|
3 |
|
base64_encode( |
214
|
3 |
|
sprintf( |
215
|
3 |
|
'%s:%s', |
216
|
3 |
|
$username, |
217
|
3 |
|
$password |
218
|
|
|
) |
219
|
|
|
) |
220
|
|
|
), |
221
|
|
|
], |
222
|
|
|
] |
223
|
|
|
); |
224
|
|
|
|
225
|
|
|
return [ |
226
|
1 |
|
'status' => 'ok', |
227
|
1 |
|
'http_code' => $response->getStatusCode(), |
228
|
1 |
|
'body' => (string) $response->getBody(), |
229
|
|
|
]; |
230
|
2 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
231
|
2 |
|
return $this->clientError($e); |
232
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
233
|
|
|
return $this->parseError($e); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Authenticate and retrieves a list of all available networks. |
239
|
|
|
* |
240
|
|
|
* @throws Exception |
241
|
|
|
* |
242
|
|
|
* @return array |
243
|
|
|
*/ |
244
|
2 |
View Code Duplication |
public function network($id) |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
try { |
247
|
2 |
|
$response = $this->get( |
248
|
2 |
|
sprintf( |
249
|
2 |
|
'/webservice/smartload/networks/%d', |
250
|
2 |
|
$id |
251
|
|
|
), |
252
|
|
|
[ |
253
|
|
|
'headers' => [ |
254
|
2 |
|
'Authorization' => sprintf( |
255
|
2 |
|
'Bearer %s', |
256
|
2 |
|
$this->options['token'] |
257
|
|
|
), |
258
|
|
|
], |
259
|
|
|
] |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
return [ |
263
|
1 |
|
'status' => 'ok', |
264
|
1 |
|
'http_code' => $response->getStatusCode(), |
265
|
1 |
|
'body' => (string) $response->getBody(), |
266
|
|
|
]; |
267
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
268
|
1 |
|
return $this->clientError($e); |
269
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
270
|
|
|
return $this->parseError($e); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Authenticate and retrieves a list of all available networks. |
276
|
|
|
* |
277
|
|
|
* @throws Exception |
278
|
|
|
* |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
2 |
View Code Duplication |
public function networks() |
|
|
|
|
282
|
|
|
{ |
283
|
|
|
try { |
284
|
2 |
|
$response = $this->get( |
285
|
2 |
|
'/webservice/smartload/networks', |
286
|
|
|
[ |
287
|
|
|
'headers' => [ |
288
|
2 |
|
'Authorization' => sprintf( |
289
|
2 |
|
'Bearer %s', |
290
|
2 |
|
$this->options['token'] |
291
|
|
|
), |
292
|
|
|
], |
293
|
|
|
] |
294
|
|
|
); |
295
|
|
|
|
296
|
|
|
return [ |
297
|
1 |
|
'status' => 'ok', |
298
|
1 |
|
'http_code' => $response->getStatusCode(), |
299
|
1 |
|
'body' => (string) $response->getBody(), |
300
|
|
|
]; |
301
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
302
|
1 |
|
return $this->clientError($e); |
303
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
304
|
|
|
return $this->parseError($e); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Test SmartCall is responding. |
310
|
|
|
* |
311
|
|
|
* @throws Exception |
312
|
|
|
* |
313
|
|
|
* @return array |
314
|
|
|
*/ |
315
|
1 |
|
public function ping() |
316
|
|
|
{ |
317
|
|
|
try { |
318
|
1 |
|
$response = $this->get( |
319
|
1 |
|
'/webservice/test/ping' |
320
|
|
|
); |
321
|
|
|
|
322
|
|
|
return [ |
323
|
1 |
|
'status' => 'ok', |
324
|
1 |
|
'http_code' => $response->getStatusCode(), |
325
|
1 |
|
'body' => (string) $response->getBody(), |
326
|
|
|
]; |
327
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
328
|
|
|
return $this->clientError($e); |
329
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
330
|
|
|
return $this->parseError($e); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Authenticate and retrieves a list of all available networks. |
336
|
|
|
* |
337
|
|
|
* @throws Exception |
338
|
|
|
* |
339
|
|
|
* @return array |
340
|
|
|
*/ |
341
|
2 |
View Code Duplication |
public function products($id) |
|
|
|
|
342
|
|
|
{ |
343
|
|
|
try { |
344
|
2 |
|
$response = $this->get( |
345
|
2 |
|
sprintf( |
346
|
2 |
|
'/webservice/smartload/products/%d', |
347
|
2 |
|
$id |
348
|
|
|
), |
349
|
|
|
[ |
350
|
|
|
'headers' => [ |
351
|
2 |
|
'Authorization' => sprintf( |
352
|
2 |
|
'Bearer %s', |
353
|
2 |
|
$this->options['token'] |
354
|
|
|
), |
355
|
|
|
], |
356
|
|
|
] |
357
|
|
|
); |
358
|
|
|
|
359
|
|
|
return [ |
360
|
1 |
|
'status' => 'ok', |
361
|
1 |
|
'http_code' => $response->getStatusCode(), |
362
|
1 |
|
'body' => (string) $response->getBody(), |
363
|
|
|
]; |
364
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
365
|
1 |
|
return $this->clientError($e); |
366
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
367
|
|
|
return $this->parseError($e); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Parse the java exception that we receive from Smartcall's Tomcat's. |
373
|
|
|
* |
374
|
|
|
* @param \GuzzleHttp\Exception\ServerException $exception |
375
|
|
|
* |
376
|
|
|
* @return array |
377
|
|
|
*/ |
378
|
8 |
View Code Duplication |
private function clientError(\GuzzleHttp\Exception\ClientException $exception) |
|
|
|
|
379
|
|
|
{ |
380
|
8 |
|
$body = (string) $exception->getResponse()->getBody(); |
381
|
|
|
|
382
|
|
|
return [ |
383
|
8 |
|
'status' => 'error', |
384
|
8 |
|
'http_code' => $exception->getResponse()->getStatusCode(), |
385
|
8 |
|
'body' => json_decode($body), |
386
|
|
|
]; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Parse the java exception that we receive from Smartcall's Tomcat's. |
391
|
|
|
* |
392
|
|
|
* @param \GuzzleHttp\Exception\ServerException $exception |
393
|
|
|
* |
394
|
|
|
* @return array |
395
|
|
|
*/ |
396
|
|
View Code Duplication |
private function parseError(\GuzzleHttp\Exception\ServerException $exception) |
|
|
|
|
397
|
|
|
{ |
398
|
|
|
$body = (string) $exception->getResponse()->getBody(); |
399
|
|
|
preg_match('!<p><b>type</b> Exception report</p><p><b>message</b> <u>(.*[^</u>])</u></p><p><b>description</b>!', $body, $matches); |
400
|
|
|
return [ |
401
|
|
|
'status' => 'error', |
402
|
|
|
'http_code' => $exception->getResponse()->getStatusCode(), |
403
|
|
|
'body' => $matches['1'], |
404
|
|
|
]; |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..