|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Ripple\Http\Guzzle; |
| 4 | + |
| 5 | +use GuzzleHttp\ClientInterface; |
| 6 | +use GuzzleHttp\ClientTrait; |
| 7 | +use GuzzleHttp\Exception\GuzzleException; |
| 8 | +use GuzzleHttp\Promise\PromiseInterface; |
| 9 | +use Psr\Http\Client\ClientExceptionInterface; |
| 10 | +use Psr\Http\Message\RequestInterface; |
| 11 | +use Psr\Http\Message\ResponseInterface; |
| 12 | + |
| 13 | +use function array_merge; |
| 14 | +use function call_user_func_array; |
| 15 | + |
| 16 | +/** |
| 17 | + * |
| 18 | + */ |
| 19 | +class Client implements ClientInterface, \Psr\Http\Client\ClientInterface |
| 20 | +{ |
| 21 | + use ClientTrait; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \GuzzleHttp\Client |
| 25 | + */ |
| 26 | + private \GuzzleHttp\Client $guzzleClient; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param array $config |
| 30 | + */ |
| 31 | + public function __construct(array $config = []) |
| 32 | + { |
| 33 | + $this->guzzleClient = new \GuzzleHttp\Client(array_merge( |
| 34 | + [ |
| 35 | + 'handler' => new RippleInvoke() |
| 36 | + ], |
| 37 | + $config |
| 38 | + )); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param RequestInterface $request |
| 43 | + * @param array $options |
| 44 | + * @return ResponseInterface |
| 45 | + * @throws GuzzleException |
| 46 | + */ |
| 47 | + public function send(RequestInterface $request, array $options = []): ResponseInterface |
| 48 | + { |
| 49 | + return $this->guzzleClient->send($request, $options); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param RequestInterface $request |
| 54 | + * @param array $options |
| 55 | + * @return PromiseInterface |
| 56 | + */ |
| 57 | + public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface |
| 58 | + { |
| 59 | + return $this->guzzleClient->sendAsync($request, $options); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param string $method |
| 64 | + * @param $uri |
| 65 | + * @param array $options |
| 66 | + * @return ResponseInterface |
| 67 | + * @throws GuzzleException |
| 68 | + */ |
| 69 | + public function request(string $method, $uri, array $options = []): ResponseInterface |
| 70 | + { |
| 71 | + return $this->guzzleClient->request($method, $uri, $options); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param string $method |
| 76 | + * @param $uri |
| 77 | + * @param array $options |
| 78 | + * @return PromiseInterface |
| 79 | + */ |
| 80 | + public function requestAsync(string $method, $uri, array $options = []): PromiseInterface |
| 81 | + { |
| 82 | + return $this->guzzleClient->requestAsync($method, $uri, $options); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param RequestInterface $request |
| 87 | + * @return ResponseInterface |
| 88 | + * @throws ClientExceptionInterface |
| 89 | + */ |
| 90 | + public function sendRequest(RequestInterface $request): ResponseInterface |
| 91 | + { |
| 92 | + return $this->guzzleClient->sendRequest($request); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param string|null $option |
| 97 | + * @return void |
| 98 | + */ |
| 99 | + public function getConfig(?string $option = null): void |
| 100 | + { |
| 101 | + $this->guzzleClient->getConfig($option); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @param string $name |
| 106 | + * @param array $arguments |
| 107 | + * @return mixed |
| 108 | + */ |
| 109 | + public function __call(string $name, array $arguments) |
| 110 | + { |
| 111 | + return call_user_func_array([$this->guzzleClient, $name], $arguments); |
| 112 | + } |
| 113 | +} |
0 commit comments