From d518f3ac6ab44ae7026896ec2a2f3688f93b65d3 Mon Sep 17 00:00:00 2001 From: Li Zhineng Date: Tue, 7 Nov 2023 11:12:43 +0800 Subject: [PATCH] configures client --- src/MnsClient.php | 38 +++++++++++++++++++++++++++++++++++--- tests/MnsClientTest.php | 8 ++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/MnsClient.php b/src/MnsClient.php index db49afa..7fc72ce 100644 --- a/src/MnsClient.php +++ b/src/MnsClient.php @@ -18,6 +18,13 @@ final class MnsClient { + /** + * The request options. + * + * @var array + */ + private array $config = []; + /** * The signature builder. */ @@ -75,6 +82,18 @@ public function accessKeySecret(): string return $this->accessKeySecret; } + /** + * Configure request options. + * + * @param array $config + */ + public function configure(array $config): self + { + $this->config = $config; + + return $this; + } + /** * The signature builder. */ @@ -198,11 +217,24 @@ private function send(string $method, string $uri, array $headers = [], array|st */ private function client(): Client { - return new Client([ + return new Client([...$this->getConfiguration(), ...[ 'base_uri' => $this->endpoint, - 'timeout' => 30.0, 'handler' => $this->createHandler(), - ]); + ]]); + } + + /** + * Get configuration for Guzzle client. + * + * @return array + */ + private function getConfiguration(): array + { + $default = [ + 'timeout' => 60.0, + ]; + + return [...$default, ...$this->config]; } /** diff --git a/tests/MnsClientTest.php b/tests/MnsClientTest.php index a69a453..79e7748 100644 --- a/tests/MnsClientTest.php +++ b/tests/MnsClientTest.php @@ -10,3 +10,11 @@ $mns->get('/'); $mns->assertSent(fn ($request) => $request->hasHeader('Authorization') && $request->getHeaderLine('Authorization') === 'MNS key:foo'); }); + +test('configures request options', function () { + $mns = new MnsClient('https://123456789101112.mns.cn-hangzhou.aliyuncs.com', 'key', 'secret'); + $mns->configure(['headers' => ['X-Foo' => 'Bar']]); + $mns->fake(); + $mns->get('/'); + $mns->assertSent(fn ($request) => $request->getHeaderLine('X-Foo') === 'Bar'); +});