From 1733b011b4d438aa8f6c70cdf6b9b4c9a80c9194 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Fri, 18 Oct 2019 19:57:03 +0300 Subject: [PATCH] Builder as class, call forwarding in Request, more HTTP methods as magic --- src/{Traits/Buildable.php => Builder.php} | 32 ++++++++++++---- src/HttpClient.php | 45 +++++++++++++++++++++-- src/Request.php | 24 +++++++++++- src/Traits/Requestable.php | 27 ++++++++++---- 4 files changed, 108 insertions(+), 20 deletions(-) rename src/{Traits/Buildable.php => Builder.php} (84%) diff --git a/src/Traits/Buildable.php b/src/Builder.php similarity index 84% rename from src/Traits/Buildable.php rename to src/Builder.php index 7a27665..242e29d 100644 --- a/src/Traits/Buildable.php +++ b/src/Builder.php @@ -1,26 +1,42 @@ request = array_merge($this->request, $options); } - protected function resetBuilderState() + /** + * Reset request options + */ + public function resetRequest() { $this->request = []; } - protected function returnAndResetBuilderState() + /** + * Get request options + * + * @return array + */ + public function getRequest() { - $request = $this->request; - $this->resetBuilderState(); - return $request; + return $this->request; } /** diff --git a/src/HttpClient.php b/src/HttpClient.php index 9874075..25c2b51 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -3,29 +3,62 @@ namespace Ivan770\HttpClient; use Illuminate\Support\Traits\Macroable; -use Ivan770\HttpClient\Traits\Buildable; use Ivan770\HttpClient\Traits\InteractsWithEloquent; use Ivan770\HttpClient\Traits\Requestable; use Symfony\Component\HttpClient\HttpClient as Client; /** + * @method HttpClient auth(string $type, array|string $credentials) Authentication credentials + * @method HttpClient authBasic(array|string $credentials) Add HTTP basic auth to request + * @method HttpClient authBearer(string $credentials) Add Bearer token to request + * @method HttpClient headers(array $headers) Add headers to request + * @method HttpClient body(array|string|resource|\Traversable|\Closure $body) Add body to request + * @method HttpClient json(array|\JsonSerializable $json) Add JSON to request + * @method HttpClient query(array $query) Add query string values to request + * @method HttpClient withoutRedirects() Ignore all redirects for this request + * @method HttpClient proxy(string $proxy, string $noproxy) Change proxy for this request * @method Response get(string $url, array $arguments = []) Send a GET request * @method Response head(string $url, array $arguments = []) Send a HEAD request * @method Response post(string $url, array $arguments = []) Send a POST request * @method Response put(string $url, array $arguments = []) Send a PUT request * @method Response delete(string $url, array $arguments = []) Send a DELETE request + * @method Response connect(string $url, array $arguments = []) Send a CONNECT request + * @method Response options(string $url, array $arguments = []) Send a OPTIONS request + * @method Response trace(string $url, array $arguments = []) Send a TRACE request + * @method Response patch(string $url, array $arguments = []) Send a PATCH request + * + * @see Builder */ class HttpClient { - use InteractsWithEloquent, Buildable, Requestable, Macroable { + use InteractsWithEloquent, Requestable, Macroable { Macroable::__call as macroCall; } + /** + * @var \Symfony\Contracts\HttpClient\HttpClientInterface + */ protected $client; - public function __construct(Client $client, $clientArgs = []) + /** + * @var Builder + */ + protected $builder; + + public function __construct(Client $client, Builder $builder, $clientArgs = []) { $this->client = $client->create($clientArgs); + $this->builder = $builder; + } + + /** + * Get builder instance + * + * @return Builder + */ + public function getBuilder() + { + return $this->builder; } public function __call($name, $arguments) @@ -38,6 +71,12 @@ public function __call($name, $arguments) return $this->callRequestMethod($name, $arguments); } + if(method_exists($this->builder, $name)) { + $this->builder->$name(...$arguments); + + return $this; + } + return $this->client->$name(...$arguments); } } \ No newline at end of file diff --git a/src/Request.php b/src/Request.php index 73a3150..a1b6b2c 100644 --- a/src/Request.php +++ b/src/Request.php @@ -5,6 +5,19 @@ use Ivan770\HttpClient\Contracts\Request as RequestContract; +/** + * @method RequestContract auth(string $type, array|string $credentials) Authentication credentials + * @method RequestContract authBasic(array|string $credentials) Add HTTP basic auth to request + * @method RequestContract authBearer(string $credentials) Add Bearer token to request + * @method RequestContract headers(array $headers) Add headers to request + * @method RequestContract body(array|string|resource|\Traversable|\Closure $body) Add body to request + * @method RequestContract json(array|\JsonSerializable $json) Add JSON to request + * @method RequestContract query(array $query) Add query string values to request + * @method RequestContract withoutRedirects() Ignore all redirects for this request + * @method RequestContract proxy(string $proxy, string $noproxy) Change proxy for this request + * + * @see Builder + */ abstract class Request implements RequestContract { /** @@ -34,6 +47,8 @@ abstract class Request implements RequestContract public function __construct(HttpClient $client) { $this->client = $client; + + $this->defaultAttach($this->client); } /** @@ -107,8 +122,6 @@ public function mock($test) */ public function execute() { - $this->defaultAttach($this->client); - $method = $this->getMethod(); return $this->client->$method($this->getResource()); @@ -123,4 +136,11 @@ public function get() { return $this->execute()->getContent(); } + + public function __call($name, $arguments) + { + $this->client->$name(...$arguments); + + return $this; + } } \ No newline at end of file diff --git a/src/Traits/Requestable.php b/src/Traits/Requestable.php index c450971..68b1d6b 100644 --- a/src/Traits/Requestable.php +++ b/src/Traits/Requestable.php @@ -7,11 +7,15 @@ trait Requestable { protected $methods = [ - "get", - "head", - "post", - "put", - "delete", + 'get', + 'head', + 'post', + 'put', + 'delete', + 'connect', + 'options', + 'trace', + 'patch', ]; protected function isRequestMethod($name) @@ -21,9 +25,18 @@ protected function isRequestMethod($name) protected function callRequestMethod($name, $arguments) { - $this->applyRequestOptions($arguments[1] ?? []); - $request = $this->returnAndResetBuilderState(); + $this->builder->applyRequestOptions($arguments[1] ?? []); + $request = $this->getRequest(); + $response = $this->client->request(strtoupper($name), $arguments[0], $request); return new Response($response); } + + protected function getRequest() + { + $request = $this->builder->getRequest(); + $this->builder->resetRequest(); + + return $request; + } } \ No newline at end of file