-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathutils.py
101 lines (86 loc) · 2.52 KB
/
utils.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
import typing as t
class HTTPFutureExtractor:
@staticmethod
def remaining_request(headers: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]:
"""
Check Request limit times
Please read the official Upbit Client document.
Documents: https://ujhin.github.io/upbit-client-docs/
"""
remaining = headers['Remaining-Req']
return {
k: v
for k, v in [
param.split('=')
for param
in remaining.split('; ')
]
}
@staticmethod
def future_extraction(http_future) -> t.Dict[str, t.Any]:
resp = http_future.future.result()
remaining = HTTPFutureExtractor.remaining_request(resp.headers)
# resp.raise_for_status()
result = {
"remaining_request": remaining,
"response": {
"url": resp.url,
"headers": resp.headers,
"status_code": resp.status_code,
"reason": resp.reason,
"text": resp.text,
"content": resp.content,
"ok": resp.ok
}
}
try:
result['result'] = resp.json()
except:
result['result'] = {
"error": {
"message": resp.text,
"name": resp.reason
}
}
finally:
return result
class Validator:
@staticmethod
def validate_price(price: t.Union[int, float, str]) -> float:
"""
Please read the official Upbit Client document.
Documents: https://ujhin.github.io/upbit-client-docs/
[Order price units]
~10 : 0.01
~100 : 0.1
~1,000 : 1
~10,000 : 5
~100,000 : 10
~500,000 : 50
~1,000,000 : 100
~2,000,000 : 500
+2,000,000 : 1,000
"""
price = float(price)
unit = 0.01
if price <= 10:
unit = 0.01
elif price <= 100:
unit = 0.1
elif price <= 1_000:
unit = 1
elif price <= 10_000:
unit = 5
elif price <= 100_000:
unit = 10
elif price <= 500_000:
unit = 50
elif price <= 1_000_000:
unit = 100
elif price <= 2_000_000:
unit = 500
elif price > 2_000_000:
unit = 1000
else:
raise ValueError('Invalid Price')
return price - (price % unit)