-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi_client.py
More file actions
40 lines (33 loc) · 1.1 KB
/
api_client.py
File metadata and controls
40 lines (33 loc) · 1.1 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
import time
import hmac
import hashlib
import requests
import json
class APIClient():
def __init__(self, api_key, shop_id):
self.api_key = api_key
self.shop_id = shop_id
def _send_http_request(self, api_method, data):
data = {
"shop_id": self.shop_id,
**data
}
# По умолчанию питон json.dumps добавляет пробелы в JSON
# Но php метод json_encode их не добавляет.
jsoned_data = json.dumps(data, separators=(',', ':'))
sign = hmac.digest(
self.api_key.encode(),
jsoned_data.encode(),
'sha256'
).hex()
resp = requests.post(f"https://tegro.money/api/{api_method}/",
data=jsoned_data,
headers={
"Authorization": f'Bearer {sign}',
}
)
return resp.json()
def api_call(self, api_method, params={}, nonce=None):
if nonce == None:
nonce = int(time.time())
return self._send_http_request(api_method, {"nonce": nonce, **params})