66from 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-
159class 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