1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Abstraction over Http client implementations. |
5
|
|
|
* |
6
|
|
|
* @author Maksim Masiukevich <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @license https://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types = 0); |
12
|
|
|
|
13
|
|
|
namespace ServiceBus\HttpClient\Artax; |
14
|
|
|
|
15
|
|
|
use Amp\CancelledException; |
16
|
|
|
use Amp\Dns\DnsException; |
17
|
|
|
use Amp\Dns\NoRecordException; |
18
|
|
|
use Amp\Http\Client\ParseException; |
19
|
|
|
use Amp\Http\Client\Request; |
20
|
|
|
use Amp\Http\Client\SocketException; |
21
|
|
|
use Amp\Http\Client\TimeoutException; |
22
|
|
|
use Amp\Promise; |
23
|
|
|
use GuzzleHttp\Psr7\Response; |
24
|
|
|
use Psr\Log\LoggerInterface; |
25
|
|
|
use ServiceBus\HttpClient\Exception as HttpClientExceptions; |
26
|
|
|
use function ServiceBus\Common\throwableMessage; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Download file. |
30
|
|
|
* |
31
|
|
|
* @api |
32
|
|
|
* |
33
|
|
|
* @return Promise<string> |
34
|
|
|
* |
35
|
|
|
* @throws \ServiceBus\HttpClient\Exception\HttpClientException |
36
|
|
|
* @throws \ServiceBus\HttpClient\Exception\ConnectionFailed |
37
|
|
|
* @throws \ServiceBus\HttpClient\Exception\DnsResolveFailed |
38
|
|
|
* @throws \ServiceBus\HttpClient\Exception\IncorrectParameters |
39
|
|
|
* @throws \ServiceBus\HttpClient\Exception\RequestTimeoutReached |
40
|
|
|
*/ |
41
|
|
|
function downloadFile(string $url, string $toDirectory, string $withName): Promise |
42
|
|
|
{ |
43
|
1 |
|
return (new ArtaxHttpClient())->download($url, $toDirectory, $withName); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @internal |
48
|
|
|
*/ |
49
|
3 |
|
function logArtaxRequest(LoggerInterface $logger, Request $request, string $requestId): \Generator |
50
|
|
|
{ |
51
|
3 |
|
$logger->debug( |
52
|
3 |
|
'Request: [{requestMethod}] {requestUri} {requestHeaders}', |
53
|
|
|
[ |
54
|
3 |
|
'requestMethod' => $request->getMethod(), |
55
|
3 |
|
'requestUri' => $request->getUri(), |
56
|
3 |
|
'requestContent' => yield $request->getBody()->createBodyStream()->read(), |
57
|
3 |
|
'requestHeaders' => $request->getHeaders(), |
58
|
3 |
|
'requestId' => $requestId, |
59
|
|
|
] |
60
|
|
|
); |
61
|
3 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @internal |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
function logArtaxResponse(LoggerInterface $logger, Response $response, string $requestId, string $executionTime): void |
69
|
|
|
{ |
70
|
1 |
|
$logger->debug( |
71
|
1 |
|
'Response: {responseHttpCode} {responseContent} {responseHeaders}', |
72
|
|
|
[ |
73
|
1 |
|
'responseHttpCode' => $response->getStatusCode(), |
74
|
1 |
|
'responseContent' => (string) $response->getBody(), |
75
|
1 |
|
'responseHeaders' => $response->getHeaders(), |
76
|
1 |
|
'requestId' => $requestId, |
77
|
1 |
|
'executionTime' => \sprintf('%.2f', $executionTime) |
78
|
|
|
] |
79
|
|
|
); |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @internal |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
function logArtaxThrowable(LoggerInterface $logger, \Throwable $throwable, string $requestId, string $executionTime): void |
88
|
|
|
{ |
89
|
2 |
|
$logger->error( |
90
|
2 |
|
'During the execution of the request with identifier "{requestId}" an exception was caught: "{throwableMessage}"', |
91
|
|
|
[ |
92
|
2 |
|
'requestId' => $requestId, |
93
|
2 |
|
'throwableMessage' => throwableMessage($throwable), |
|
|
|
|
94
|
2 |
|
'throwablePoint' => \sprintf('%s:%d', $throwable->getFile(), $throwable->getLine()), |
95
|
2 |
|
'executionTime' => \sprintf('%.2f', $executionTime) |
96
|
|
|
] |
97
|
|
|
); |
98
|
2 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @internal |
102
|
|
|
* |
103
|
|
|
* @return \Throwable |
104
|
|
|
*/ |
105
|
|
|
function adaptArtaxThrowable(\Throwable $throwable): \Throwable |
106
|
|
|
{ |
107
|
|
|
/** @psalm-var array<class-string<\Amp\Http\Client\HttpException>, class-string<\Exception>> $mapping */ |
108
|
2 |
|
$mapping = [ |
109
|
|
|
NoRecordException::class => HttpClientExceptions\DnsResolveFailed::class, |
110
|
|
|
DnsException::class => HttpClientExceptions\DnsResolveFailed::class, |
111
|
|
|
SocketException::class => HttpClientExceptions\ConnectionFailed::class, |
112
|
|
|
ParseException::class => HttpClientExceptions\IncorrectParameters::class, |
113
|
|
|
TimeoutException::class => HttpClientExceptions\RequestTimeoutReached::class, |
114
|
|
|
CancelledException::class => HttpClientExceptions\RequestTimeoutReached::class, |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
/** @psalm-var class-string<\Amp\Http\Client\HttpException> $exceptionClass */ |
118
|
2 |
|
$exceptionClass = \get_class($throwable); |
119
|
|
|
|
120
|
2 |
|
if (isset($mapping[$exceptionClass])) |
121
|
|
|
{ |
122
|
|
|
/** @var class-string<\Exception> $exceptionClass */ |
123
|
1 |
|
$exceptionClass = $mapping[$exceptionClass]; |
124
|
|
|
|
125
|
|
|
/** @psalm-suppress UnsafeInstantiation */ |
126
|
1 |
|
return new $exceptionClass($throwable->getMessage(), (int) $throwable->getCode(), $throwable); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return new HttpClientExceptions\HttpClientException( |
130
|
1 |
|
$throwable->getMessage(), |
131
|
1 |
|
(int) $throwable->getCode(), |
132
|
|
|
$throwable |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|