Skip to content

Commit 2bb083b

Browse files
committed
[Update] Update Version 1.2.2.3
Refactoring source codes
1 parent 17e3547 commit 2bb083b

File tree

4 files changed

+75
-36
lines changed

4 files changed

+75
-36
lines changed

upbit/authentication.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
from bravado.requests_client import Authenticator
99

1010

11-
MAPPER = 'swg_mapper.json'
12-
QUERY_PARAMS = ['uuids', 'txids', 'identifiers', 'states']
11+
class APIKeyAuthenticator(Authenticator):
1312

13+
MAPPER = "swg_mapper.json"
14+
QUERY_PARAMS = tuple("uuids", "txids", "identifiers", "states")
1415

15-
class APIKeyAuthenticator(Authenticator):
1616

1717
def __init__(
1818
self,
@@ -23,23 +23,26 @@ def __init__(
2323

2424
super(APIKeyAuthenticator, self).__init__(host)
2525

26-
self.host = host
26+
self.host = host
2727
self.access_key = access_key
2828
self.secret_key = secret_key
2929

30+
3031
def matches(self, url):
31-
return MAPPER not in url
32+
return APIKeyAuthenticator.MAPPER not in url
33+
3234

3335
def apply(self, request):
3436
payload = self.generate_payload(request)
3537

36-
request.headers['User-Agent' ] = "ujhin's Upbit SDKs"
37-
request.headers['Accept-Encoding'] = 'gzip, deflate'
38-
request.headers['Accept' ] = '*/*'
39-
request.headers['Connection' ] = 'keep-alive'
40-
request.headers['Authorization' ] = payload
38+
request.headers["User-Agent" ] = "ujhin's Upbit SDKs"
39+
request.headers["Accept-Encoding"] = "gzip, deflate"
40+
request.headers["Accept" ] = "*/*"
41+
request.headers["Connection" ] = "keep-alive"
42+
request.headers["Authorization" ] = payload
4143
return request
4244

45+
4346
def generate_payload(self, request):
4447
params = request.params
4548
data = request.data
@@ -57,20 +60,21 @@ def generate_payload(self, request):
5760
sha512.update(query.encode())
5861
query_hash = sha512.hexdigest()
5962

60-
payload['query_hash' ] = query_hash
61-
payload['query_hash_alg'] = 'SHA512'
63+
payload["query_hash" ] = query_hash
64+
payload["query_hash_alg"] = "SHA512"
6265

6366
jwt_token = jwt.encode(payload, self.secret_key)
6467
authorize_token = f"Bearer {jwt_token}"
6568
return authorize_token
6669

70+
6771
def generate_query(self, params):
6872
query = urlencode({
6973
k: v
7074
for k, v in params.items()
71-
if k not in QUERY_PARAMS
75+
if k.lower() not in APIKeyAuthenticator.QUERY_PARAMS
7276
})
73-
for query_param in QUERY_PARAMS:
77+
for query_param in APIKeyAuthenticator.QUERY_PARAMS:
7478
if params.get(query_param):
7579
param = params.pop(query_param)
7680
params[f"{query_param}[]"] = param

upbit/client.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,41 @@ def __init__(
3131
**kwargs
3232
).SWGClient
3333

34-
self.APIKey = models.APIKey(self.__client)
35-
self.Account = models.Account(self.__client)
36-
self.Candle = models.Candle(self.__client)
37-
self.Deposit = models.Deposit(self.__client)
38-
self.Market = models.Market(self.__client)
39-
self.Order = models.Order(self.__client)
40-
self.Trade = models.Trade(self.__client)
41-
self.Withdraw = models.Withdraw(self.__client)
34+
self.__APIKey = models.APIKey(self.__client)
35+
self.__Account = models.Account(self.__client)
36+
self.__Candle = models.Candle(self.__client)
37+
self.__Deposit = models.Deposit(self.__client)
38+
self.__Market = models.Market(self.__client)
39+
self.__Order = models.Order(self.__client)
40+
self.__Trade = models.Trade(self.__client)
41+
self.__Withdraw = models.Withdraw(self.__client)
42+
43+
44+
@property
45+
def APIKey(self):
46+
return self.__APIKey
47+
@property
48+
def Account(self):
49+
return self.__Account
50+
@property
51+
def Candle(self):
52+
return self.__Candle
53+
@property
54+
def Deposit(self):
55+
return self.__Deposit
56+
@property
57+
def Market(self):
58+
return self.__Market
59+
@property
60+
def Order(self):
61+
return self.__Order
62+
@property
63+
def Trade(self):
64+
return self.__Trade
65+
@property
66+
def Withdraw(self):
67+
return self.__Withdraw
68+
4269

4370
def __str__(self):
4471
return self.__repr__()

upbit/pkginfo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _get_versions(package_name):
2929
PACKAGE_NAME = 'upbit-client'
3030

3131
OPEN_API_VERSION = '1.2.2'
32-
CURRENT_VERSION = OPEN_API_VERSION+'.2'
32+
CURRENT_VERSION = OPEN_API_VERSION+'.3'
3333

3434
RELEASED_VERSION = _get_versions(PACKAGE_NAME)
3535
LATEST_VERSION = RELEASED_VERSION[0]

upbit/websocket.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
from typing import Union, List
77

88

9-
WEBSOCKET_URI = "wss://api.upbit.com/websocket/v1"
10-
11-
FIELD_TYPES = ['ticker', 'trade', 'orderbook']
12-
FIELD_FORMATS = ['SIMPLE', 'DEFAULT']
13-
14-
159
class UpbitWebSocket:
1610
"""
1711
Upbit WebSocket Client
@@ -26,20 +20,26 @@ class UpbitWebSocket:
2620
- Official Support Email: [email protected]
2721
"""
2822

23+
WEBSOCKET_URI = "wss://api.upbit.com/websocket/v1"
24+
FIELD_TYPES = tuple("ticker", "trade", "orderbook")
25+
FIELD_FORMATS = tuple("SIMPLE", "DEFAULT")
26+
27+
2928
def __init__(
3029
self,
3130
uri: Union[str] = None,
3231
ping_interval: Union[int, float] = None,
3332
ping_timeout: Union[int, float] = None
3433
):
3534

36-
self.__uri = uri if uri else WEBSOCKET_URI
35+
self.__uri = uri if uri else UpbitWebSocket.WEBSOCKET_URI
3736
self.__conn = None
3837
self.connect(
3938
ping_interval=ping_interval,
4039
ping_timeout=ping_timeout
4140
)
4241

42+
4343
@property
4444
def URI(self):
4545
return self.__uri
@@ -56,6 +56,7 @@ def Connection(self):
5656
def Connection(self, conn):
5757
self.__conn = conn
5858

59+
5960
def connect(
6061
self,
6162
ping_interval: Union[int, float] = None,
@@ -75,14 +76,17 @@ def connect(
7576
ping_timeout=ping_timeout
7677
)
7778

78-
async def ping(self):
79+
80+
async def ping(self, decode: str = "utf8"):
7981
"""
8082
Client to Server PING
8183
"""
8284
async with self as conn:
83-
await conn.send('PING')
85+
await conn.send("PING")
8486
recv = await conn.recv()
85-
return json.loads(recv)
87+
pong = recv.decode(decode)
88+
return json.loads(pong)
89+
8690

8791
@staticmethod
8892
def generate_orderbook_codes(
@@ -104,6 +108,7 @@ def generate_orderbook_codes(
104108
] if counts else currencies
105109
return codes
106110

111+
107112
@staticmethod
108113
def generate_type_field(
109114
type: str,
@@ -127,8 +132,9 @@ def generate_type_field(
127132

128133
field = {}
129134

130-
if type in FIELD_TYPES:
131-
field["type"] = type
135+
t = type.lower()
136+
if t in UpbitWebSocket.FIELD_TYPES:
137+
field["type"] = t
132138
else:
133139
raise ValueError("'type' is not available")
134140

@@ -142,6 +148,7 @@ def generate_type_field(
142148

143149
return field
144150

151+
145152
@staticmethod
146153
def generate_payload(
147154
type_fields: Union[List[dict]],
@@ -167,11 +174,12 @@ def generate_payload(
167174
payload.extend(type_fields)
168175

169176
fmt = format.upper()
170-
fmt = fmt if fmt in FIELD_FORMATS else 'DEFAULT'
177+
fmt = fmt if fmt in UpbitWebSocket.FIELD_FORMATS else "DEFAULT"
171178
payload.append({"format": fmt})
172179

173180
return json.dumps(payload)
174181

182+
175183
async def __aenter__(self):
176184
return await self.Connection.__aenter__()
177185

0 commit comments

Comments
 (0)