-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfairdesk.py
72 lines (60 loc) · 2.78 KB
/
fairdesk.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
import requests
from os import getenv
import time
import hmac
import hashlib
import orjson
class Fairdesk:
def __init__(self):
self.base_url = "https://api.fairdesk.com"
self.api_key = getenv("FAIRDESK_API_KEY")
self.api_secret = getenv("FAIRDESK_API_SECRET")
def get_uid_info(self, uid, value=99):
if self.api_key is None or self.api_secret is None:
raise PermissionError("Authenticated endpoints require keys.")
payload = "/api/v1/private/account/partner-direct-user-deposit"
query = f"traderUid={uid}"
headers = self.generate_headers(payload, query)
r = requests.get(self.base_url + payload + f"?{query}", headers=headers)
try:
uid_json = orjson.loads(r.text)
if int(uid_json["data"]) >= value:
return True, int(uid_json["data"]), True
elif int(uid_json["data"]) == -9999:
return False, 0, False
except Exception as e:
print("Issue with processing the request response", e)
return False, 0, False
return False, 0, True
def generate_headers(self, payload, query, recv_window=10000):
# recvWindow, may be sent to specify the number of milliseconds after timestamp the request is valid for. If recvWindow is not sent, it defaults to 5000.
# Serious trading is about timing. Networks can be unstable and unreliable, which can lead to requests taking varying amounts of time to reach the servers. With recvWindow, you can specify that the request must be processed within a certain number of milliseconds or be rejected by the server.
timestamp = int(time.time() * 1000)
sign = self._auth(payload, query, recv_window, timestamp)
return {
"X-FAIRDESK-ACCESS-KEY": self.api_key,
"X-FAIRDESK-REQUEST-SIGNATURE": sign,
"X-FAIRDESK-REQUEST-EXPIRY": str(timestamp + recv_window),
"Content-Type": "application/json",
"Connection": "keep-alive",
}
def _auth(self, payload, query, recv_window, timestamp):
"""
Generates authentication signature per Fairdesk API specifications.
"""
def generate_hmac():
hash = hmac.new(
bytes(self.api_secret, "utf-8"),
param_str.encode("utf-8"),
hashlib.sha256,
)
return hash.hexdigest()
if self.api_key is None or self.api_secret is None:
raise PermissionError("Authenticated endpoints require keys.")
param_str = payload + query + str(timestamp + recv_window)
return generate_hmac()
def get_exchange_name(self):
return "Fairdesk"
if __name__ == "__main__":
fairdesk = Fairdesk()
print(fairdesk.get_uid_info("1234567890"))