-
Notifications
You must be signed in to change notification settings - Fork 17
/
CoinexAPI.py
223 lines (178 loc) · 6.72 KB
/
CoinexAPI.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Copyright (C) 2013, Maxime Biais <[email protected]>
import config
import time
import hashlib
import json as complex_json
import requests
import logging
import time
class RequestClient(object):
__headers = {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
}
def __init__(self, headers={}):
self.access_id = config.coinex_api_id
self.secret_key = config.coinex_api_key
self.headers = self.__headers
self.headers.update(headers)
@staticmethod
def get_sign(params, secret_key):
sort_params = sorted(params)
data = []
for item in sort_params:
data.append(item + '=' + str(params[item]))
str_params = "{0}&secret_key={1}".format('&'.join(data), secret_key)
token = hashlib.md5(str.encode(str_params)).hexdigest().upper()
return token
def set_authorization(self, params):
params['access_id'] = self.access_id
params['tonce'] = int(time.time()*1000)
self.headers['AUTHORIZATION'] = self.get_sign(params, self.secret_key)
def _request(self, method, url, params={}, data='', json={}):
method = method.upper()
if method == 'GET':
self.set_authorization(params)
result = requests.request('GET', url, params=params, headers=self.headers)
else:
if data:
json.update(complex_json.loads(data))
self.set_authorization(json)
result = requests.request(method, url, json=json, headers=self.headers)
return result
def request(self, method, url, params={}, data='', json={}):
try:
return self._request(method,url,params,data,json)
except Exception as e:
logging.error(e)
time.sleep(0.5)
return self._request(method,url,params,data,json)
class PrivateAPI(object):
def __init__(self):
super().__init__()
def buy(self, amount, price, market):
"""Create a buy limit order"""
request_client = RequestClient()
data = {
"amount": "%.8f" % (amount),
"price": "%.8f" % (price),
"type": "buy",
"market": market
}
response = request_client.request(
'POST',
'https://api.coinex.com/v1/order/limit',
json=data,
)
if response != None:
data = complex_json.loads(response.text)
if data["code"] != 0:
raise Exception(data["message"])
elif "data" in data:
return data
def sell(self, amount, price, market):
"""Create a sell limit order"""
request_client = RequestClient()
data = {
"amount": "%.8f" % (amount),
"price": "%.8f" % (price),
"type": "sell",
"market": market
}
response = request_client.request(
'POST',
'https://api.coinex.com/v1/order/limit',
json=data,
)
if response != None:
data = complex_json.loads(response.text)
if data["code"] != 0:
raise Exception(data["message"])
elif "data" in data:
return data
def get_balances(self):
"""Get balance"""
request_client = RequestClient()
response = request_client.request('GET', 'https://api.coinex.com/v1/balance/')
if response != None:
data = complex_json.loads(response.text)
if data["code"] != 0:
raise Exception(data["message"])
elif "data" in data:
return data
else:
raise Exception("Critical error no get_balances")
def get_difficulty(self):
"""Get Difficulty"""
request_client = RequestClient()
response = request_client.request('GET', 'https://api.coinex.com/v1/order/mining/difficulty')
if response != None:
data = complex_json.loads(response.text)
if data["code"] != 0:
raise Exception(data["message"])
elif "data" in data:
return data
else:
raise Exception("Critical error no get_difficulty")
def get_latest_transaction(self,market):
request_client = RequestClient()
response = request_client.request('GET', 'https://api.coinex.com/v1/market/deals?market='+market)
if response != None:
data = complex_json.loads(response.text)
if data["code"] != 0:
raise Exception(data["message"])
elif "data" in data:
return data
else:
raise Exception("Critical error no get_latest_transaction")
def get_ticker(self,market):
request_client = RequestClient()
response = request_client.request('GET', 'https://api.coinex.com/v1/market/ticker?market='+market)
if response != None:
data = complex_json.loads(response.text)
if data["code"] != 0:
raise Exception(data["message"])
elif "data" in data:
return data
else:
raise Exception("Critical error no get_ticker")
def get_orders(self,market):
request_client = RequestClient()
data = {
"page": 1,
"limit": 100,
"market": market
}
response = request_client.request(
'GET',
'https://api.coinex.com/v1/order/pending',
params=data,
)
if response != None:
data = complex_json.loads(response.text)
if data["code"] != 0:
raise Exception(data["message"])
elif "data" in data:
return data
else:
raise Exception("Critical error no get_orders")
def get_order(self,market,_id):
request_client = RequestClient()
data = {
"id": _id,
"market": market
}
response = request_client.request(
'GET',
'https://api.coinex.com/v1/order',
params=data,
)
if response != None:
data = complex_json.loads(response.text)
if data["code"] != 0:
raise Exception(data["message"])
elif "data" in data:
return data
else:
raise Exception("Critical error no get_order")