1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Koded package. |
||
5 | * |
||
6 | * (c) Mihail Binev <[email protected]> |
||
7 | * |
||
8 | * Please view the LICENSE distributed with this source code |
||
9 | * for the full copyright and license information. |
||
10 | * |
||
11 | */ |
||
12 | |||
13 | namespace Koded\Http\Client; |
||
14 | |||
15 | use Koded\Http\Interfaces\HttpStatus; |
||
16 | use Psr\Http\Message\{RequestInterface, ResponseInterface}; |
||
17 | |||
18 | trait Psr18ClientTrait |
||
19 | { |
||
20 | /** |
||
21 | * Sends a PSR-7 request and returns a PSR-7 response. |
||
22 | * |
||
23 | * @param RequestInterface $request |
||
24 | * @return ResponseInterface |
||
25 | * @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request. |
||
26 | */ |
||
27 | public function sendRequest(RequestInterface $request): ResponseInterface |
||
28 | { |
||
29 | /** @var ResponseInterface $response */ |
||
30 | $response = $this |
||
31 | ->withMethod($request->getMethod()) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
32 | 10 | ->withUri($request->getUri()) |
|
33 | ->withHeaders($request->getHeaders()) |
||
34 | ->withBody($request->getBody()) |
||
35 | ->read(); |
||
36 | 10 | ||
37 | 10 | if ($response->getStatusCode() >= HttpStatus::BAD_REQUEST) { |
|
38 | 10 | throw new Psr18Exception( |
|
39 | 10 | $response->getBody()->getContents(), |
|
40 | 10 | $response->getStatusCode(), |
|
41 | $request); |
||
42 | 10 | } |
|
43 | 6 | return $response; |
|
44 | } |
||
45 | } |
||
46 |