This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
request.py
80 lines (55 loc) · 2.52 KB
/
request.py
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
import requests
from urllib.parse import urljoin
from urllib.parse import urlparse
from aftership.hmac.hmac import Hmac
from aftership.signstring.signstring import SignString
from .const import AFTERSHIP_API_KEY, API_ENDPOINT, API_VERSION, AS_SIGNATURE_HMAC_SHA256, AS_API_KEY, CONTENT_TYPE, SIGNATURE_AES_HMAC_SHA256
from .util import get_api_key, get_api_secret
def build_request_url(path):
return urljoin(API_ENDPOINT, path)
def make_request(method, path, **kwargs):
url = build_request_url(path)
res = urlparse(url)
params = kwargs.get('params', None)
path = res.path
params_str = ""
if params:
params_str = '&'.join([str(key)+'='+str(value) for key, value in params.items()])
if not path.startswith("/"):
path = '/' + path
if len(params_str) > 0:
path = '{}?{}'.format(path, params_str)
signature_type = kwargs.pop('signature_type', None)
if signature_type is None:
return request_with_token(method, url, **kwargs)
body = kwargs.get('json', None)
content_type = None
if (method == "POST" or method == "PUT" or method == "PATCH") and body is not None:
content_type = CONTENT_TYPE
# if using SignString, you must use AS_API_KEY header
if signature_type == SIGNATURE_AES_HMAC_SHA256:
return request_with_aes_hmac256_signature(method, url, path, content_type, **kwargs)
return None
def request_with_token(method, url, **kwargs):
headers = kwargs.pop('headers', dict())
if headers.get(AFTERSHIP_API_KEY) is None and headers.get(AS_API_KEY) is None:
headers[AFTERSHIP_API_KEY] = get_api_key()
kwargs['headers'] = headers
return requests.request(method, url, **kwargs)
def request_with_aes_hmac256_signature(method, url, path, content_type, **kwargs):
headers = kwargs.pop('headers', dict())
if headers.get(AS_API_KEY, None) is None:
headers[AS_API_KEY] = get_api_key()
body = kwargs.get('json', None)
date, sign_string = gen_sign_string(method, path, body, headers, content_type)
hmac = Hmac(get_api_secret())
hmac_signature = hmac.hmac_signature(sign_string)
headers[AS_SIGNATURE_HMAC_SHA256] = hmac_signature
headers['Date'] = date
if content_type is not None:
headers["Content-Type"] = content_type
kwargs['headers'] = headers
return requests.request(method, url, **kwargs)
def gen_sign_string(method, path, body, headers, content_type):
s = SignString(headers[AS_API_KEY])
return s.gen_sign_string(method, path, body, headers, content_type)