-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring source codes
- Loading branch information
Showing
4 changed files
with
75 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,6 @@ | |
from typing import Union, List | ||
|
||
|
||
WEBSOCKET_URI = "wss://api.upbit.com/websocket/v1" | ||
|
||
FIELD_TYPES = ['ticker', 'trade', 'orderbook'] | ||
FIELD_FORMATS = ['SIMPLE', 'DEFAULT'] | ||
|
||
|
||
class UpbitWebSocket: | ||
""" | ||
Upbit WebSocket Client | ||
|
@@ -26,20 +20,26 @@ class UpbitWebSocket: | |
- Official Support Email: [email protected] | ||
""" | ||
|
||
WEBSOCKET_URI = "wss://api.upbit.com/websocket/v1" | ||
FIELD_TYPES = tuple("ticker", "trade", "orderbook") | ||
FIELD_FORMATS = tuple("SIMPLE", "DEFAULT") | ||
|
||
|
||
def __init__( | ||
self, | ||
uri: Union[str] = None, | ||
ping_interval: Union[int, float] = None, | ||
ping_timeout: Union[int, float] = None | ||
): | ||
|
||
self.__uri = uri if uri else WEBSOCKET_URI | ||
self.__uri = uri if uri else UpbitWebSocket.WEBSOCKET_URI | ||
self.__conn = None | ||
self.connect( | ||
ping_interval=ping_interval, | ||
ping_timeout=ping_timeout | ||
) | ||
|
||
|
||
@property | ||
def URI(self): | ||
return self.__uri | ||
|
@@ -56,6 +56,7 @@ def Connection(self): | |
def Connection(self, conn): | ||
self.__conn = conn | ||
|
||
|
||
def connect( | ||
self, | ||
ping_interval: Union[int, float] = None, | ||
|
@@ -75,14 +76,17 @@ def connect( | |
ping_timeout=ping_timeout | ||
) | ||
|
||
async def ping(self): | ||
|
||
async def ping(self, decode: str = "utf8"): | ||
""" | ||
Client to Server PING | ||
""" | ||
async with self as conn: | ||
await conn.send('PING') | ||
await conn.send("PING") | ||
recv = await conn.recv() | ||
return json.loads(recv) | ||
pong = recv.decode(decode) | ||
return json.loads(pong) | ||
|
||
|
||
@staticmethod | ||
def generate_orderbook_codes( | ||
|
@@ -104,6 +108,7 @@ def generate_orderbook_codes( | |
] if counts else currencies | ||
return codes | ||
|
||
|
||
@staticmethod | ||
def generate_type_field( | ||
type: str, | ||
|
@@ -127,8 +132,9 @@ def generate_type_field( | |
|
||
field = {} | ||
|
||
if type in FIELD_TYPES: | ||
field["type"] = type | ||
t = type.lower() | ||
if t in UpbitWebSocket.FIELD_TYPES: | ||
field["type"] = t | ||
else: | ||
raise ValueError("'type' is not available") | ||
|
||
|
@@ -142,6 +148,7 @@ def generate_type_field( | |
|
||
return field | ||
|
||
|
||
@staticmethod | ||
def generate_payload( | ||
type_fields: Union[List[dict]], | ||
|
@@ -167,11 +174,12 @@ def generate_payload( | |
payload.extend(type_fields) | ||
|
||
fmt = format.upper() | ||
fmt = fmt if fmt in FIELD_FORMATS else 'DEFAULT' | ||
fmt = fmt if fmt in UpbitWebSocket.FIELD_FORMATS else "DEFAULT" | ||
payload.append({"format": fmt}) | ||
|
||
return json.dumps(payload) | ||
|
||
|
||
async def __aenter__(self): | ||
return await self.Connection.__aenter__() | ||
|
||
|