-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathHttpClientWrapper.php
390 lines (357 loc) · 14.6 KB
/
HttpClientWrapper.php
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
// Copyright 2022 DeepL SE (https://www.deepl.com)
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
namespace DeepL;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18Client;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\RequestExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Log\LoggerInterface;
use Psr\Http\Client\ClientInterface;
/**
* Internal class implementing HTTP requests.
* @private
*/
class HttpClientWrapper
{
private $serverUrl;
private $headers;
private $maxRetries;
private $minTimeout;
private $logger;
private $proxy;
private $customHttpClient;
private $requestFactory;
/**
* PSR-18 client that is only used to construct the HTTP request, not to send it.
*/
private $streamClient;
private $streamFactory;
/**
* @var resource cURL handle, or null if using a custom HTTP client.
* @see HttpClientWrapper::__construct
*/
private $curlHandle;
public const OPTION_FILE = 'file';
public const OPTION_HEADERS = 'headers';
public const OPTION_PARAMS = 'params';
public const OPTION_OUTFILE = 'outfile';
public function __construct(
string $serverUrl,
array $headers,
float $timeout,
int $maxRetries,
?LoggerInterface $logger,
?string $proxy,
?ClientInterface $customHttpClient = null
) {
$this->serverUrl = $serverUrl;
$this->maxRetries = $maxRetries;
$this->minTimeout = $timeout;
$this->headers = $headers;
$this->logger = $logger;
$this->proxy = $proxy;
$this->customHttpClient = $customHttpClient;
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
$this->streamClient = new Psr18Client();
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
$this->curlHandle = $customHttpClient === null ? \curl_init() : null;
}
public function __destruct()
{
if ($this->customHttpClient === null) {
\curl_close($this->curlHandle);
}
}
/**
* Makes API request retrying if necessary, and returns (as Promise) response.
* @param string $method HTTP method, for example 'GET'.
* @param string $url Path to endpoint, excluding base server URL.
* @param array|null $options Array of options, possible arguments are given by OPTIONS_ constants.
* @return array Status code and content.
* @throws DeepLException
*/
public function sendRequestWithBackoff(string $method, string $url, ?array $options = []): array
{
$url = $this->serverUrl . $url;
$headers = array_replace(
$this->headers,
$options[self::OPTION_HEADERS] ?? []
);
$file = $options[self::OPTION_FILE] ?? null;
$params = $options[self::OPTION_PARAMS] ?? [];
$this->logInfo("Request to DeepL API $method $url");
$this->logDebug('Request details: ' . json_encode($params));
$backoff = new BackoffTimer();
$response = null;
$exception = null;
while ($backoff->getNumRetries() <= $this->maxRetries) {
$outFile = isset($options[self::OPTION_OUTFILE]) ? fopen($options[self::OPTION_OUTFILE], 'w') : null;
$timeout = max($this->minTimeout, $backoff->getTimeUntilDeadline());
$response = null;
$exception = null;
try {
$response = $this->sendRequest($method, $url, $timeout, $headers, $params, $file, $outFile);
} catch (ConnectionException $e) {
$exception = $e;
}
if ($outFile) {
fclose($outFile);
}
if (!$this->shouldRetry($response, $exception) || $backoff->getNumRetries() + 1 >= $this->maxRetries) {
break;
}
if ($exception !== null) {
$this->logDebug("Encountered a retryable-error: {$exception->getMessage()}");
}
$this->logInfo('Starting retry ' . ($backoff->getNumRetries() + 1) .
" for request $method $url after sleeping for {$backoff->getTimeUntilDeadline()} seconds.");
$backoff->sleepUntilDeadline();
}
if ($exception !== null) {
throw $exception;
} else {
list($statusCode, $content) = $response;
$this->logInfo("DeepL API response $method $url $statusCode");
$this->logDebug("Response details: $content");
return $response;
}
}
/**
* Sends a HTTP request. Note that in the case of a custom HTTP client, some of these options are
* ignored in favor of whatever is set in the client (e.g. timeouts and proxy). If we fall back to cURL,
* those options are respected.
* @param string $method HTTP method to use.
* @param string $url Absolute URL to query.
* @param float $timeout Time to wait before triggering timeout, in seconds.
* @param array $headers Array of headers to include in request.
* @param array $params Array of parameters to include in body.
* @param string|null $filePath If not null, path to file to upload with request.
* @param resource|null $outFile If not null, file to write output to.
* @return array Array where the first element is the HTTP status code and the second element is the response body.
* @throws ConnectionException
*/
private function sendRequest(
string $method,
string $url,
float $timeout,
array $headers,
array $params,
?string $filePath,
$outFile
): array {
if ($this->customHttpClient !== null) {
return $this->sendCustomHttpRequest($method, $url, $headers, $params, $filePath, $outFile);
} else {
return $this->sendCurlRequest($method, $url, $timeout, $headers, $params, $filePath, $outFile);
}
}
/**
* Creates a PSR-7 compliant HTTP request with the given arguments.
* @param string $method HTTP method to use
* @param string $uri The URI for the request
* @param array $headers Array of headers for the request
* @param StreamInterface $body body to be used for the request.
* @return RequestInterface HTTP request object
*/
private function createHttpRequest(string $method, string $url, array $headers, StreamInterface $body)
{
$request = $this->requestFactory->createRequest($method, $url);
foreach ($headers as $header_key => $header_val) {
$request = $request->withHeader($header_key, $header_val);
}
$request = $request->withBody($body);
return $request;
}
/**
* Sends a HTTP request using the custom HTTP client.
* @param string $method HTTP method to use.
* @param string $url Absolute URL to query.
* @param array $headers Array of headers to include in request.
* @param array $params Array of parameters to include in body.
* @param string|null $filePath If not null, path to file to upload with request.
* @param resource|null $outFile If not null, file to write output to.
* @return array Array where the first element is the HTTP status code and the second element is the response body.
* @throws ConnectionException
*/
private function sendCustomHttpRequest(
string $method,
string $url,
array $headers,
array $params,
?string $filePath,
$outFile
): array {
$body = null;
if ($filePath !== null) {
$builder = new MultipartStreamBuilder($this->streamClient);
$builder->addResource('file', fopen($filePath, 'r'));
foreach ($params as $param_name => $value) {
$builder->addResource($param_name, $value);
}
$body = $builder->build();
$boundary = $builder->getBoundary();
$headers['Content-Type'] = "multipart/form-data; boundary=\"$boundary\"";
} elseif (count($params) > 0) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
$body = $this->streamFactory->createStream(
$this->urlEncodeWithRepeatedParams($params)
);
} else {
$body = $this->streamFactory->createStream('');
}
$request = $this->createHttpRequest($method, $url, $headers, $body);
try {
$response = $this->customHttpClient->sendRequest($request);
$response_data = (string) $response->getBody();
if ($outFile) {
fwrite($outFile, $response_data);
}
return [$response->getStatusCode(), $response_data];
} catch (RequestExceptionInterface $e) {
throw new ConnectionException($e->getMessage(), $e->getCode(), null, false);
} catch (ClientExceptionInterface $e) {
throw new ConnectionException($e->getMessage(), $e->getCode(), null, true);
}
}
/**
* Sends a HTTP request using cURL
* @param string $method HTTP method to use.
* @param string $url Absolute URL to query.
* @param float $timeout Time to wait before triggering timeout, in seconds.
* @param array $headers Array of headers to include in request.
* @param array $params Array of parameters to include in body.
* @param string|null $filePath If not null, path to file to upload with request.
* @param resource|null $outFile If not null, file to write output to.
* @return array Array where the first element is the HTTP status code and the second element is the response body.
* @throws ConnectionException
*/
private function sendCurlRequest(
string $method,
string $url,
float $timeout,
array $headers,
array $params,
?string $filePath,
$outFile
): array {
$curlOptions = [];
$curlOptions[\CURLOPT_HEADER] = false;
switch ($method) {
case "POST":
$curlOptions[\CURLOPT_POST] = true;
break;
case "GET":
$curlOptions[\CURLOPT_HTTPGET] = true;
break;
default:
$curlOptions[\CURLOPT_CUSTOMREQUEST] = $method;
break;
}
$curlOptions[\CURLOPT_URL] = $url;
$curlOptions[\CURLOPT_CONNECTTIMEOUT] = $timeout;
$curlOptions[\CURLOPT_TIMEOUT_MS] = $timeout * 1000;
if ($this->proxy !== null) {
$curlOptions[\CURLOPT_PROXY] = $this->proxy;
}
// Convert headers from an associative array to an array of "key: value" elements
$curlOptions[\CURLOPT_HTTPHEADER] = \array_map(function (string $key, string $value): string {
return "$key: $value";
}, array_keys($headers), array_values($headers));
if ($filePath !== null) {
// If a file is to be uploaded, add it to the list of body parameters
$params['file'] = \curl_file_create($filePath);
$curlOptions[\CURLOPT_POSTFIELDS] = $params;
} elseif (count($params) > 0) {
// If there are repeated parameters, passing the parameters directly to cURL will index the repeated
// parameters which is not what we need, so instead we encode the parameters without indexes.
// This case only occurs if no file is uploaded.
$curlOptions[\CURLOPT_POSTFIELDS] = $this->urlEncodeWithRepeatedParams($params);
}
if ($outFile) {
// Stream response content to specified file
$curlOptions[\CURLOPT_FILE] = $outFile;
} else {
// Return response content as function result
$curlOptions[\CURLOPT_RETURNTRANSFER] = true;
}
\curl_reset($this->curlHandle);
// The next 3 curl calls are unqualified so that we can mock them, see
// https://github.com/php-mock/php-mock-phpunit#restrictions
curl_setopt_array($this->curlHandle, $curlOptions);
$result = curl_exec($this->curlHandle);
if ($result !== false) {
$statusCode = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
return [$statusCode, $result];
} else {
$errorMessage = \curl_error($this->curlHandle);
$errorCode = \curl_errno($this->curlHandle);
switch ($errorCode) {
case \CURLE_UNSUPPORTED_PROTOCOL:
case \CURLE_URL_MALFORMAT:
case \CURLE_URL_MALFORMAT_USER:
$shouldRetry = false;
$errorMessage = "Invalid server URL. $errorMessage";
break;
case \CURLE_OPERATION_TIMEOUTED:
case \CURLE_COULDNT_CONNECT:
case \CURLE_GOT_NOTHING:
$shouldRetry = true;
break;
default:
$shouldRetry = false;
break;
}
throw new ConnectionException($errorMessage, $errorCode, null, $shouldRetry);
}
}
private function shouldRetry(?array $response, ?ConnectionException $exception): bool
{
if ($exception !== null) {
return $exception->shouldRetry;
}
list($statusCode, ) = $response;
// Retry on Too-Many-Requests error and internal errors
return $statusCode === 429 || $statusCode >= 500;
}
public function logDebug(string $message): void
{
if ($this->logger) {
$this->logger->debug($message);
}
}
public function logInfo(string $message): void
{
if ($this->logger) {
$this->logger->info($message);
}
}
private static function urlEncodeWithRepeatedParams(?array $params): string
{
$params = $params ?? [];
$fields = [];
foreach ($params as $key => $value) {
$name = \urlencode($key);
if (is_array($value)) {
$fields[] = implode(
'&',
array_map(
function (string $textElement) use ($name): string {
return $name . '=' . \urlencode($textElement);
},
$value
)
);
} elseif (is_null($value)) {
// Parameters with null value are skipped
} else {
$fields[] = $name . '=' . \urlencode($value);
}
}
return implode("&", $fields);
}
}