diff --git a/src/Client.php b/src/Client.php index aa08a2e..931e43f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -3,7 +3,10 @@ namespace PhpHttpClient; use GuzzleHttp\Client as GuzzleClient; +use GuzzleHttp\HandlerStack; use PhpHttpClient\Concerns\HttpBehavior; +use PhpHttpClient\Exceptions\HttpClientException; +use Psr\Http\Message\ResponseInterface; /** * @final @@ -12,7 +15,10 @@ class Client implements Contracts\HttpMethods { use HttpBehavior; + /** @var GuzzleClient */ private $instance; + + /** @var array */ private $options; public function __construct(string $baseUri, array $options = []) { @@ -22,38 +28,62 @@ public function __construct(string $baseUri, array $options = []) public function withQuery(array $query): self { - $this->options['query'] = $query; - return $this; + $clone = clone $this; + $clone->options['query'] = $query; + return $clone; } public function withBody(array $body): self { - $this->options['body'] = $body; - return $this; + $clone = clone $this; + $clone->options['body'] = $body; + return $clone; + } + + public function withJson(array $data): self + { + $clone = clone $this; + $clone->options['json'] = $data; + return $clone; + } + + public function withMiddleware(callable $middleware): self + { + $clone = clone $this; + $handler = $clone->instance->getConfig('handler'); + if ($handler instanceof HandlerStack) { + $handler->push($middleware); + } + + return $clone; } public function withOptions(array $options): self { - $this->options = array_merge($this->options, $options); - return $this; + $clone = clone $this; + $clone->options = array_merge($clone->options, $options); + return $clone; } public function withHeaders(array $headers): self { - $this->options['headers'] = $headers; - return $this; + $clone = clone $this; + $clone->options['headers'] = $headers; + return $clone; } public function withBasicAuth(string $username, string $password): self { - $this->options['auth'] = [$username, $password]; - return $this; + $clone = clone $this; + $clone->options['auth'] = [$username, $password]; + return $clone; } public function withDigestAuth(string $username, string $password): self { - $this->options['auth'] = [$username, $password, 'digest']; - return $this; + $clone = clone $this; + $clone->options['auth'] = [$username, $password, 'digest']; + return $clone; } public function getOptions(): array @@ -66,11 +96,6 @@ public function getBaseUri(): string return $this->getInstance()->getConfig('base_uri'); } - public function getBaseUrl(): string - { - return $this->getInstance()->getConfig('base_url'); - } - public function getHeaders(): array { return $this->getInstance()->getConfig('headers'); @@ -80,4 +105,17 @@ public function getAuth(): array { return $this->getInstance()->getConfig('auth'); } + + public function getBodyAsString(ResponseInterface $response): string + { + return (string) $response->getBody(); + } + + /** + * @return mixed + */ + public function getJson(ResponseInterface $response, bool $assoc = true) + { + return json_decode($this->getBodyAsString($response), $assoc); + } } \ No newline at end of file diff --git a/src/Concerns/HttpBehavior.php b/src/Concerns/HttpBehavior.php index 1c78da6..c1b4efa 100644 --- a/src/Concerns/HttpBehavior.php +++ b/src/Concerns/HttpBehavior.php @@ -3,6 +3,7 @@ use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Exception\GuzzleException; +use PhpHttpClient\Exceptions\HttpClientException; use Psr\Http\Message\ResponseInterface; trait HttpBehavior @@ -22,7 +23,13 @@ public function getUri(array $config = []): string */ public function request(array $config = []): ResponseInterface { - return $this->getInstance()->request($config['method'], $config['url'], $config); + $merged = array_merge($this->options ?? [], $config); + + try { + return $this->getInstance()->request($merged['method'], $merged['url'], $merged); + } catch (GuzzleException $e) { + throw new HttpClientException($e->getMessage(), (int) $e->getCode(), $e); + } } /** diff --git a/src/Exceptions/HttpClientException.php b/src/Exceptions/HttpClientException.php new file mode 100644 index 0000000..05f8c52 --- /dev/null +++ b/src/Exceptions/HttpClientException.php @@ -0,0 +1,13 @@ +toBeInstanceOf(Client::class); }); +it('tests withJson method', function () { + $client = new Client('http://example.com'); + $data = ['foo' => 'bar']; + + $result = $client->withJson($data); + + expect($result)->toBeInstanceOf(Client::class); +}); + it('tests withQuery method with github api', function () { $client = new Client('https://api.github.com'); $query = ['q' => 'php']; diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index baac17c..32a4f59 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -11,4 +11,8 @@ it('should return the protocol of the url', function () { $this->assertEquals(parseProtocol('https://www.google.com'), 'https'); +}); + +it('should return the type of the argument', function () { + $this->assertEquals('integer', kindOf(1)); }); \ No newline at end of file