1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Codenixsv\ApiClient; |
6
|
|
|
|
7
|
|
|
use Codenixsv\ApiClient\Configurator\RequestConfiguratorInterface; |
8
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
9
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
10
|
|
|
use Http\Discovery\Psr18ClientDiscovery; |
11
|
|
|
use Psr\Http\Client\ClientInterface; |
12
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
13
|
|
|
use Psr\Http\Message\RequestInterface; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
16
|
|
|
use Psr\Http\Message\StreamInterface; |
17
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class BaseClient |
21
|
|
|
* @package Codenixsv\ApiClient |
22
|
|
|
*/ |
23
|
|
|
class BaseClient implements BaseClientInterface |
24
|
|
|
{ |
25
|
|
|
/** @var ClientInterface */ |
26
|
|
|
private $httpClient; |
27
|
|
|
/** @var RequestFactoryInterface */ |
28
|
|
|
private $requestFactory; |
29
|
|
|
/** @var UriFactoryInterface */ |
30
|
|
|
private $uriFactory; |
31
|
|
|
/** @var StreamFactoryInterface */ |
32
|
|
|
private $streamFactory; |
33
|
|
|
/** @var string */ |
34
|
|
|
private $baseUri = ''; |
35
|
|
|
/** @var RequestConfiguratorInterface[] */ |
36
|
|
|
private $requestConfigurators = []; |
37
|
|
|
|
38
|
15 |
|
public function __construct( |
39
|
|
|
?ClientInterface $httpClient = null, |
40
|
|
|
?RequestFactoryInterface $requestFactory = null, |
41
|
|
|
?UriFactoryInterface $uriFactory = null, |
42
|
|
|
?StreamFactoryInterface $streamFactory = null |
43
|
|
|
) { |
44
|
15 |
|
$this->httpClient = $httpClient ?: Psr18ClientDiscovery::find(); |
45
|
15 |
|
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory(); |
46
|
15 |
|
$this->uriFactory = $uriFactory ?: Psr17FactoryDiscovery::findUrlFactory(); |
47
|
15 |
|
$this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory(); |
48
|
15 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return ClientInterface |
52
|
|
|
*/ |
53
|
10 |
|
public function getHttpClient(): ClientInterface |
54
|
|
|
{ |
55
|
10 |
|
return $this->httpClient; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
12 |
|
public function getBaseUri(): string |
62
|
|
|
{ |
63
|
12 |
|
return $this->baseUri; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $baseUri |
68
|
|
|
*/ |
69
|
1 |
|
public function setBaseUri(string $baseUri): void |
70
|
|
|
{ |
71
|
1 |
|
$this->baseUri = $baseUri; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param RequestConfiguratorInterface $configurator |
76
|
|
|
*/ |
77
|
3 |
|
public function addRequestConfigurator(RequestConfiguratorInterface $configurator): void |
78
|
|
|
{ |
79
|
3 |
|
$this->requestConfigurators[] = $configurator; |
80
|
3 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
2 |
|
public function clearRequestConfigurators(): void |
86
|
|
|
{ |
87
|
2 |
|
$this->requestConfigurators = []; |
88
|
2 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return RequestConfiguratorInterface[] |
92
|
|
|
*/ |
93
|
2 |
|
public function getRequestConfigurators(): array |
94
|
|
|
{ |
95
|
2 |
|
return $this->requestConfigurators; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $method |
100
|
|
|
* @param string $endpoint |
101
|
|
|
* @param null|string|StreamInterface|resource $body |
102
|
|
|
* @param array $headers |
103
|
|
|
* @return ResponseInterface |
104
|
|
|
* @throws ClientExceptionInterface |
105
|
|
|
*/ |
106
|
10 |
|
public function request(string $method, string $endpoint, $body = null, array $headers = []): ResponseInterface |
107
|
|
|
{ |
108
|
10 |
|
$request = $this->buildRequest($method, $endpoint, $body, $headers); |
109
|
10 |
|
return $this->httpClient->sendRequest($request); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param RequestInterface $request |
114
|
|
|
* @return RequestInterface |
115
|
|
|
*/ |
116
|
10 |
|
private function configureRequest(RequestInterface $request): RequestInterface |
117
|
|
|
{ |
118
|
10 |
|
foreach ($this->requestConfigurators as $configurator) { |
119
|
1 |
|
$request = $configurator->configure($request); |
120
|
|
|
} |
121
|
10 |
|
return $request; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $endpoint |
126
|
|
|
* @param array $headers |
127
|
|
|
* @return ResponseInterface |
128
|
|
|
* @throws ClientExceptionInterface |
129
|
|
|
*/ |
130
|
1 |
|
public function get(string $endpoint, array $headers = []): ResponseInterface |
131
|
|
|
{ |
132
|
1 |
|
return $this->request('GET', $endpoint, null, $headers); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $endpoint |
137
|
|
|
* @param $body |
138
|
|
|
* @param array $headers |
139
|
|
|
* @return ResponseInterface |
140
|
|
|
* @throws ClientExceptionInterface |
141
|
|
|
*/ |
142
|
4 |
|
public function post(string $endpoint, $body, array $headers = []): ResponseInterface |
143
|
|
|
{ |
144
|
4 |
|
return $this->request('POST', $endpoint, $body, $headers); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $endpoint |
149
|
|
|
* @param $body |
150
|
|
|
* @param array $headers |
151
|
|
|
* @return ResponseInterface |
152
|
|
|
* @throws ClientExceptionInterface |
153
|
|
|
*/ |
154
|
1 |
|
public function patch(string $endpoint, $body, array $headers = []): ResponseInterface |
155
|
|
|
{ |
156
|
1 |
|
return $this->request('PATCH', $endpoint, $body, $headers); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $endpoint |
161
|
|
|
* @param $body |
162
|
|
|
* @param array $headers |
163
|
|
|
* @return ResponseInterface |
164
|
|
|
* @throws ClientExceptionInterface |
165
|
|
|
*/ |
166
|
1 |
|
public function put(string $endpoint, $body, array $headers = []): ResponseInterface |
167
|
|
|
{ |
168
|
1 |
|
return $this->request('PUT', $endpoint, $body, $headers); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $endpoint |
173
|
|
|
* @param array $headers |
174
|
|
|
* @return ResponseInterface |
175
|
|
|
* @throws ClientExceptionInterface |
176
|
|
|
*/ |
177
|
1 |
|
public function delete(string $endpoint, array $headers = []): ResponseInterface |
178
|
|
|
{ |
179
|
1 |
|
return $this->request('DELETE', $endpoint, null, $headers); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $method |
184
|
|
|
* @param string $endpoint |
185
|
|
|
* @param $body |
186
|
|
|
* @param array $headers |
187
|
|
|
* @return RequestInterface |
188
|
|
|
*/ |
189
|
10 |
|
protected function buildRequest( |
190
|
|
|
string $method, |
191
|
|
|
string $endpoint, |
192
|
|
|
$body = null, |
193
|
|
|
array $headers = [] |
194
|
|
|
): RequestInterface { |
195
|
10 |
|
$uri = $this->uriFactory->createUri($this->getBaseUri() . $endpoint); |
196
|
|
|
|
197
|
10 |
|
$request = $this->requestFactory->createRequest($method, $uri); |
198
|
10 |
|
$request = $this->appendBodyToRequest($request, $body); |
199
|
10 |
|
$request = $this->appendHeadersToRequest($request, $headers); |
200
|
10 |
|
$request = $this->configureRequest($request); |
201
|
|
|
|
202
|
10 |
|
return $request; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param RequestInterface $request |
207
|
|
|
* @param array $headers |
208
|
|
|
* @return RequestInterface |
209
|
|
|
*/ |
210
|
10 |
|
protected function appendHeadersToRequest(RequestInterface $request, array $headers) |
211
|
|
|
{ |
212
|
10 |
|
foreach ($headers as $name => $value) { |
213
|
5 |
|
$request = $request->withHeader($name, $value); |
214
|
|
|
} |
215
|
10 |
|
return $request; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param RequestInterface $request |
220
|
|
|
* @param $body |
221
|
|
|
* @return RequestInterface |
222
|
|
|
*/ |
223
|
10 |
|
protected function appendBodyToRequest(RequestInterface $request, $body) |
224
|
|
|
{ |
225
|
10 |
|
if ($body !== null) { |
226
|
6 |
|
if (is_resource($body)) { |
227
|
1 |
|
$body = $this->streamFactory->createStreamFromResource($body); |
228
|
|
|
} |
229
|
6 |
|
if (!($body instanceof StreamInterface)) { |
230
|
4 |
|
$body = $this->streamFactory->createStream((string) $body); |
231
|
|
|
} |
232
|
6 |
|
$request = $request->withBody($body); |
233
|
|
|
} |
234
|
|
|
|
235
|
10 |
|
return $request; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|