|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Yandex PHP Library |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright NIX Solutions Ltd. |
|
6
|
|
|
* @link https://github.com/nixsolutions/yandex-php-library |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @namespace |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace Yandex\Common; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
|
15
|
|
|
use GuzzleHttp\ClientInterface; |
|
16
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
17
|
|
|
use GuzzleHttp\Psr7\Response; |
|
18
|
|
|
use Yandex\Common\Exception\MissedArgumentException; |
|
19
|
|
|
use Yandex\Common\Exception\ProfileNotFoundException; |
|
20
|
|
|
use Yandex\Common\Exception\YandexException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class AbstractServiceClient |
|
24
|
|
|
* |
|
25
|
|
|
* @package Yandex\Common |
|
26
|
|
|
* |
|
27
|
|
|
* @author Eugene Zabolotniy <[email protected]> |
|
28
|
|
|
* @created 13.09.13 12:09 |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class AbstractServiceClient extends AbstractPackage |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Request schemes constants |
|
34
|
|
|
*/ |
|
35
|
|
|
const HTTPS_SCHEME = 'https'; |
|
36
|
|
|
const HTTP_SCHEME = 'http'; |
|
37
|
|
|
|
|
38
|
|
|
const DECODE_TYPE_JSON = 'json'; |
|
39
|
|
|
const DECODE_TYPE_XML = 'xml'; |
|
40
|
|
|
const DECODE_TYPE_DEFAULT = self::DECODE_TYPE_JSON; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $serviceScheme = self::HTTPS_SCHEME; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Can be HTTP 1.0 or HTTP 1.1 |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $serviceProtocolVersion = '1.1'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $serviceDomain = ''; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $servicePort = ''; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $accessToken = ''; |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var \DateTime |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $expiresIn = null; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $proxy = ''; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var bool |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $debug = false; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var null |
|
86
|
|
|
*/ |
|
87
|
|
|
protected $client = null; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var string |
|
91
|
|
|
*/ |
|
92
|
|
|
protected $libraryName = 'yandex-php-library'; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public function getUserAgent() |
|
98
|
|
|
{ |
|
99
|
3 |
|
return $this->libraryName . '/' . Version::$version; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $accessToken |
|
104
|
|
|
* |
|
105
|
|
|
* @return self |
|
106
|
|
|
*/ |
|
107
|
170 |
|
public function setAccessToken($accessToken) |
|
108
|
|
|
{ |
|
109
|
170 |
|
$this->accessToken = $accessToken; |
|
110
|
|
|
|
|
111
|
170 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
45 |
|
public function getAccessToken() |
|
118
|
|
|
{ |
|
119
|
45 |
|
return $this->accessToken; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param \DateTime $expiresIn |
|
124
|
|
|
*/ |
|
125
|
3 |
|
public function setExpiresIn($expiresIn) |
|
126
|
|
|
{ |
|
127
|
3 |
|
$this->expiresIn = $expiresIn; |
|
128
|
3 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return \DateTime |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function getExpiresIn() |
|
134
|
|
|
{ |
|
135
|
2 |
|
return $this->expiresIn; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param $proxy |
|
140
|
|
|
* @return $this |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setProxy($proxy) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->proxy = $proxy; |
|
145
|
|
|
|
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
3 |
|
public function getProxy() |
|
153
|
|
|
{ |
|
154
|
3 |
|
return $this->proxy; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param $debug |
|
159
|
|
|
* @return $this |
|
160
|
|
|
*/ |
|
161
|
|
|
public function setDebug($debug) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->debug = (bool) $debug; |
|
164
|
|
|
|
|
165
|
|
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return bool |
|
170
|
|
|
*/ |
|
171
|
3 |
|
public function getDebug() |
|
172
|
|
|
{ |
|
173
|
3 |
|
return $this->debug; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param string $serviceDomain |
|
178
|
|
|
* |
|
179
|
|
|
* @return self |
|
180
|
|
|
*/ |
|
181
|
4 |
|
public function setServiceDomain($serviceDomain) |
|
182
|
|
|
{ |
|
183
|
4 |
|
$this->serviceDomain = $serviceDomain; |
|
184
|
|
|
|
|
185
|
4 |
|
return $this; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
9 |
|
public function getServiceDomain() |
|
192
|
|
|
{ |
|
193
|
9 |
|
return $this->serviceDomain; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param string $servicePort |
|
198
|
|
|
* |
|
199
|
|
|
* @return self |
|
200
|
|
|
*/ |
|
201
|
4 |
|
public function setServicePort($servicePort) |
|
202
|
|
|
{ |
|
203
|
4 |
|
$this->servicePort = $servicePort; |
|
204
|
|
|
|
|
205
|
4 |
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
4 |
|
public function getServicePort() |
|
212
|
|
|
{ |
|
213
|
4 |
|
return $this->servicePort; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param string $serviceScheme |
|
218
|
|
|
* |
|
219
|
|
|
* @return self |
|
220
|
|
|
*/ |
|
221
|
4 |
|
public function setServiceScheme($serviceScheme = self::HTTPS_SCHEME) |
|
222
|
|
|
{ |
|
223
|
4 |
|
$this->serviceScheme = $serviceScheme; |
|
224
|
|
|
|
|
225
|
4 |
|
return $this; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
6 |
|
public function getServiceScheme() |
|
232
|
|
|
{ |
|
233
|
6 |
|
return $this->serviceScheme; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param string $resource |
|
238
|
|
|
* @return string |
|
239
|
|
|
*/ |
|
240
|
17 |
|
public function getServiceUrl($resource = '') |
|
241
|
|
|
{ |
|
242
|
17 |
|
return $this->serviceScheme . '://' . $this->serviceDomain . '/' . rawurlencode($resource); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Check package configuration |
|
247
|
|
|
* |
|
248
|
|
|
* @return boolean |
|
249
|
|
|
*/ |
|
250
|
|
|
protected function doCheckSettings() |
|
251
|
|
|
{ |
|
252
|
|
|
return true; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param ClientInterface $client |
|
257
|
|
|
* @return $this |
|
258
|
|
|
*/ |
|
259
|
2 |
|
protected function setClient(ClientInterface $client) |
|
260
|
|
|
{ |
|
261
|
2 |
|
$this->client = $client; |
|
|
|
|
|
|
262
|
|
|
|
|
263
|
2 |
|
return $this; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @return ClientInterface |
|
268
|
|
|
*/ |
|
269
|
4 |
View Code Duplication |
protected function getClient() |
|
|
|
|
|
|
270
|
|
|
{ |
|
271
|
4 |
|
if (is_null($this->client)) { |
|
272
|
|
|
$defaultOptions = [ |
|
273
|
3 |
|
'base_uri' => $this->getServiceUrl(), |
|
274
|
|
|
'headers' => [ |
|
275
|
3 |
|
'Authorization' => 'OAuth ' . $this->getAccessToken(), |
|
276
|
3 |
|
'Host' => $this->getServiceDomain(), |
|
277
|
3 |
|
'User-Agent' => $this->getUserAgent(), |
|
278
|
3 |
|
'Accept' => '*/*' |
|
279
|
|
|
] |
|
280
|
|
|
]; |
|
281
|
3 |
|
if ($this->getProxy()) { |
|
282
|
|
|
$defaultOptions['proxy'] = $this->getProxy(); |
|
283
|
|
|
} |
|
284
|
3 |
|
if ($this->getDebug()) { |
|
285
|
|
|
$defaultOptions['debug'] = $this->getDebug(); |
|
286
|
|
|
} |
|
287
|
3 |
|
$this->client = new Client($defaultOptions); |
|
|
|
|
|
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
4 |
|
return $this->client; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Sends a request |
|
295
|
|
|
* |
|
296
|
|
|
* @param string $method HTTP method |
|
297
|
|
|
* @param string $uri URI object or string. |
|
298
|
|
|
* @param array $options Request options to apply. |
|
299
|
|
|
* |
|
300
|
|
|
* @throws Exception\MissedArgumentException |
|
301
|
|
|
* @throws Exception\ProfileNotFoundException |
|
302
|
|
|
* @throws Exception\YandexException |
|
303
|
|
|
* @return Response |
|
304
|
|
|
*/ |
|
305
|
8 |
|
protected function sendRequest($method, $uri, array $options = []) |
|
306
|
|
|
{ |
|
307
|
|
|
try { |
|
308
|
8 |
|
$response = $this->getClient()->request($method, $uri, $options); |
|
309
|
|
|
} catch (ClientException $ex) { |
|
310
|
|
|
// get error from response |
|
311
|
|
|
$decodedResponseBody = $this->getDecodedBody($ex->getResponse()->getBody()); |
|
312
|
|
|
$code = $ex->getResponse()->getStatusCode(); |
|
313
|
|
|
|
|
314
|
|
|
// handle a service error message |
|
315
|
|
|
if (is_array($decodedResponseBody) |
|
316
|
|
|
&& isset($decodedResponseBody['error'], $decodedResponseBody['message']) |
|
317
|
|
|
) { |
|
318
|
|
|
switch ($decodedResponseBody['error']) { |
|
319
|
|
|
case 'MissedRequiredArguments': |
|
320
|
|
|
throw new MissedArgumentException($decodedResponseBody['message']); |
|
321
|
|
|
case 'AssistantProfileNotFound': |
|
322
|
|
|
throw new ProfileNotFoundException($decodedResponseBody['message']); |
|
323
|
|
|
default: |
|
324
|
|
|
throw new YandexException($decodedResponseBody['message'], $code); |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
// unknown error |
|
329
|
|
|
throw $ex; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
8 |
|
return $response; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* @param $body |
|
337
|
|
|
* @param string $type |
|
338
|
|
|
* @return mixed|\SimpleXMLElement |
|
339
|
|
|
*/ |
|
340
|
113 |
|
protected function getDecodedBody($body, $type = null) |
|
341
|
|
|
{ |
|
342
|
113 |
|
if (!isset($type)) { |
|
343
|
113 |
|
$type = static::DECODE_TYPE_DEFAULT; |
|
344
|
|
|
} |
|
345
|
|
|
switch ($type) { |
|
346
|
113 |
|
case self::DECODE_TYPE_XML: |
|
347
|
14 |
|
return simplexml_load_string((string) $body); |
|
348
|
99 |
|
case self::DECODE_TYPE_JSON: |
|
349
|
|
|
default: |
|
350
|
99 |
|
return json_decode((string) $body, true); |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
|
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..