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

Commit 1239172

Browse files
authored
Merge pull request #2 from dew-serverless/configure
Supports configure HTTP client
2 parents 28e784b + d518f3a commit 1239172

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

Diff for: src/MnsClient.php

+35-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818

1919
final class MnsClient
2020
{
21+
/**
22+
* The request options.
23+
*
24+
* @var array<string, mixed>
25+
*/
26+
private array $config = [];
27+
2128
/**
2229
* The signature builder.
2330
*/
@@ -75,6 +82,18 @@ public function accessKeySecret(): string
7582
return $this->accessKeySecret;
7683
}
7784

85+
/**
86+
* Configure request options.
87+
*
88+
* @param array<string, mixed> $config
89+
*/
90+
public function configure(array $config): self
91+
{
92+
$this->config = $config;
93+
94+
return $this;
95+
}
96+
7897
/**
7998
* The signature builder.
8099
*/
@@ -198,11 +217,24 @@ private function send(string $method, string $uri, array $headers = [], array|st
198217
*/
199218
private function client(): Client
200219
{
201-
return new Client([
220+
return new Client([...$this->getConfiguration(), ...[
202221
'base_uri' => $this->endpoint,
203-
'timeout' => 30.0,
204222
'handler' => $this->createHandler(),
205-
]);
223+
]]);
224+
}
225+
226+
/**
227+
* Get configuration for Guzzle client.
228+
*
229+
* @return array<string, mixed>
230+
*/
231+
private function getConfiguration(): array
232+
{
233+
$default = [
234+
'timeout' => 60.0,
235+
];
236+
237+
return [...$default, ...$this->config];
206238
}
207239

208240
/**

Diff for: tests/MnsClientTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@
1010
$mns->get('/');
1111
$mns->assertSent(fn ($request) => $request->hasHeader('Authorization') && $request->getHeaderLine('Authorization') === 'MNS key:foo');
1212
});
13+
14+
test('configures request options', function () {
15+
$mns = new MnsClient('https://123456789101112.mns.cn-hangzhou.aliyuncs.com', 'key', 'secret');
16+
$mns->configure(['headers' => ['X-Foo' => 'Bar']]);
17+
$mns->fake();
18+
$mns->get('/');
19+
$mns->assertSent(fn ($request) => $request->getHeaderLine('X-Foo') === 'Bar');
20+
});

0 commit comments

Comments
 (0)