-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathHttpClient.php
More file actions
104 lines (89 loc) · 3.64 KB
/
HttpClient.php
File metadata and controls
104 lines (89 loc) · 3.64 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
<?php
namespace Akeneo\Pim\ApiClient\Client;
use GuzzleHttp\Promise\PromiseInterface;
use Http\Promise\Promise;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
/**
* Http client to send a request without any authentication.
*
* @author Alexandre Hocquard <alexandre.hocquard@akeneo.com>
* @copyright 2017 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class HttpClient implements HttpClientInterface
{
public const HTTP_OK = 200;
public const HTTP_CREATED = 201;
public const HTTP_NO_CONTENT = 204;
public const HTTP_BAD_REQUEST = 400;
public const HTTP_UNAUTHORIZED = 401;
public const HTTP_FORBIDDEN = 403;
public const HTTP_NOT_FOUND = 404;
public const HTTP_METHOD_NOT_ALLOWED = 405;
public const HTTP_NOT_ACCEPTABLE = 406;
public const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
public const HTTP_UNPROCESSABLE_ENTITY = 422;
public const HTTP_TOO_MANY_REQUESTS = 429;
protected HttpExceptionHandler $httpExceptionHandler;
public function __construct(
protected ClientInterface|\Psr\Http\Client\ClientInterface $httpClient,
protected RequestFactoryInterface $requestFactory,
private StreamFactoryInterface $streamFactory,
private Options $options
) {
$this->httpExceptionHandler = new HttpExceptionHandler();
}
private function prepareRequest(string $httpMethod, $uri, array $headers = [], $body = null): RequestInterface
{
$request = $this->requestFactory->createRequest($httpMethod, $uri);
if (null !== $body && is_string($body)) {
$request = $request->withBody($this->streamFactory->createStream($body));
}
if (null !== $body && $body instanceof StreamInterface) {
$request = $request->withBody($body);
}
foreach ($headers as $header => $content) {
$request = $request->withHeader($header, $content);
}
if ($this->options->hasHeaders()) {
foreach ($this->options->getHeaders() as $header => $content) {
$request = $request->withHeader($header, $content);
}
}
return $request;
}
/**
* {@inheritdoc}
*/
public function sendRequest(string $httpMethod, $uri, array $headers = [], $body = null): ResponseInterface
{
$request = $this->prepareRequest($httpMethod, $uri, $headers, $body);
$response = $this->retry($this->httpClient->sendRequest($request), $request);
return $this->httpExceptionHandler->transformResponseToException($request, $response);
}
public function sendAsync(
string $httpMethod,
$uri,
array $headers = [],
$body = null
): PromiseInterface|Promise {
$request = $this->prepareRequest($httpMethod, $uri, $headers, $body);
return $this->httpClient->sendAsyncRequest($request);
}
private function retry(ResponseInterface $response, RequestInterface $request, int $retryDelay = 2): ResponseInterface
{
if ($this->options->canRetry() && HttpClient::HTTP_TOO_MANY_REQUESTS === $response->getStatusCode()) {
$retry = 0;
while ($this->options->getMaxRetry() > $retry && HttpClient::HTTP_TOO_MANY_REQUESTS === $response->getStatusCode()) {
usleep($retryDelay * 1000);
$response = $this->httpClient->sendRequest($request);
$retry++;
}
}
return $response;
}
}