Skip to content
This repository was archived by the owner on Jan 1, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions ftx/wsapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from json import JSONDecodeError
from typing import Optional

import websockets
from websockets.legacy.client import WebSocketClientProtocol
from websocket import WebSocket

from ftx.fifo import AsyncFifoQueue

Expand All @@ -18,7 +17,7 @@ class FtxWebSocketClient:

See: https://docs.ftx.com/#websocket-api
"""
_ws: Optional[WebSocketClientProtocol]
_ws: Optional[WebSocket]

def __init__(self,
api_key: Optional[str] = None,
Expand Down Expand Up @@ -46,12 +45,25 @@ def __init__(self,
self.socket_url = socket_url
self._queue = AsyncFifoQueue(maxsize=queue_size)

async def _connect(self, url):
await asyncio.get_event_loop().run_in_executor(None, lambda: self._ws.connect(url))

async def _close(self):
await asyncio.get_event_loop().run_in_executor(None, self._ws.close)

async def _send(self, data):
await asyncio.get_event_loop().run_in_executor(None, lambda: self._ws.send(data))

async def _recv(self):
return await asyncio.get_event_loop().run_in_executor(None, self._ws.recv)

async def connect(self):
"""
Connect to the websocket
:return:
"""
self._ws = await websockets.connect(self.socket_url)
self._ws = WebSocket()
await self._connect(self.socket_url)
self._log('websocket connected')
asyncio.create_task(self._loop_fn())
asyncio.create_task(self._ping_loop_fn())
Expand All @@ -61,16 +73,16 @@ async def disconnect(self):
Disconnects the websocket if it's open
:return:
"""
if self._ws and self._ws.open:
await self._ws.close()
if self._ws and self._ws.connected:
await self._close()
self._ws = None

@property
def connected(self):
"""
True if the websocket is connected and open
"""
return self._ws and self._ws.open
return self._ws and self._ws.connected

@property
def messages_dropped(self):
Expand Down Expand Up @@ -103,7 +115,7 @@ async def send_message(self, msg):
:param msg: the message
"""
self._log('->', msg)
await self._ws.send(json.dumps(msg))
await self._send(json.dumps(msg))

async def _ping_loop_fn(self):
"""
Expand All @@ -123,7 +135,7 @@ async def _loop_fn(self):
"""
while self.connected:
try:
msg = await self._ws.recv()
msg = await self._recv()
try:
self._on_message(json.loads(msg))
except JSONDecodeError:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ciso8601
requests
websockets
websocket-client
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
author_email='thomgabriel@protonmail.com',
url='https://github.com/quan-digital/ftx/tree/v1.2',
download_url='https://github.com/quan-digital/ftx/archive/v1.2.tar.gz',
install_requires=['requests', 'ciso8601'],
install_requires=['requests', 'ciso8601', 'websocket-client'],
packages=find_packages(),
keywords=[
'ftx', 'bitcoin', 'crypto-api', 'api-connector', 'exchange-api',
Expand Down