1 | <?php |
||
2 | |||
3 | namespace AlibabaCloud\Client\Request\Traits; |
||
4 | |||
5 | use AlibabaCloud\Client\AlibabaCloud; |
||
6 | use AlibabaCloud\Client\Clients\Client; |
||
7 | use AlibabaCloud\Client\Support\Arrays; |
||
8 | use AlibabaCloud\Client\Request\Request; |
||
9 | use AlibabaCloud\Client\Credentials\StsCredential; |
||
10 | use AlibabaCloud\Client\Exception\ClientException; |
||
11 | use AlibabaCloud\Client\Exception\ServerException; |
||
12 | use AlibabaCloud\Client\Credentials\AccessKeyCredential; |
||
13 | use AlibabaCloud\Client\Credentials\Requests\AssumeRole; |
||
14 | use AlibabaCloud\Client\Credentials\CredentialsInterface; |
||
15 | use AlibabaCloud\Client\Credentials\BearerTokenCredential; |
||
16 | use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider; |
||
17 | use AlibabaCloud\Client\Credentials\Requests\GenerateSessionAccessKey; |
||
18 | |||
19 | /** |
||
20 | * Trait ClientTrait |
||
21 | * |
||
22 | * @package AlibabaCloud\Client\Request\Traits |
||
23 | * |
||
24 | * @mixin Request |
||
25 | */ |
||
26 | trait ClientTrait |
||
27 | { |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private static $config = []; |
||
32 | |||
33 | /** |
||
34 | * @param array $config |
||
35 | */ |
||
36 | 1 | public static function config(array $config) |
|
37 | { |
||
38 | 1 | self::$config = $config; |
|
39 | 1 | } |
|
40 | |||
41 | /** |
||
42 | * Return credentials directly if it is an AssumeRole or GenerateSessionAccessKey. |
||
43 | * |
||
44 | * @return AccessKeyCredential|BearerTokenCredential|CredentialsInterface|StsCredential |
||
45 | * @throws ClientException |
||
46 | * @throws ServerException |
||
47 | */ |
||
48 | 86 | public function credential() |
|
49 | { |
||
50 | 86 | if ($this instanceof AssumeRole || $this instanceof GenerateSessionAccessKey) { |
|
51 | 13 | return $this->httpClient()->getCredential(); |
|
52 | } |
||
53 | |||
54 | 75 | $timeout = isset($this->options['timeout']) |
|
55 | 75 | ? $this->options['timeout'] |
|
56 | 75 | : Request::TIMEOUT; |
|
57 | |||
58 | 75 | $connectTimeout = isset($this->options['connect_timeout']) |
|
59 | 75 | ? $this->options['connect_timeout'] |
|
60 | 75 | : Request::CONNECT_TIMEOUT; |
|
61 | |||
62 | 75 | return $this->httpClient()->getSessionCredential($timeout, $connectTimeout); |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * Get the client based on the request's settings. |
||
67 | * |
||
68 | * @return Client |
||
69 | * @throws ClientException |
||
70 | */ |
||
71 | 99 | public function httpClient() |
|
72 | { |
||
73 | 99 | if (!AlibabaCloud::all()) { |
|
74 | 4 | if (CredentialsProvider::hasCustomChain()) { |
|
75 | 2 | CredentialsProvider::customProvider($this->client); |
|
76 | 2 | } else { |
|
77 | 2 | CredentialsProvider::defaultProvider($this->client); |
|
78 | } |
||
79 | 4 | } |
|
80 | |||
81 | 99 | return AlibabaCloud::get($this->client); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Merged with the client's options, the same name will be overwritten. |
||
86 | * |
||
87 | * @throws ClientException |
||
88 | */ |
||
89 | 66 | public function mergeOptionsIntoClient() |
|
90 | { |
||
91 | 66 | $this->options = Arrays::merge( |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
92 | [ |
||
93 | 66 | $this->httpClient()->options, |
|
94 | 66 | $this->options |
|
95 | 66 | ] |
|
96 | 66 | ); |
|
97 | 66 | } |
|
98 | } |
||
99 |