1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of RussianPost SDK package. |
5
|
|
|
* |
6
|
|
|
* © Appwilio (http://appwilio.com), JhaoDa (https://github.com/jhaoda) |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Appwilio\RussianPostSDK\Dispatching\Http; |
15
|
|
|
|
16
|
|
|
use GuzzleHttp\Psr7\Request; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Psr\Log\LoggerAwareTrait; |
19
|
|
|
use Psr\Log\LoggerAwareInterface; |
20
|
|
|
use GuzzleHttp\ClientInterface; |
21
|
|
|
use GuzzleHttp\Psr7\UploadedFile; |
22
|
|
|
use GuzzleHttp\Exception\ClientException; |
23
|
|
|
use GuzzleHttp\Exception\ServerException; |
24
|
|
|
use Psr\Http\Message\RequestInterface; |
25
|
|
|
use Psr\Http\Message\ResponseInterface; |
26
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Instantiator; |
27
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Contracts\Arrayable; |
28
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Exceptions\BadRequest; |
29
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Contracts\DispatchingException; |
30
|
|
|
use function GuzzleHttp\json_encode as guzzle_json_encode; |
31
|
|
|
use function GuzzleHttp\json_decode as guzzle_json_decode; |
32
|
|
|
use function GuzzleHttp\Psr7\stream_for as guzzle_stream_for; |
33
|
|
|
use function GuzzleHttp\Psr7\build_query as guzzle_build_query; |
34
|
|
|
use function GuzzleHttp\Psr7\modify_request as guzzle_modify_request; |
35
|
|
|
|
36
|
|
|
final class ApiClient implements LoggerAwareInterface |
37
|
|
|
{ |
38
|
|
|
use LoggerAwareTrait; |
39
|
|
|
|
40
|
|
|
private const API_URL = 'https://otpravka-api.pochta.ru'; |
41
|
|
|
|
42
|
|
|
/** @var Authentication */ |
43
|
|
|
private $authentication; |
44
|
|
|
|
45
|
|
|
/** @var ClientInterface */ |
46
|
|
|
private $httpClient; |
47
|
|
|
|
48
|
34 |
|
public function __construct(Authentication $authentication, ClientInterface $httpClient, LoggerInterface $logger) |
49
|
|
|
{ |
50
|
34 |
|
$this->authentication = $authentication; |
51
|
34 |
|
$this->httpClient = $httpClient; |
52
|
34 |
|
$this->logger = $logger; |
53
|
34 |
|
} |
54
|
|
|
|
55
|
23 |
|
public function get(string $path, ?Arrayable $request = null, $type = null) |
56
|
|
|
{ |
57
|
23 |
|
return $this->send('GET', ...\func_get_args()); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
public function post(string $path, Arrayable $request, $type = null) |
61
|
|
|
{ |
62
|
6 |
|
return $this->send('POST', ...\func_get_args()); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function put(string $path, Arrayable $request, $type = null) |
66
|
|
|
{ |
67
|
1 |
|
return $this->send('PUT', ...\func_get_args()); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function delete(string $path, Arrayable $request, $type = null) |
71
|
|
|
{ |
72
|
1 |
|
return $this->send('DELETE', ...\func_get_args()); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Выполнение запроса. |
77
|
|
|
* |
78
|
|
|
* @param string $method |
79
|
|
|
* @param string $path |
80
|
|
|
* @param Arrayable|null $request |
81
|
|
|
* @param mixed $responseType |
82
|
|
|
* |
83
|
|
|
* @throws DispatchingException |
84
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
31 |
|
private function send(string $method, string $path, ?Arrayable $request = null, $responseType = null) |
89
|
|
|
{ |
90
|
31 |
|
$method = \strtoupper($method); |
91
|
|
|
|
92
|
|
|
try { |
93
|
31 |
|
$response = $this->httpClient->send($this->buildHttpRequest($method, $path, $request)); |
94
|
|
|
|
95
|
12 |
|
$contentType = $response->getHeaderLine('Content-Type'); |
96
|
|
|
|
97
|
12 |
|
if (\preg_match('~^application/(pdf|zip)$~', $contentType, $matches)) { |
98
|
1 |
|
return $this->buildFile($response, $matches[1]); |
99
|
|
|
} |
100
|
|
|
|
101
|
11 |
|
if (\preg_match('~^application/json~', $contentType)) { |
102
|
10 |
|
$content = $this->getResponseContent($response); |
103
|
|
|
|
104
|
10 |
|
$this->logger->info('pochta.ru Dispatching response:.', $content); |
105
|
|
|
|
106
|
10 |
|
return $responseType === null |
107
|
4 |
|
? $content |
108
|
10 |
|
: Instantiator::instantiate($responseType, $content); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
throw new BadRequest(); |
112
|
20 |
|
} catch (ClientException $e) { |
113
|
18 |
|
throw $this->handleClientException($e); |
114
|
2 |
|
} catch (ServerException $e) { |
115
|
1 |
|
throw $this->handleServerException($e); |
116
|
1 |
|
} catch (\Exception $e) { |
117
|
1 |
|
throw $e; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
31 |
|
private function buildHttpRequest(string $method, string $path, ?Arrayable $payload): RequestInterface |
122
|
|
|
{ |
123
|
31 |
|
$request = $this->authentication->authenticate( |
124
|
31 |
|
new Request($method, self::API_URL.$path, ['Accept' => 'application/json;charset=UTF-8']) |
125
|
|
|
); |
126
|
|
|
|
127
|
31 |
|
if ($payload === null) { |
128
|
23 |
|
return $request; |
129
|
|
|
} |
130
|
|
|
|
131
|
9 |
|
$data = $this->serializeRequestData(\array_filter($payload->toArray())); |
132
|
|
|
|
133
|
9 |
|
$this->logger->info("pochta.ru Dispatching request: {$path}", $data); |
134
|
|
|
|
135
|
9 |
|
if ($method === 'GET') { |
136
|
1 |
|
return guzzle_modify_request($request, ['query' => guzzle_build_query($data)]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $request |
140
|
8 |
|
->withHeader('Content-Type', 'application/json;charset=UTF-8') |
141
|
8 |
|
->withBody(guzzle_stream_for(guzzle_json_encode($data))); |
142
|
|
|
} |
143
|
|
|
|
144
|
9 |
|
private function serializeRequestData(array $data): array |
145
|
|
|
{ |
146
|
|
|
return \array_map(function ($value) { |
147
|
9 |
|
if (\is_object($value) && \method_exists($value, '__toString')) { |
148
|
1 |
|
return (string) $value; |
149
|
|
|
} |
150
|
|
|
|
151
|
9 |
|
if (\is_array($value)) { |
152
|
5 |
|
return $this->serializeRequestData($value); |
153
|
|
|
} |
154
|
|
|
|
155
|
9 |
|
return $value; |
156
|
9 |
|
}, $data); |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
private function buildFile(ResponseInterface $response, string $type): UploadedFile |
160
|
|
|
{ |
161
|
1 |
|
\preg_match('~=(.+)$~', $response->getHeaderLine('Content-Disposition'), $matches); |
162
|
|
|
|
163
|
1 |
|
$this->logger->info("pochta.ru Dispatching response: file {$matches[1]}.{$type} ({$response->getBody()->getSize()} butes)."); |
164
|
|
|
|
165
|
1 |
|
return new UploadedFile( |
166
|
1 |
|
$response->getBody(), |
167
|
1 |
|
$response->getBody()->getSize(), |
168
|
1 |
|
\UPLOAD_ERR_OK, |
169
|
1 |
|
"{$matches[1]}.{$type}", |
170
|
1 |
|
$response->getHeaderLine('Content-Type') |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
18 |
|
private function handleClientException(ClientException $exception): DispatchingException |
175
|
|
|
{ |
176
|
18 |
|
if (\in_array($exception->getCode(), [401, 403])) { |
177
|
12 |
|
throw $this->handleAuthenticationException($exception); |
178
|
|
|
} |
179
|
|
|
|
180
|
6 |
|
$content = $this->getResponseContent($exception->getResponse()); |
|
|
|
|
181
|
|
|
|
182
|
6 |
|
return new BadRequest( |
183
|
6 |
|
$content['message'] ?? $content['error'] ?? $content['desc'], |
184
|
6 |
|
(int) ($content['status'] ?? $content['code'] ?? $exception->getCode()) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
12 |
|
private function handleAuthenticationException(ClientException $exception) |
189
|
|
|
{ |
190
|
12 |
|
$content = $this->getResponseContent($exception->getResponse()); |
|
|
|
|
191
|
|
|
|
192
|
12 |
|
return new BadRequest( |
193
|
12 |
|
$content['message'] ?? $content['desc'] ?? '', |
194
|
12 |
|
isset($content['code']) ? (int) $content['code'] : $exception->getCode() |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
private function handleServerException(ServerException $e): DispatchingException |
199
|
|
|
{ |
200
|
1 |
|
throw $e; |
201
|
|
|
} |
202
|
|
|
|
203
|
28 |
|
private function getResponseContent(ResponseInterface $response): array |
204
|
|
|
{ |
205
|
28 |
|
return guzzle_json_decode((string) $response->getBody(), true); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|