|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EasyHttp\GuzzleLayer; |
|
4
|
|
|
|
|
5
|
|
|
use EasyHttp\LayerContracts\Contracts\HttpClientAdapter; |
|
6
|
|
|
use EasyHttp\LayerContracts\Contracts\HttpClientRequest; |
|
7
|
|
|
use EasyHttp\LayerContracts\Contracts\HttpClientResponse; |
|
8
|
|
|
use EasyHttp\LayerContracts\Exceptions\HttpClientException; |
|
9
|
|
|
use EasyHttp\LayerContracts\Exceptions\HttpConnectionException; |
|
10
|
|
|
use GuzzleHttp\ClientInterface; |
|
11
|
|
|
use GuzzleHttp\Exception\ConnectException; |
|
12
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
13
|
|
|
use GuzzleHttp\Exception\TransferException; |
|
14
|
|
|
|
|
15
|
|
|
class GuzzleAdapter implements HttpClientAdapter |
|
16
|
|
|
{ |
|
17
|
|
|
protected ClientInterface $client; |
|
18
|
|
|
|
|
19
|
21 |
|
public function __construct(ClientInterface $client) |
|
20
|
|
|
{ |
|
21
|
21 |
|
$this->client = $client; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
21 |
|
public function request(HttpClientRequest $request): HttpClientResponse |
|
25
|
|
|
{ |
|
26
|
|
|
try { |
|
27
|
21 |
|
$response = $this->client->request( |
|
28
|
21 |
|
$request->getMethod(), |
|
29
|
21 |
|
$request->getUri(), |
|
30
|
21 |
|
$this->buildOptions($request) |
|
31
|
|
|
); |
|
32
|
9 |
|
} catch (ConnectException $exception) { |
|
33
|
1 |
|
throw new HttpConnectionException($exception->getMessage(), (int) $exception->getCode(), $exception); |
|
34
|
8 |
|
} catch (RequestException $exception) { |
|
35
|
7 |
|
if (! $exception->hasResponse()) { |
|
36
|
5 |
|
throw new HttpClientException($exception->getMessage(), (int) $exception->getCode(), $exception); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
$response = $exception->getResponse(); |
|
40
|
1 |
|
} catch (TransferException $exception) { |
|
41
|
1 |
|
throw new HttpClientException($exception->getMessage(), (int) $exception->getCode(), $exception); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
14 |
|
return new GuzzleResponse($response); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
21 |
|
private function buildOptions(HttpClientRequest $request): array |
|
48
|
|
|
{ |
|
49
|
|
|
$options = [ |
|
50
|
21 |
|
'timeout' => $request->getTimeout(), |
|
51
|
21 |
|
'verify' => $request->isSSL() |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
$this |
|
55
|
21 |
|
->setHeaders($request, $options) |
|
56
|
21 |
|
->setJson($request, $options) |
|
57
|
21 |
|
->setQuery($request, $options) |
|
58
|
21 |
|
->setSecurityContext($request, $options) |
|
59
|
21 |
|
->setBasicAuth($request, $options); |
|
60
|
|
|
|
|
61
|
21 |
|
return $options; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
21 |
|
private function setHeaders(HttpClientRequest $request, &$options): self |
|
65
|
|
|
{ |
|
66
|
21 |
|
if ($request->hasHeaders()) { |
|
67
|
2 |
|
$options['headers'] = $request->getHeaders(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
21 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
21 |
|
private function setJson(HttpClientRequest $request, &$options): self |
|
74
|
|
|
{ |
|
75
|
21 |
|
if ($request->hasJson()) { |
|
76
|
3 |
|
$options['json'] = $request->getJson(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
21 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
21 |
|
private function setQuery(HttpClientRequest $request, &$options): self |
|
83
|
|
|
{ |
|
84
|
21 |
|
if ($request->hasQuery()) { |
|
85
|
3 |
|
$options['query'] = $request->getQuery(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
21 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
21 |
|
private function setSecurityContext(HttpClientRequest $request, &$options): self |
|
92
|
|
|
{ |
|
93
|
21 |
|
if ($request->hasSecurityContext() && $request->getSecurityContext()->hasCertificate()) { |
|
94
|
3 |
|
$options['cert'] = $request->getSecurityContext()->getCertificate(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
21 |
|
if ($request->hasSecurityContext() && $request->getSecurityContext()->hasPrivateKey()) { |
|
98
|
2 |
|
$options['ssl_key'] = $request->getSecurityContext()->getPrivateKey(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
21 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
21 |
|
private function setBasicAuth(HttpClientRequest $request, &$options): self |
|
105
|
|
|
{ |
|
106
|
21 |
|
if (count($request->getBasicAuth())) { |
|
107
|
2 |
|
$options['auth'] = $request->getBasicAuth(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
21 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|