4
4
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5
5
* @license http://www.tintsoft.com/license/
6
6
*/
7
+
7
8
namespace xutl \qcloud ;
8
9
9
10
use yii \base \Component ;
10
11
use yii \base \InvalidConfigException ;
11
12
12
13
/**
13
- * Class Qcloud
14
+ * Class QCloud
14
15
* @package xutl\qcloud
15
16
*/
16
- class Qcloud extends Component
17
+ class QCloud extends Component
17
18
{
19
+ /**
20
+ * @var string
21
+ */
22
+ public $ secretId ;
23
+
24
+ /**
25
+ * @var string
26
+ */
27
+ public $ secretKey ;
28
+
29
+ /**
30
+ * @var bool 是否使用安全连接
31
+ */
32
+ public $ secureConnection = true ;
33
+
34
+ /**
35
+ * 请求的Uri
36
+ * @var string
37
+ */
38
+ public $ serverUri = '/v2/index.php ' ;
39
+
40
+ public $ defaultRegion = null ;
41
+
42
+ /**
43
+ * @inheritdoc
44
+ */
45
+ public function init ()
46
+ {
47
+ parent ::init ();
48
+ if ($ this ->secretId === null ) {
49
+ throw new InvalidConfigException ('The "secretId" property must be set. ' );
50
+ }
51
+ if ($ this ->secretKey === null ) {
52
+ throw new InvalidConfigException ('The "secretKey" property must be set. ' );
53
+ }
54
+ }
55
+
56
+ /**
57
+ * 创建API请求
58
+ * @param string $service 服务名称
59
+ * @param string|null $region 区域名称
60
+ * @return \yii\httpclient\Request
61
+ */
62
+ public function createRequest ($ service , $ region = null )
63
+ {
64
+ $ serverHost = $ service . '.api.qcloud.com ' ;
65
+ return (new Client ([
66
+ 'serverHost ' => $ serverHost ,
67
+ 'secretId ' => $ this ->secretId ,
68
+ 'secretKey ' => $ this ->secretKey ,
69
+ 'secureConnection ' => $ this ->secureConnection ,
70
+ 'serverUri ' => $ this ->serverUri ,
71
+ 'region ' => $ region ? $ region : $ this ->defaultRegion
72
+ ]))->createRequest ()->setMethod ('POST ' );
73
+ }
74
+
75
+
18
76
/**
19
77
* 用户账户
20
78
*/
@@ -134,100 +192,4 @@ class Qcloud extends Component
134
192
* 云搜
135
193
*/
136
194
const API_YUNSOU = 'yunsou ' ;
137
-
138
- /**
139
- * @var string
140
- */
141
- public $ secretId ;
142
-
143
- /**
144
- * @var string
145
- */
146
- public $ secretKey ;
147
-
148
- /**
149
- * 请求的Uri
150
- * @var string
151
- */
152
- public $ serverUri = '/v2/index.php ' ;
153
-
154
- /**
155
- * 请求方法
156
- * @var string
157
- */
158
- public $ requestMethod = "POST " ;
159
-
160
- /**
161
- * 服务器地址
162
- * @var array
163
- */
164
- protected $ _serverHosts = [
165
- 'account ' => 'account.api.qcloud.com ' ,
166
- 'bill ' => 'bill.api.qcloud.com ' ,
167
- 'bm ' => 'bm.api.qcloud.com ' ,
168
- 'cbs ' => 'cbs.api.qcloud.com ' ,
169
- 'cdb ' => 'cdb.api.qcloud.com ' ,
170
- 'cdn ' => 'cdn.api.qcloud.com ' ,
171
- 'cmem ' => 'cmem.api.qcloud.com ' ,
172
- 'cns ' => 'cns.api.qcloud.com ' ,
173
- 'cvm ' => 'cvm.api.qcloud.com ' ,
174
- 'eip ' => 'eip.api.qcloud.com ' ,
175
- 'image ' => 'image.api.qcloud.com ' ,
176
- 'lb ' => 'lb.api.qcloud.com ' ,
177
- 'live ' => 'live.api.qcloud.com ' ,
178
- 'market ' => 'market.api.qcloud.com ' ,
179
- 'monitor ' => 'monitor.api.qcloud.com ' ,
180
- 'scaling ' => 'scaling.api.qcloud.com ' ,
181
- 'sec ' => 'csec.api.qcloud.com ' ,
182
- 'snapshot ' => 'snapshot.api.qcloud.com ' ,
183
- 'tdsql ' => 'tdsql.api.qcloud.com ' ,
184
- 'trade ' => 'trade.api.qcloud.com ' ,
185
- 'vod ' => 'vod.api.qcloud.com ' ,
186
- 'vpc ' => 'vpc.api.qcloud.com ' ,
187
- 'wenzhi ' => 'wenzhi.api.qcloud.com ' ,
188
- 'yunsou ' => 'yunsou.api.qcloud.com ' ,
189
- ];
190
-
191
- /**
192
- * @inheritdoc
193
- */
194
- public function init ()
195
- {
196
- parent ::init ();
197
- if ($ this ->secretId === null ) {
198
- throw new InvalidConfigException ('The "secretId" property must be set. ' );
199
- }
200
- if ($ this ->secretKey === null ) {
201
- throw new InvalidConfigException ('The "secretKey" property must be set. ' );
202
- }
203
- }
204
-
205
- /**
206
- * 获取服务器主机名
207
- * @param string $service
208
- * @return mixed|null
209
- */
210
- protected function getServerHost ($ service )
211
- {
212
- return isset ($ this ->_serverHosts [$ service ]) ? $ this ->_serverHosts [$ service ] : null ;
213
- }
214
-
215
- /**
216
- * 创建API请求
217
- * @param string $service 服务名称
218
- * @param string|null $region 区域名称
219
- * @return Request
220
- */
221
- public function createRequest ($ service , $ region = null )
222
- {
223
- $ serverHost = $ this ->getServerHost ($ service );
224
- return new Request ([
225
- 'secretId ' => $ this ->secretId ,
226
- 'secretKey ' => $ this ->secretKey ,
227
- 'defaultRegion ' => $ region ,
228
- 'requestMethod ' => $ this ->requestMethod ,
229
- 'serverUri ' => $ this ->serverUri ,
230
- 'serverHost ' => $ serverHost
231
- ]);
232
- }
233
195
}
0 commit comments