forked from Dragonexio/OpenApi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
181 lines (135 loc) · 5.17 KB
/
base.py
File metadata and controls
181 lines (135 loc) · 5.17 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
# -*- coding: utf-8 -*-
import os
import base64
import datetime
import hashlib
import hmac
import json
import logging
import requests
from examples.python3.error_codes import CODE_OK, CODE_SERVER_ERROR, CODE_INVALID_PARAMS
logging.basicConfig(level=logging.DEBUG)
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
class Base(object):
def __init__(self, access_key, secret_key, host):
self.access_key = access_key
self.secret_key = secret_key
self.host = host
@staticmethod
def __format_response(response: requests.Response):
status_code = response.status_code
if status_code != 200:
logging.debug('http request failed, status_code={}'.format(response))
return HTTPResponse(ok=False, body=response.text)
return HTTPResponse(ok=True, body=response.text)
def url(self, path):
return '{}{}'.format(self.host, path)
def get(self, path, params=None, headers=None):
url = self.url(path)
if headers is None:
headers = self.default_headers(method='GET', path=path)
rsp = requests.get(url=url, params=params, headers=headers)
return self.__format_response(response=rsp)
def post(self, path, data=None, headers=None):
url = self.url(path)
if isinstance(data, dict):
data = json.dumps(data)
if headers is None:
headers = self.default_headers(method='POST', path=path, data=data)
rsp = requests.post(url=url, data=data, headers=headers)
return self.__format_response(response=rsp)
def default_headers(self, method, path, data=None):
headers = {
'Date': datetime.datetime.utcnow().strftime(GMT_FORMAT),
'Content-Type': 'application/json',
'token': self.token,
'dragonex-atruth': 'DragonExIsTheBest' # 此字段非必须
}
if data is not None:
headers.update({'Content-Sha1': self.sha1(data)})
auth = self.auth(method=method, path=path, headers=headers)
headers.update({"Auth": auth})
return headers
@staticmethod
def sha1(data):
if not isinstance(data, (bytes, bytearray)):
data = bytes(data, encoding='utf-8')
h = hashlib.sha1()
h.update(data)
return h.hexdigest()
def auth(self, method, path, headers):
new_headers = {}
for k, v in headers.items():
new_headers[k.lower()] = v
content_md5 = new_headers.get('content-sha1', '')
content_type = new_headers.get('content-type', '')
date = new_headers.get('date', '')
dra_headers = ['{}:{}'.format(k, v) for k, v in new_headers.items() if k.startswith('dragonex-')]
dra_headers.sort()
can_headers = '{}\n'.format('\n'.join(dra_headers)) if dra_headers else ''
str_to_sign = '\n'.join([method.upper(), content_md5, content_type, date, can_headers]) + path
sign = self.sign(str_to_sign, self.secret_key)
auth = '{}:{}'.format(self.access_key, sign)
return auth
@staticmethod
def sign(string_to_sign, secret_key):
h = hmac.new(bytes(secret_key, encoding='utf-8'), bytes(string_to_sign, encoding='utf-8'),
digestmod=hashlib.sha1).digest()
return base64.b64encode(h).decode('utf-8')
@property
def token_file(self):
return os.path.join(os.curdir, '{}.token'.format(self.access_key))
@property
def token(self):
if not os.path.exists(self.token_file):
return ''
with open(self.token_file, 'r') as f:
return f.read()
@token.setter
def token(self, token):
token = token.decode('utf-8') if isinstance(token, bytes) else token
with open(self.token_file, 'w') as f:
f.write(token)
class HTTPResponse(object):
def __init__(self, ok, body):
if isinstance(body, dict):
d = body
elif isinstance(body, (bytes, bytearray, str)):
body = body.decode('utf-8') if isinstance(body, (bytes, bytearray)) else body
try:
d = json.loads(body)
except Exception as e:
logging.error('err={}, body={}'.format(e, body))
d = {'code': CODE_SERVER_ERROR, 'msg': 'error return', 'data': {}}
else:
d = {'code': CODE_SERVER_ERROR, 'msg': 'error return', 'data': {}}
self._code = d.get('code', 0)
self._msg = d.get('msg')
self._data = d.get('data')
self._ok = True if self.code == CODE_OK else False
@property
def ok(self):
return self._ok
@ok.setter
def ok(self, ok):
self._ok = ok
@property
def code(self):
return self._code
@code.setter
def code(self, code):
self._code = code
@property
def msg(self):
return self._msg
@msg.setter
def msg(self, msg):
self._msg = msg
@property
def data(self):
return self._data
@data.setter
def data(self, data):
self._data = data
InvalidParamsHttpResponse = HTTPResponse(ok=False,
body={'code': CODE_INVALID_PARAMS, 'msg': 'invalid params', 'data': {}})