53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
# -*- coding: utf8 -*-
|
|
# author:tan
|
|
# date:2018-12-10
|
|
|
|
"""
|
|
腾讯云api通用类
|
|
"""
|
|
|
|
from ops.qcloud_sign import QcSign
|
|
import requests
|
|
import json
|
|
import random
|
|
import time
|
|
import os
|
|
|
|
|
|
# 腾讯云api操作
|
|
class QcApi(object):
|
|
def __init__(self, endpoint, data, v='3.0'):
|
|
self.secret_id = os.environ.get('SECRET_ID')
|
|
self.secret_key = os.environ.get('SECRET_KEY')
|
|
self.endpoint = endpoint
|
|
self.method = "POST"
|
|
self.version = v
|
|
|
|
self.data = dict()
|
|
for i in data:
|
|
self.data[i] = data.get(i)
|
|
|
|
# 公共参数
|
|
if self.version == '2.0':
|
|
if not data.get('Region'):
|
|
self.data['Region'] = 'bj'
|
|
else:
|
|
if not data.get('Region'):
|
|
self.data['Region'] = 'ap-beijing'
|
|
if self.version != '3.0':
|
|
self.data['Version'] = self.version
|
|
else:
|
|
self.data['Version'] = '2017-03-12'
|
|
self.data['SecretId'] = self.secret_id
|
|
self.data['Timestamp'] = int(time.time()) # 时间戳
|
|
self.data['Nonce'] = random.randint(1000, 1000000) # 随机正整数
|
|
sign = QcSign(self.secret_key, self.method, self.endpoint, self.data, self.version)
|
|
self.data['Signature'] = sign.sign_str()
|
|
|
|
def workflow(self):
|
|
try:
|
|
r = requests.post('https://' + self.endpoint, data=self.data, timeout=10)
|
|
rel = json.loads(r.content.decode('utf-8'))
|
|
return rel
|
|
except:
|
|
return False |