-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathTransport.php
More file actions
executable file
·260 lines (222 loc) · 7.28 KB
/
Copy pathTransport.php
File metadata and controls
executable file
·260 lines (222 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace Cristal\ApiWrapper\Transports;
use Closure;
use Cristal\ApiWrapper\Exceptions\ApiEntityNotFoundException;
use Cristal\ApiWrapper\Exceptions\ApiException;
use Cristal\ApiWrapper\Exceptions\Handlers\AbstractErrorHandler;
use Cristal\ApiWrapper\Exceptions\Handlers\BadRequestErrorHandler;
use Cristal\ApiWrapper\Exceptions\Handlers\ForbiddenErrorHandler;
use Cristal\ApiWrapper\Exceptions\Handlers\NetworkErrorHandler;
use Cristal\ApiWrapper\Exceptions\Handlers\NotFoundErrorHandler;
use Cristal\ApiWrapper\Exceptions\Handlers\UnauthorizedErrorHandler;
use Cristal\ApiWrapper\MultipartParam;
use Curl\Curl as CurlClient;
use CURLFile;
/**
* Class Transport
* @package Cristal\ApiWrapper\Transports
*/
class Transport implements TransportInterface
{
const HTTP_NETWORK_ERROR_CODE = 0;
const HTTP_UNAUTHORIZED = 401;
const HTTP_FORBIDDEN = 403;
const HTTP_NOT_FOUND_ERROR_CODE = 404;
const HTTP_BAD_REQUEST = 400;
const HTTP_UNPROCESSABLE_ENTITY = 422;
const JSON_MIME_TYPE = 'application/json';
/**
* @var null|string
*/
protected $entrypoint;
/**
* @var CurlClient
*/
protected $client;
/**
* @var AbstractErrorHandler[]
*/
protected $errorHandlers = [];
/**
* Transport constructor.
*
* @param string $entrypoint
* @param CurlClient $client
*/
public function __construct(string $entrypoint, CurlClient $client)
{
$this->client = $client;
$this->entrypoint = rtrim($entrypoint, '/').'/';
$this->setErrorHandler(self::HTTP_NETWORK_ERROR_CODE, new NetworkErrorHandler($this));
$this->setErrorHandler(self::HTTP_UNAUTHORIZED, new UnauthorizedErrorHandler($this));
$this->setErrorHandler(self::HTTP_FORBIDDEN, new ForbiddenErrorHandler($this));
$this->setErrorHandler(self::HTTP_NOT_FOUND_ERROR_CODE, new NotFoundErrorHandler($this));
$this->setErrorHandler(self::HTTP_BAD_REQUEST, new BadRequestErrorHandler($this));
$this->setErrorHandler(self::HTTP_UNPROCESSABLE_ENTITY, new BadRequestErrorHandler($this));
}
/**
* Define or remove an error handler for the request.
* Pass null to remove an existing handler.
*
* @param int $code
* @param AbstractErrorHandler|null $handler
*
* @return $this
*/
public function setErrorHandler(int $code, ?AbstractErrorHandler $handler)
{
$this->errorHandlers[$code] = $handler;
if (is_null($handler)) {
unset($this->errorHandlers[$code]);
}
return $this;
}
/**
* Get Curl client.
*
* @return CurlClient
*/
public function getClient()
{
return $this->client;
}
/**
* {@inheritdoc}
*/
public function request($endpoint, array $data = [], $method = 'get')
{
$rawResponse = $this->rawRequest($endpoint, $data, $method);
$httpStatusCode = $this->getClient()->httpStatusCode;
$response = json_decode((string) $rawResponse, true);
if ($httpStatusCode >= 200 && $httpStatusCode <= 299) {
return $response;
}
$exception = new ApiException(
$response,
sprintf(
'The request ended on a %s code : %s',
$httpStatusCode,
$this->arrayGet($response ?? [], $this->getErrorKey()) ?? $rawResponse ?? 'Unknown error message'
),
$httpStatusCode
);
if ($handler = $this->errorHandlers[$httpStatusCode] ?? false) {
return $handler->handle($exception, compact('endpoint', 'data', 'method'));
}
throw $exception;
}
public function getErrorKey(): string
{
return 'message';
}
/**
* {@inheritdoc}
*/
public function rawRequest($endpoint, array $data = [], $method = 'get')
{
$method = strtolower($method);
switch ($method) {
case 'get':
$url = $this->getUrl($endpoint, $data);
$this->getClient()->get($url);
break;
case 'post':
$this->getClient()->post(
$this->getUrl($endpoint),
$this->encodeBody($data),
(version_compare(PHP_VERSION, '5.5.11') < 0) || defined('HHVM_VERSION')
);
break;
case 'put':
$url = $this->getUrl($endpoint);
$this->getClient()->put($url, $this->encodeBody($data));
break;
case 'patch':
$url = $this->getUrl($endpoint);
$this->getClient()->patch($url, $this->encodeBody($data));
break;
case 'delete':
$url = $this->getUrl($endpoint);
$this->getClient()->delete($url, $data);
break;
}
return $this->getClient()->rawResponse;
}
/**
* Build URL with stored entrypoint, the endpoint and data queries.
*
* @param string $endpoint
* @param array $data
*
* @return string
*/
protected function getUrl(string $endpoint, array $data = [])
{
$url = $this->getEntrypoint() . ltrim($endpoint, '/');
return $url . $this->appendData($data);
}
/**
* Get entrypoint URL.
*
* @return string
*/
public function getEntrypoint()
{
return $this->entrypoint;
}
/**
* Add request parameters to the URI.
*
* @param array $data
*
* @return string|null
*/
protected function appendData(array $data = [])
{
if (!count($data)) {
return null;
}
$data = array_map(fn($item) => is_null($item) ? '' : $item, $data);
return '?' . http_build_query($data);
}
/**
* If a file is sent, use multipart header and raw data.
*
* @param interable $data
*
* @return false|string
*/
public function encodeBody($data)
{
foreach ($data as $value) {
if ($value instanceof CURLFile) {
$this->getClient()->setHeader('Content-Type', 'multipart/form-data');
return $data;
}
if ($value instanceof MultipartParam) {
$delimiter = '----WebKitFormBoundary'.uniqid('', true);
$this->getClient()->setHeader('Content-Type', 'multipart/form-data; boundary=' . $delimiter);
return implode('', array_map(function ($param, $name) use ($delimiter) {
if (!$param instanceof MultipartParam) {
$param = new MultipartParam($param);
}
return $param->render($name, $delimiter);
}, $data, array_keys($data))).'--'.$delimiter.'--';
}
}
$this->getClient()->setHeader('Content-Type', static::JSON_MIME_TYPE);
return json_encode($data);
}
public function getResponseHeaders(): array
{
return iterator_to_array($this->getClient()->getResponseHeaders());
}
protected function arrayGet(array $array, string $key)
{
$exploded = explode('.', $key, 2);
if (!isset($exploded[1])) {
return $array[$key] ?? null;
}
return $this->arrayGet($array[$exploded[0]], $exploded[1]);
}
}