-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsigned_requests.py
39 lines (25 loc) · 1018 Bytes
/
signed_requests.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
import hashlib
import hmac
import time
from urllib.parse import urlencode
import requests
from credentials import API_KEY, SECRET_KEY
def send_signed_request(http_method, url, payload=None):
payload = payload or {}
query = urlencode(payload, True)
if query:
query = '{}×tamp={}'.format(query, _get_timestamp())
else:
query = 'timestamp={}'.format(_get_timestamp())
url = url + '?' + query + '&signature=' + _hashing(query)
params = {'url': url, 'params': {}}
response = _dispatch_request(http_method)(**params)
return response.json()
def _get_timestamp():
return int(time.time() * 1000)
def _hashing(query):
return hmac.new(SECRET_KEY.encode('utf-8'), query.encode('utf-8'), hashlib.sha256).hexdigest()
def _dispatch_request(http_method):
session = requests.Session()
session.headers.update({'Content-Type': 'application/json;charset=utf-8', 'X-MBX-APIKEY': API_KEY})
return {'GET': session.get, 'POST': session.post}[http_method]