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
|
11 |
|
public function __construct($options = []) |
41
|
|
|
{ |
42
|
|
|
/* |
43
|
|
|
* Allow on instantiation to overwrite the defaults |
44
|
|
|
*/ |
45
|
11 |
|
$this->options = array_merge( |
|
|
|
|
46
|
11 |
|
$this->options, |
47
|
11 |
|
$options |
48
|
|
|
); |
49
|
|
|
$config = [ |
50
|
11 |
|
'base_uri' => sprintf( |
51
|
11 |
|
'%s://%s:%s/', |
52
|
11 |
|
$this->options['scheme'], |
53
|
11 |
|
$this->options['hostname'], |
54
|
11 |
|
$this->options['port'] |
55
|
|
|
), |
56
|
|
|
'verify' => false, |
57
|
|
|
'headers' => [ |
58
|
11 |
|
'User-Agent' => 'SmartcallRestfulAPIClient-PHP/'.self::VERSION.' '.\GuzzleHttp\default_user_agent(), |
59
|
|
|
], |
60
|
|
|
]; |
61
|
11 |
|
parent::__construct($config); |
62
|
11 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the bearer token. |
66
|
|
|
* |
67
|
|
|
* @param string $token Bearer Token from Auth request |
68
|
|
|
*/ |
69
|
3 |
|
public function setBearerToken($token) |
70
|
|
|
{ |
71
|
3 |
|
$this->options['token'] = $token; |
72
|
3 |
|
} |
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 |
|
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
|
|
|
* Test SmartCall is responding. |
239
|
|
|
* |
240
|
|
|
* @throws Exception |
241
|
|
|
* |
242
|
|
|
* @return array |
243
|
|
|
*/ |
244
|
1 |
|
public function ping() |
245
|
|
|
{ |
246
|
|
|
try { |
247
|
1 |
|
$response = $this->get( |
248
|
1 |
|
'/webservice/test/ping' |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
return [ |
252
|
1 |
|
'status' => 'ok', |
253
|
1 |
|
'http_code' => $response->getStatusCode(), |
254
|
1 |
|
'body' => (string) $response->getBody(), |
255
|
|
|
]; |
256
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
257
|
|
|
return $this->clientError($e); |
258
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
259
|
|
|
return $this->parseError($e); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Parse the java exception that we receive from Smartcall's Tomcat's. |
265
|
|
|
* |
266
|
|
|
* @param \GuzzleHttp\Exception\ServerException $exception |
267
|
|
|
* |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
5 |
View Code Duplication |
private function clientError(\GuzzleHttp\Exception\ClientException $exception) |
|
|
|
|
271
|
|
|
{ |
272
|
5 |
|
$body = (string) $exception->getResponse()->getBody(); |
273
|
|
|
|
274
|
|
|
return [ |
275
|
5 |
|
'status' => 'error', |
276
|
5 |
|
'http_code' => $exception->getResponse()->getStatusCode(), |
277
|
5 |
|
'body' => json_decode($body), |
278
|
|
|
]; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Parse the java exception that we receive from Smartcall's Tomcat's. |
283
|
|
|
* |
284
|
|
|
* @param \GuzzleHttp\Exception\ServerException $exception |
285
|
|
|
* |
286
|
|
|
* @return array |
287
|
|
|
*/ |
288
|
|
View Code Duplication |
private function parseError(\GuzzleHttp\Exception\ServerException $exception) |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
$body = (string) $exception->getResponse()->getBody(); |
291
|
|
|
preg_match('!<p><b>type</b> Exception report</p><p><b>message</b> <u>(.*[^</u>])</u></p><p><b>description</b>!', $body, $matches); |
292
|
|
|
return [ |
293
|
|
|
'status' => 'error', |
294
|
|
|
'http_code' => $exception->getResponse()->getStatusCode(), |
295
|
|
|
'body' => $matches['1'], |
296
|
|
|
]; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
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..