Skip to content

Commit

Permalink
refactor(client): Refactor the Client class
Browse files Browse the repository at this point in the history
- Refactor the constructor to accept nullable GuzzleClient and HandlerStack parameters
- Add a new method `createHttpClient` to create the GuzzleClient instance
- Modify the `send` method to use the `createHttpClient` method
- Add new methods `setHttpClient`, `setHandlerStack`, and `pushMiddleware` to allow customization of the GuzzleClient
- Update the `send` method to use the `handlerStack` property
- Update the `createHttpClient` method to check if the `httpClient` property is already set
- Update the `createHttpClient` method to push the `ApplyCredentialToRequest` middleware to the `handlerStack`
- Update the `createHttpClient` method to set the `handlerStack` property in the GuzzleClient options
  • Loading branch information
guanguans committed Jan 24, 2024
1 parent 6ff1f12 commit 6ff05b6
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions src/Foundation/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Guanguans\Notify\Foundation\Credentials\NullCredential;
use Guanguans\Notify\Foundation\Middleware\ApplyCredentialToRequest;
use Guanguans\Notify\Foundation\Traits\Tappable;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\HandlerStack;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -25,19 +26,14 @@ class Client implements Contracts\Client
use Tappable;

private Credential $credential;
private ?\GuzzleHttp\Client $httpClient;
private ?GuzzleClient $httpClient;
private HandlerStack $handlerStack;

public function __construct(Credential $credential = null, \GuzzleHttp\Client $httpClient = null)
public function __construct(Credential $credential = null, GuzzleClient $httpClient = null)
{
$this->credential = $credential ?? new NullCredential();
$this->httpClient = $httpClient ?? new \GuzzleHttp\Client([
'handler' => (function (): HandlerStack {
$handlerStack = HandlerStack::create();
$handlerStack->push(new ApplyCredentialToRequest($this->credential), ApplyCredentialToRequest::name());

return $handlerStack;
})(),
]);
$this->httpClient = $httpClient;
$this->handlerStack = HandlerStack::create();
}

/**
Expand All @@ -46,11 +42,48 @@ public function __construct(Credential $credential = null, \GuzzleHttp\Client $h
public function send(Contracts\Message $message): ResponseInterface
{
return $this
->httpClient
->createHttpClient()
->request(
$message->httpMethod(),
$message->httpUri(),
$this->credential->applyToOptions($message->toHttpOptions())
);
}

public function setHttpClient(?GuzzleClient $httpClient): self
{
$this->httpClient = $httpClient;

return $this;
}

public function setHandlerStack(HandlerStack $handlerStack): self
{
$this->handlerStack = $handlerStack;

return $this;
}

public function pushMiddleware(callable $callable, string $name = ''): self
{
$this->handlerStack->push($callable, $name);

return $this;
}

private function createHttpClient(): GuzzleClient
{
if (! $this->httpClient instanceof GuzzleClient) {
$this->handlerStack->push(
new ApplyCredentialToRequest($this->credential),
ApplyCredentialToRequest::name()
);

$this->httpClient = new GuzzleClient([
'handler' => $this->handlerStack,
]);
}

return $this->httpClient;
}
}

0 comments on commit 6ff05b6

Please sign in to comment.