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\ServerException $e) { |
111
|
|
|
return $this->parseError($e); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Authenticate and invalidates all the user allocated tokens |
117
|
|
|
* |
118
|
|
|
* @param string $username |
|
|
|
|
119
|
|
|
* @param string $password |
|
|
|
|
120
|
|
|
* |
121
|
|
|
* @throws Exception |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
2 |
|
public function authDelete() |
126
|
|
|
{ |
127
|
|
|
try { |
128
|
2 |
|
$response = $this->delete( |
129
|
2 |
|
'/webservice/auth', |
130
|
|
|
[ |
131
|
|
|
'headers' => [ |
132
|
2 |
|
'Authorization' => sprintf( |
133
|
2 |
|
'Bearer %s', |
134
|
2 |
|
$this->options['token'] |
135
|
|
|
), |
136
|
|
|
], |
137
|
|
|
] |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
return [ |
141
|
1 |
|
'status' => 'ok', |
142
|
1 |
|
'http_code' => $response->getStatusCode(), |
143
|
1 |
|
'body' => (string) $response->getBody(), |
144
|
|
|
]; |
145
|
1 |
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
146
|
|
|
return $this->parseError($e); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Authenticate and invalidates all the user allocated tokens |
152
|
|
|
* |
153
|
|
|
* @param string $username |
154
|
|
|
* @param string $password |
155
|
|
|
* |
156
|
|
|
* @throws Exception |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function authFlush($username, $password) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
try { |
163
|
|
|
$response = $this->delete( |
164
|
|
|
'/webservice/auth/token', |
165
|
|
|
[ |
166
|
|
|
'headers' => [ |
167
|
|
|
'Authorization' => sprintf( |
168
|
|
|
'Basic %s', |
169
|
|
|
base64_encode( |
170
|
|
|
sprintf( |
171
|
|
|
'%s:%s', |
172
|
|
|
$username, |
173
|
|
|
$password |
174
|
|
|
) |
175
|
|
|
) |
176
|
|
|
), |
177
|
|
|
], |
178
|
|
|
] |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
return [ |
182
|
|
|
'status' => 'ok', |
183
|
|
|
'http_code' => $response->getStatusCode(), |
184
|
|
|
'body' => (string) $response->getBody(), |
185
|
|
|
]; |
186
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
187
|
|
|
return $this->parseError($e); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Authenticate and gets the number of available session tokens |
193
|
|
|
* |
194
|
|
|
* @param string $username |
195
|
|
|
* @param string $password |
196
|
|
|
* |
197
|
|
|
* @throws Exception |
198
|
|
|
* |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
3 |
View Code Duplication |
public function authToken($username, $password) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
try { |
204
|
3 |
|
$response = $this->get( |
205
|
3 |
|
'/webservice/auth/token', |
206
|
|
|
[ |
207
|
|
|
'headers' => [ |
208
|
3 |
|
'Authorization' => sprintf( |
209
|
3 |
|
'Basic %s', |
210
|
3 |
|
base64_encode( |
211
|
3 |
|
sprintf( |
212
|
3 |
|
'%s:%s', |
213
|
3 |
|
$username, |
214
|
3 |
|
$password |
215
|
|
|
) |
216
|
|
|
) |
217
|
|
|
), |
218
|
|
|
], |
219
|
|
|
] |
220
|
|
|
); |
221
|
|
|
|
222
|
|
|
return [ |
223
|
1 |
|
'status' => 'ok', |
224
|
1 |
|
'http_code' => $response->getStatusCode(), |
225
|
1 |
|
'body' => (string) $response->getBody(), |
226
|
|
|
]; |
227
|
2 |
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
228
|
|
|
return $this->parseError($e); |
|
|
|
|
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Test SmartCall is responding. |
234
|
|
|
* |
235
|
|
|
* @throws Exception |
236
|
|
|
* |
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
1 |
|
public function ping() |
240
|
|
|
{ |
241
|
|
|
try { |
242
|
1 |
|
$response = $this->get( |
243
|
1 |
|
'/webservice/test/ping' |
244
|
|
|
); |
245
|
|
|
|
246
|
|
|
return [ |
247
|
1 |
|
'status' => 'ok', |
248
|
1 |
|
'http_code' => $response->getStatusCode(), |
249
|
1 |
|
'body' => (string) $response->getBody(), |
250
|
|
|
]; |
251
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
252
|
|
|
return $this->parseError($e); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
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..