1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the overtrue/http. |
||
5 | * |
||
6 | * (c) overtrue <[email protected]> |
||
7 | * |
||
8 | * This source file is subject to the MIT license that is bundled |
||
9 | * with this source code in the file LICENSE. |
||
10 | */ |
||
11 | |||
12 | namespace Overtrue\Http; |
||
13 | |||
14 | use GuzzleHttp\ClientInterface; |
||
15 | use Overtrue\Http\Traits\CreatesDefaultHttpClient; |
||
16 | use Overtrue\Http\Traits\HasHttpRequests; |
||
17 | use Overtrue\Http\Traits\ResponseCastable; |
||
18 | |||
19 | /** |
||
20 | * Class BaseClient. |
||
21 | */ |
||
22 | class Client |
||
23 | { |
||
24 | use HasHttpRequests { |
||
25 | request as performRequest; |
||
26 | } |
||
27 | use CreatesDefaultHttpClient; |
||
28 | use ResponseCastable; |
||
29 | |||
30 | /** |
||
31 | * @var \Overtrue\Http\Config |
||
32 | */ |
||
33 | protected $config; |
||
34 | |||
35 | /** |
||
36 | * @var |
||
37 | */ |
||
38 | protected $baseUri; |
||
39 | |||
40 | /** |
||
41 | * @return static |
||
42 | */ |
||
43 | public static function create(): self |
||
44 | { |
||
45 | return new static(...func_get_args()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param \Overtrue\Http\Config|array $config |
||
50 | */ |
||
51 | public function __construct($config = []) |
||
52 | { |
||
53 | $this->config = $this->normalizeConfig($config); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param string $uri |
||
58 | * @param array $options |
||
59 | * @param bool $async |
||
60 | * |
||
61 | * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string |
||
62 | */ |
||
63 | public function get(string $uri, array $options = [], $async = false) |
||
64 | { |
||
65 | return $this->request($uri, 'GET', $options, $async); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param string $uri |
||
70 | * @param array $options |
||
71 | * |
||
72 | * @return array|object|\Overtrue\Http\Support\Collection|\Psr\Http\Message\ResponseInterface|string |
||
73 | */ |
||
74 | public function getAsync(string $uri, array $options = []) |
||
75 | { |
||
76 | return $this->get($uri, $options, true); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param string $uri |
||
81 | * @param array $data |
||
82 | * @param array $options |
||
83 | * @param bool $async |
||
84 | * |
||
85 | * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string |
||
86 | */ |
||
87 | public function post(string $uri, array $data = [], array $options = [], $async = false) |
||
88 | { |
||
89 | return $this->request($uri, 'POST', \array_merge($options, ['form_params' => $data]), $async); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $uri |
||
94 | * @param array $data |
||
95 | * @param array $options |
||
96 | * |
||
97 | * @return array|object|\Overtrue\Http\Support\Collection|\Psr\Http\Message\ResponseInterface|string |
||
98 | */ |
||
99 | public function postAsync(string $uri, array $data = [], array $options = []) |
||
100 | { |
||
101 | return $this->post($uri, $data, $options, true); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $uri |
||
106 | * @param array $data |
||
107 | * @param array $options |
||
108 | * @param bool $async |
||
109 | * |
||
110 | * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string |
||
111 | */ |
||
112 | public function patch(string $uri, array $data = [], array $options = [], $async = false) |
||
113 | { |
||
114 | return $this->request($uri, 'PATCH', \array_merge($options, ['form_params' => $data]), $async); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param string $uri |
||
119 | * @param array $data |
||
120 | * @param array $options |
||
121 | * |
||
122 | * @return array|object|\Overtrue\Http\Support\Collection|\Psr\Http\Message\ResponseInterface|string |
||
123 | */ |
||
124 | public function patchAsync(string $uri, array $data = [], array $options = []) |
||
125 | { |
||
126 | return $this->patch($uri, $data, $options, true); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param string $uri |
||
131 | * @param array $data |
||
132 | * @param array $options |
||
133 | * @param bool $async |
||
134 | * |
||
135 | * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string |
||
136 | */ |
||
137 | public function put(string $uri, array $data = [], array $options = [], $async = false) |
||
138 | { |
||
139 | return $this->request($uri, 'PUT', \array_merge($options, ['form_params' => $data]), $async); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param string $uri |
||
144 | * @param array $data |
||
145 | * @param array $options |
||
146 | * |
||
147 | * @return array|object|\Overtrue\Http\Support\Collection|\Psr\Http\Message\ResponseInterface|string |
||
148 | */ |
||
149 | public function putAsync(string $uri, array $data = [], array $options = []) |
||
150 | { |
||
151 | return $this->put($uri, $data, $options, true); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param string $uri |
||
156 | * @param array $options |
||
157 | * @param bool $async |
||
158 | * |
||
159 | * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string |
||
160 | */ |
||
161 | public function options(string $uri, array $options = [], $async = false) |
||
162 | { |
||
163 | return $this->request($uri, 'OPTIONS', $options, $async); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param string $uri |
||
168 | * @param array $options |
||
169 | * |
||
170 | * @return array|object|\Overtrue\Http\Support\Collection|\Psr\Http\Message\ResponseInterface|string |
||
171 | */ |
||
172 | public function optionsAsync(string $uri, array $options = []) |
||
173 | { |
||
174 | return $this->options($uri, $options, true); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param string $uri |
||
179 | * @param array $options |
||
180 | * @param bool $async |
||
181 | * |
||
182 | * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string |
||
183 | */ |
||
184 | public function head(string $uri, array $options = [], $async = false) |
||
185 | { |
||
186 | return $this->request($uri, 'HEAD', $options, $async); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param string $uri |
||
191 | * @param array $options |
||
192 | * |
||
193 | * @return array|object|\Overtrue\Http\Support\Collection|\Psr\Http\Message\ResponseInterface|string |
||
194 | */ |
||
195 | public function headAsync(string $uri, array $options = []) |
||
196 | { |
||
197 | return $this->head($uri, $options, true); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param string $uri |
||
202 | * @param array $options |
||
203 | * @param bool $async |
||
204 | * |
||
205 | * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string |
||
206 | */ |
||
207 | public function delete(string $uri, array $options = [], $async = false) |
||
208 | { |
||
209 | return $this->request($uri, 'DELETE', $options, $async); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param string $uri |
||
214 | * @param array $options |
||
215 | * |
||
216 | * @return array|object|\Overtrue\Http\Support\Collection|\Psr\Http\Message\ResponseInterface|string |
||
217 | */ |
||
218 | public function deleteAsync(string $uri, array $options = []) |
||
219 | { |
||
220 | return $this->delete($uri, $options, true); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @param string $uri |
||
225 | * @param array $files |
||
226 | * @param array $form |
||
227 | * @param array $options |
||
228 | * @param bool $async |
||
229 | * |
||
230 | * @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string |
||
231 | */ |
||
232 | public function upload(string $uri, array $files = [], array $form = [], array $options = [], $async = false) |
||
233 | { |
||
234 | $multipart = []; |
||
235 | |||
236 | foreach ($files as $name => $contents) { |
||
237 | $contents = \is_resource($contents) ? $contents : \fopen($contents, 'r'); |
||
238 | $multipart[] = \compact('name', 'contents'); |
||
239 | } |
||
240 | |||
241 | foreach ($form as $name => $contents) { |
||
242 | $multipart = array_merge($multipart, $this->normalizeMultipartField($name, $contents)); |
||
243 | } |
||
244 | |||
245 | return $this->request($uri, 'POST', \array_merge($options, ['multipart' => $multipart]), $async); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param string $uri |
||
250 | * @param array $files |
||
251 | * @param array $form |
||
252 | * @param array $options |
||
253 | * |
||
254 | * @return array|object|\Overtrue\Http\Support\Collection|\Psr\Http\Message\ResponseInterface|string |
||
255 | */ |
||
256 | public function uploadAsync(string $uri, array $files = [], array $form = [], array $options = []) |
||
257 | { |
||
258 | return $this->upload($uri, $files, $form, $options, true); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param string $uri |
||
263 | * @param string $method |
||
264 | * @param array $options |
||
265 | * @param bool $async |
||
266 | * |
||
267 | * @return \Psr\Http\Message\ResponseInterface|\GuzzleHttp\Promise\PromiseInterface|\Overtrue\Http\Support\Collection|array|object|string |
||
268 | */ |
||
269 | public function request(string $uri, string $method = 'GET', array $options = [], bool $async = false) |
||
270 | { |
||
271 | $result = $this->requestRaw($uri, $method, $options, $async); |
||
272 | |||
273 | $transformer = function ($response) { |
||
274 | return $this->castResponseToType($response, $this->config->getOption('response_type')); |
||
275 | }; |
||
276 | |||
277 | return $async ? $result->then($transformer) : $transformer($result); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @param string $uri |
||
282 | * @param string $method |
||
283 | * @param array $options |
||
284 | * @param bool $async |
||
285 | * |
||
286 | * @return \Psr\Http\Message\ResponseInterface|\GuzzleHttp\Promise\PromiseInterface|\Overtrue\Http\Support\Collection|array|object|string |
||
287 | */ |
||
288 | public function requestRaw(string $uri, string $method = 'GET', array $options = [], bool $async = false) |
||
289 | { |
||
290 | if (property_exists($this, 'baseUri') && !is_null($this->baseUri)) { |
||
291 | $options['base_uri'] = $this->baseUri; |
||
292 | } |
||
293 | |||
294 | return $this->performRequest($uri, $method, $options, $async); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @return \GuzzleHttp\ClientInterface |
||
299 | */ |
||
300 | public function getHttpClient(): ClientInterface |
||
301 | { |
||
302 | if (!$this->httpClient) { |
||
303 | $this->httpClient = $this->createDefaultHttClient($this->config->toArray()); |
||
304 | } |
||
305 | |||
306 | return $this->httpClient; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * @return \Overtrue\Http\Config |
||
311 | */ |
||
312 | public function getConfig(): \Overtrue\Http\Config |
||
313 | { |
||
314 | return $this->config; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * @param \Overtrue\Http\Config $config |
||
319 | * |
||
320 | * @return \Overtrue\Http\Client |
||
321 | */ |
||
322 | public function setConfig(\Overtrue\Http\Config $config): self |
||
323 | { |
||
324 | $this->config = $config; |
||
325 | |||
326 | return $this; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @param string $name |
||
331 | * @param mixed $contents |
||
332 | * |
||
333 | * @return array |
||
334 | */ |
||
335 | public function normalizeMultipartField(string $name, $contents) |
||
336 | { |
||
337 | $field = []; |
||
338 | |||
339 | if (!is_array($contents)) { |
||
340 | return [compact('name', 'contents')]; |
||
341 | } |
||
342 | |||
343 | foreach ($contents as $key => $value) { |
||
344 | $key = sprintf('%s[%s]', $name, $key); |
||
345 | $field = array_merge($field, is_array($value) ? $this->normalizeMultipartField($key, $value) : [['name' => $key, 'contents' => $value]]); |
||
346 | } |
||
347 | |||
348 | return $field; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * @param mixed $config |
||
353 | * |
||
354 | * @return \Overtrue\Http\Config |
||
355 | */ |
||
356 | protected function normalizeConfig($config): \Overtrue\Http\Config |
||
357 | { |
||
358 | if (\is_array($config)) { |
||
359 | $config = new Config($config); |
||
360 | } |
||
361 | |||
362 | if (!($config instanceof Config)) { |
||
363 | throw new \InvalidArgumentException('config must be array or instance of Overtrue\Http\Config.'); |
||
364 | } |
||
365 | |||
366 | return $config; |
||
367 | } |
||
368 | } |
||
369 |