Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from dew-serverless/configure
Browse files Browse the repository at this point in the history
Supports configure HTTP client
  • Loading branch information
lizhineng authored Nov 7, 2023
2 parents 28e784b + d518f3a commit 1239172
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/MnsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@

final class MnsClient
{
/**
* The request options.
*
* @var array<string, mixed>
*/
private array $config = [];

/**
* The signature builder.
*/
Expand Down Expand Up @@ -75,6 +82,18 @@ public function accessKeySecret(): string
return $this->accessKeySecret;
}

/**
* Configure request options.
*
* @param array<string, mixed> $config
*/
public function configure(array $config): self
{
$this->config = $config;

return $this;
}

/**
* The signature builder.
*/
Expand Down Expand Up @@ -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<string, mixed>
*/
private function getConfiguration(): array
{
$default = [
'timeout' => 60.0,
];

return [...$default, ...$this->config];
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/MnsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit 1239172

Please sign in to comment.