Skip to content

Commit 6ff05b6

Browse files
committed
refactor(client): Refactor the Client class
- 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
1 parent 6ff1f12 commit 6ff05b6

1 file changed

Lines changed: 44 additions & 11 deletions

File tree

src/Foundation/Client.php

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Guanguans\Notify\Foundation\Credentials\NullCredential;
1717
use Guanguans\Notify\Foundation\Middleware\ApplyCredentialToRequest;
1818
use Guanguans\Notify\Foundation\Traits\Tappable;
19+
use GuzzleHttp\Client as GuzzleClient;
1920
use GuzzleHttp\Exception\GuzzleException;
2021
use GuzzleHttp\HandlerStack;
2122
use Psr\Http\Message\ResponseInterface;
@@ -25,19 +26,14 @@ class Client implements Contracts\Client
2526
use Tappable;
2627

2728
private Credential $credential;
28-
private ?\GuzzleHttp\Client $httpClient;
29+
private ?GuzzleClient $httpClient;
30+
private HandlerStack $handlerStack;
2931

30-
public function __construct(Credential $credential = null, \GuzzleHttp\Client $httpClient = null)
32+
public function __construct(Credential $credential = null, GuzzleClient $httpClient = null)
3133
{
3234
$this->credential = $credential ?? new NullCredential();
33-
$this->httpClient = $httpClient ?? new \GuzzleHttp\Client([
34-
'handler' => (function (): HandlerStack {
35-
$handlerStack = HandlerStack::create();
36-
$handlerStack->push(new ApplyCredentialToRequest($this->credential), ApplyCredentialToRequest::name());
37-
38-
return $handlerStack;
39-
})(),
40-
]);
35+
$this->httpClient = $httpClient;
36+
$this->handlerStack = HandlerStack::create();
4137
}
4238

4339
/**
@@ -46,11 +42,48 @@ public function __construct(Credential $credential = null, \GuzzleHttp\Client $h
4642
public function send(Contracts\Message $message): ResponseInterface
4743
{
4844
return $this
49-
->httpClient
45+
->createHttpClient()
5046
->request(
5147
$message->httpMethod(),
5248
$message->httpUri(),
5349
$this->credential->applyToOptions($message->toHttpOptions())
5450
);
5551
}
52+
53+
public function setHttpClient(?GuzzleClient $httpClient): self
54+
{
55+
$this->httpClient = $httpClient;
56+
57+
return $this;
58+
}
59+
60+
public function setHandlerStack(HandlerStack $handlerStack): self
61+
{
62+
$this->handlerStack = $handlerStack;
63+
64+
return $this;
65+
}
66+
67+
public function pushMiddleware(callable $callable, string $name = ''): self
68+
{
69+
$this->handlerStack->push($callable, $name);
70+
71+
return $this;
72+
}
73+
74+
private function createHttpClient(): GuzzleClient
75+
{
76+
if (! $this->httpClient instanceof GuzzleClient) {
77+
$this->handlerStack->push(
78+
new ApplyCredentialToRequest($this->credential),
79+
ApplyCredentialToRequest::name()
80+
);
81+
82+
$this->httpClient = new GuzzleClient([
83+
'handler' => $this->handlerStack,
84+
]);
85+
}
86+
87+
return $this->httpClient;
88+
}
5689
}

0 commit comments

Comments
 (0)