Skip to content

Commit 09021d6

Browse files
committed
Added Custom HttpClient
1 parent 9f8cc90 commit 09021d6

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/Request/Request.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ abstract class Request implements ArrayAccess
8181
*/
8282
public $client;
8383

84+
/**
85+
* @var \AlibabaCloud\Client\Clients\Client
86+
*/
87+
public $httpClient;
88+
8489
/**
8590
* @var Uri
8691
*/
@@ -286,6 +291,19 @@ public function client($clientName)
286291
return $this;
287292
}
288293

294+
295+
/**
296+
*
297+
*
298+
* @param \AlibabaCloud\Client\Clients\Client $client
299+
* @return $this
300+
*/
301+
public function withHttpClient(\AlibabaCloud\Client\Clients\Client $client)
302+
{
303+
$this->httpClient = $client;
304+
return $this;
305+
}
306+
289307
/**
290308
* @return bool
291309
* @throws ClientException

src/Request/Traits/ClientTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function credential()
7070
*/
7171
public function httpClient()
7272
{
73+
if ($this->httpClient){
74+
return $this->httpClient;
75+
}
76+
7377
if (!AlibabaCloud::all()) {
7478
if (CredentialsProvider::hasCustomChain()) {
7579
CredentialsProvider::customProvider($this->client);

tests/Unit/Request/Traits/ClientTraitTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ public function testHttpClient()
107107
$request->httpClient();
108108
}
109109

110+
/**
111+
* @throws ClientException
112+
*/
113+
public function testCustomHttpClient()
114+
{
115+
$httpClient = AlibabaCloud::accessKeyClient('key', 'secret')
116+
->regionId('cn-hangzhou');
117+
$request = AlibabaCloud::rpc()->withHttpClient($httpClient);
118+
self::assertEquals($httpClient,$request->httpClient);
119+
}
120+
110121
/**
111122
* @throws ClientException
112123
*/

0 commit comments

Comments
 (0)