-
Notifications
You must be signed in to change notification settings - Fork 10
/
api_functions.py
126 lines (97 loc) · 4.88 KB
/
api_functions.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
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
import hashlib
import hmac
import json
import time
import requests
from settings import *
def get_positions():
# BTC POSITIONS
tstamp = str(int(time.time()) * 1000)
url = "/api/v2/private/get_positions"
# nonce should be a random string, static nonce for simple illustration here
nonce = "1234567890"
# body of the POST request, JSON-RPC string
body = "{\"jsonrpc\": \"2.0\",\"id\": \"8003\",\"method\": \"private/get_positions\",\"params\": {\"currency\": \"BTC\"}}"
# signature for the request
request_data = "POST" + "\n" + url + "\n" + body + "\n"
base_signature_string = tstamp + "\n" + nonce + "\n" + request_data
byte_key = api_client_secret.encode()
message = base_signature_string.encode()
sig = hmac.new(byte_key, message, hashlib.sha256).hexdigest()
authorization = "deri-hmac-sha256 id=" + api_client_id + ",ts=" + tstamp + ",sig=" + sig + ",nonce=" + nonce
headers = {"Authorization": authorization}
# send HTTPS POST request
json_response = requests.post((api_exchange_address + url + "?"), headers=headers, data=body)
response_dict = json.loads(json_response.content)
positions_list_BTC = response_dict["result"]
# ETH POSITIONS
tstamp = str(int(time.time()) * 1000)
url = "/api/v2/private/get_positions"
# nonce should be a random string, static nonce for simple illustration here
nonce = "1234567891"
# body of the POST request, JSON-RPC string
body = "{\"jsonrpc\": \"2.0\",\"id\": \"8003\",\"method\": \"private/get_positions\",\"params\": {\"currency\": \"ETH\"}}"
# signature for the request
request_data = "POST" + "\n" + url + "\n" + body + "\n"
base_signature_string = tstamp + "\n" + nonce + "\n" + request_data
byte_key = api_client_secret.encode()
message = base_signature_string.encode()
sig = hmac.new(byte_key, message, hashlib.sha256).hexdigest()
authorization = "deri-hmac-sha256 id=" + api_client_id + ",ts=" + tstamp + ",sig=" + sig + ",nonce=" + nonce
headers = {"Authorization": authorization}
# send HTTPS POST request
json_response = requests.post((api_exchange_address + url + "?"), headers=headers, data=body)
response_dict = json.loads(json_response.content)
positions_list_ETH = response_dict["result"]
# SOL POSITIONS
tstamp = str(int(time.time()) * 1000)
url = "/api/v2/private/get_positions"
# nonce should be a random string, static nonce for simple illustration here
nonce = "1234567892"
# body of the POST request, JSON-RPC string
body = "{\"jsonrpc\": \"2.0\",\"id\": \"8003\",\"method\": \"private/get_positions\",\"params\": {\"currency\": \"SOL\"}}"
# signature for the request
request_data = "POST" + "\n" + url + "\n" + body + "\n"
base_signature_string = tstamp + "\n" + nonce + "\n" + request_data
byte_key = api_client_secret.encode()
message = base_signature_string.encode()
sig = hmac.new(byte_key, message, hashlib.sha256).hexdigest()
authorization = "deri-hmac-sha256 id=" + api_client_id + ",ts=" + tstamp + ",sig=" + sig + ",nonce=" + nonce
headers = {"Authorization": authorization}
# send HTTPS POST request
json_response = requests.post((api_exchange_address + url + "?"), headers=headers, data=body)
response_dict = json.loads(json_response.content)
positions_list_SOL = response_dict["result"]
return positions_list_BTC, positions_list_ETH, positions_list_SOL
def get_instrument(instrument):
url = "/api/v2/public/get_instrument"
parameters = {'instrument_name': instrument}
# send HTTPS GET request
json_response = requests.get((api_exchange_address + url + "?"), params=parameters)
response_dict = json.loads(json_response.content)
instrument_details = response_dict["result"]
return instrument_details
def get_instruments(currency):
url = "/api/v2/public/get_instruments"
parameters = {'currency': currency}
# send HTTPS GET request
json_response = requests.get((api_exchange_address + url + "?"), params=parameters)
response_dict = json.loads(json_response.content)
instrument_list = response_dict["result"]
return instrument_list
def get_book_summary_by_instrument(instrument):
url = "/api/v2/public/get_book_summary_by_instrument"
parameters = {'instrument_name': instrument}
# send HTTPS GET request
json_response = requests.get((api_exchange_address + url + "?"), params=parameters)
response_dict = json.loads(json_response.content)
instrument_details = response_dict["result"]
return instrument_details
def get_order_book(instrument):
url = "/api/v2/public/get_order_book"
parameters = {'instrument_name': instrument}
# send HTTPS GET request
json_response = requests.get((api_exchange_address + url + "?"), params=parameters)
response_dict = json.loads(json_response.content)
instrument_details = response_dict["result"]
return instrument_details