...
|
...
|
@@ -28,7 +28,7 @@ class QcloudApi: |
|
|
self.secretId = secretId
|
|
|
self.secretKey = secretKey
|
|
|
|
|
|
def do_query(self, params, req_url=URLS_lb):
|
|
|
def do_query(self, params, req_url=URLS_lb, api_version=2):
|
|
|
"""
|
|
|
Do Query Qcloud api
|
|
|
:param params: 查询参数,dict类型,必须包含 Action
|
...
|
...
|
@@ -39,15 +39,26 @@ class QcloudApi: |
|
|
print("must provide [Action] params !")
|
|
|
raise ValueError(params)
|
|
|
|
|
|
public_params = {'RequestClient': QcloudApi.Version, 'limit': 100, 'Region': QcloudApi.Region, 'Timestamp': int(time.time()), 'Nonce': QcloudApi.generate_nonce(), 'SecretId': self.secretId}
|
|
|
# merge params
|
|
|
params = dict(params, **public_params)
|
|
|
# signature
|
|
|
sign = self.signature(params, req_url)
|
|
|
params['Signature'] = sign
|
|
|
response_dict = requests.post("https://" + req_url, data=params).json()
|
|
|
if api_version == 2:
|
|
|
public_params = {'RequestClient': QcloudApi.Version, 'limit': 100, 'Region': QcloudApi.Region, 'Timestamp': int(time.time()), 'Nonce': QcloudApi.generate_nonce(), 'SecretId': self.secretId}
|
|
|
# merge params
|
|
|
params = dict(params, **public_params)
|
|
|
# signature
|
|
|
sign = self.signature(params, req_url)
|
|
|
params['Signature'] = sign
|
|
|
response_dict = requests.post("https://" + req_url, data=params).json()
|
|
|
elif api_version == 3:
|
|
|
public_params = {'Region': QcloudApi.Region, 'Timestamp': int(time.time()), 'Nonce': QcloudApi.generate_nonce(), 'Version': '2017-03-12', 'SecretId': self.secretId, 'SignatureMethod':'HmacSHA1'}
|
|
|
# merge params
|
|
|
params = dict(params, **public_params)
|
|
|
# signature
|
|
|
req_url = req_url.rstrip('/')+'/'
|
|
|
sign = self.signature(params, req_url)
|
|
|
params['Signature'] = sign
|
|
|
response_dict = requests.post("https://" + req_url, data=params).json()
|
|
|
|
|
|
print("call qcloud api success. params:\n %s \n" % (params))
|
|
|
print json.dumps(response_dict, indent=4, sort_keys=True)
|
|
|
#print json.dumps(response_dict, indent=4, sort_keys=True)
|
|
|
return response_dict
|
|
|
|
|
|
@staticmethod
|
...
|
...
|
@@ -67,6 +78,7 @@ class QcloudApi: |
|
|
kv_pairs.append('%s=%s' % (key, request_params[key]))
|
|
|
str_req = '&'.join(kv_pairs)
|
|
|
str_full = 'POST%s?%s' % (url, str_req)
|
|
|
print 'str_full',str_full
|
|
|
hmac_sign = hmac.new(bytes(self.secretKey), bytes(str_full), hashlib.sha1).digest()
|
|
|
return base64.b64encode(hmac_sign).decode()
|
|
|
|
...
|
...
|
|