Base.php
6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
if (!defined('QCLOUDAPI_ROOT_PATH')) {
// 目录入口
define('QCLOUDAPI_ROOT_PATH', dirname(dirname(__FILE__)));
}
require_once QCLOUDAPI_ROOT_PATH . '/Common/Base.php';
/**
* QcloudApi_Module_Base
* 模块基类
*/
abstract class QcloudApi_Module_Base extends QcloudApi_Common_Base
{
/**
* $_serverHost
* 接口域名
* @var string
*/
protected $_serverHost = '';
/**
* $_serverUri
* url路径
* @var string
*/
protected $_serverUri = '/v2/index.php';
/**
* $_secretId
* secretId
* @var string
*/
protected $_secretId = "";
/**
* $_secretKey
* secretKey
* @var string
*/
protected $_secretKey = "";
/**
* $_defaultRegion
* 区域参数
* @var string
*/
protected $_defaultRegion = "";
/**
* $_requestMethod
* 请求方法
* @var string
*/
protected $_requestMethod = "GET";
/**
* __construct
* @param array $config [description]
*/
public function __construct($config = array())
{
if (!empty($config))
$this->setConfig($config);
}
/**
* setConfig
* 设置配置
* @param array $config 模块配置
*/
public function setConfig($config)
{
if (!is_array($config) || !count($config))
return false;
foreach ($config as $key => $val) {
switch ($key) {
case 'SecretId':
$this->setConfigSecretId($val);
break;
case 'SecretKey':
$this->setConfigSecretKey($val);
break;
case 'DefaultRegion':
$this->setConfigDefaultRegion($val);
break;
case 'RequestMethod':
$this->setConfigRequestMethod($val);
break;
default:
;
break;
}
}
return true;
}
/**
* setConfigSecretId
* 设置secretId
* @param string $secretId secretId
*/
public function setConfigSecretId($secretId)
{
$this->_secretId = $secretId;
return $this;
}
/**
* setConfigSecretKey
* 设置secretKey
* @param string $secretKey
*/
public function setConfigSecretKey($secretKey)
{
$this->_secretKey = $secretKey;
return $this;
}
/**
* setConfigDefaultRegion
* 设置区域参数
* @param string $region
*/
public function setConfigDefaultRegion($region)
{
$this->_defaultRegion = $region;
return $this;
}
/**
* setConfigRequestMethod
* 设置请求方法
* @param string $method
*/
public function setConfigRequestMethod($method)
{
$this->_requestMethod = strtoupper($method);
return $this;
}
/**
* getLastRequest
* 获取上次请求的url
* @return
*/
public function getLastRequest()
{
require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php';
return QcloudApi_Common_Request::getRequestUrl();
}
/**
* getLastResponse
* 获取请求的原始返回
* @return
*/
public function getLastResponse()
{
require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php';
return QcloudApi_Common_Request::getRawResponse();
}
/**
* generateUrl
* 生成请求的URL,不发起请求
* @param string $name 接口方法名
* @param array $params 请求参数
* @return
*/
public function generateUrl($name, $params)
{
require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php';
$action = ucfirst($name);
$params['Action'] = $action;
if (!isset($params['Region']))
$params['Region'] = $this->_defaultRegion;
return QcloudApi_Common_Request::generateUrl($params, $this->_secretId, $this->_secretKey, $this->_requestMethod,
$this->_serverHost, $this->_serverUri);
}
/**
* __call
* 通过__call转发请求
* @param string $name 方法名
* @param array $arguments 参数
* @return
*/
public function __call($name, $arguments)
{
$response = $this->_dispatchRequest($name, $arguments);
return $this->_dealResponse($response);
}
/**
* _dispatchRequest
* 发起接口请求
* @param string $name 接口名
* @param array $arguments 接口参数
* @return
*/
protected function _dispatchRequest($name, $arguments)
{
$action = ucfirst($name);
$params = array();
if (is_array($arguments) && !empty($arguments)) {
$params = (array) $arguments[0];
}
$params['Action'] = $action;
if (!isset($params['Region']))
$params['Region'] = $this->_defaultRegion;
require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php';
$response = QcloudApi_Common_Request::send($params, $this->_secretId, $this->_secretKey, $this->_requestMethod,
$this->_serverHost, $this->_serverUri);
return $response;
}
/**
* _dealResponse
* 处理返回
* @param array $rawResponse
* @return
*/
protected function _dealResponse($rawResponse)
{
if (!is_array($rawResponse) || !isset($rawResponse['code'])) {
$this->setError("", 'request falied!');
return false;
}
if ($rawResponse['code']) {
$ext = '';
require_once QCLOUDAPI_ROOT_PATH . '/Common/Error.php';
if (isset($rawResponse['detail'])) {
// 批量异步操作,返回任务失败信息
$ext = $rawResponse['detail'];
}
$this->setError($rawResponse['code'], $rawResponse['message'], $ext);
return false;
}
unset($rawResponse['code'], $rawResponse['message']);
if (count($rawResponse))
return $rawResponse;
else
return true;
}
}