Skip to content

Commit

Permalink
Builder as class, call forwarding in Request, more HTTP methods as magic
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan770 committed Oct 18, 2019
1 parent f2b3dc9 commit 1733b01
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 20 deletions.
32 changes: 24 additions & 8 deletions src/Traits/Buildable.php → src/Builder.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
<?php

namespace Ivan770\HttpClient\Traits;
namespace Ivan770\HttpClient;

trait Buildable
class Builder
{
/**
* Request options, that will be passed to Symfony HttpClient
*
* @var array
*/
protected $request = [];

protected function applyRequestOptions($options)
/**
* Apply request options to current builder
*
* @param $options
*/
public function applyRequestOptions($options)
{
$this->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;
}

/**
Expand Down
45 changes: 42 additions & 3 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}
24 changes: 22 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down Expand Up @@ -34,6 +47,8 @@ abstract class Request implements RequestContract
public function __construct(HttpClient $client)
{
$this->client = $client;

$this->defaultAttach($this->client);
}

/**
Expand Down Expand Up @@ -107,8 +122,6 @@ public function mock($test)
*/
public function execute()
{
$this->defaultAttach($this->client);

$method = $this->getMethod();

return $this->client->$method($this->getResource());
Expand All @@ -123,4 +136,11 @@ public function get()
{
return $this->execute()->getContent();
}

public function __call($name, $arguments)
{
$this->client->$name(...$arguments);

return $this;
}
}
27 changes: 20 additions & 7 deletions src/Traits/Requestable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
}

0 comments on commit 1733b01

Please sign in to comment.