-
-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring manager for ease testing #305
Refactoring manager for ease testing #305
Conversation
Hello! First of all thanks for the PR, the code looks very neat and valid. Is it support for Apache MockServer? https://www.mock-server.com/ I didn't know that, I do similar with a SSH redirect: https://www.ssh.com/academy/ssh/tunneling-example Could you provide more information for other users on how to use it with this lib? What I noticed, REST is not sent to the proxy: Thank you! |
Initially, I tried to raise import asyncio
import json
import logging
import random
from datetime import datetime
import websockets
from binance import Client
logging.basicConfig(level=logging.INFO,
format="{asctime} {msecs} | {levelname} | {process} | {thread} | {module} | {filename}:{lineno} "
"| {message}",
style="{",
datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.getLogger(__name__)
WS_HOST = 'localhost'
WS_PORT = 8765
MESSAGES_COUNT = 1000
MESSAGE_TIMING = 0.00063
def get_symbols():
binance_client = Client()
exchange_info = binance_client.futures_exchange_info()
return [symbol_info['symbol'] for symbol_info in exchange_info['symbols']]
async def get_message():
symbol = random.choice(symbols)
return json.dumps({
"data": {
"e": "kline",
"E": datetime.now().timestamp() * 1000000,
"s": symbol,
"k": {
"t": 1638747660000,
"T": 1638747719999,
"s": symbol,
"i": "1m",
"f": 100,
"L": 200,
"o": "0.0010",
"c": "0.0020",
"h": "0.0025",
"l": "0.0015",
"v": "1000",
"n": 100,
"x": False,
"q": "1.0000",
"V": "500",
"Q": "0.500",
"B": "123456",
}
}
})
async def handler(websocket, path):
await websocket.recv()
tm = datetime.now()
for i in range(MESSAGES_COUNT):
message = await get_message()
await websocket.send(message)
logger.info(f"{message=}")
await asyncio.sleep(MESSAGE_TIMING)
logger.info((datetime.now() - tm).total_seconds())
return
def start_ws_server():
start_server = websockets.serve(handler, WS_HOST, WS_PORT)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
if __name__ == '__main__':
symbols = get_symbols()
start_ws_server() |
Is it possible to add support for REST API in this package too? This would be needed to run |
Yes, sure. You can do it. I just limited myself to web sockets in my task. The REST did not look. If you have the desire and opportunity - you can change the PR and tag me. I won't have time next week. |
I am also full of tasks :/ i started preparing a new update for full UBS packages... I would appreciate if we could add this to the next release but from my point of view we have to do:
I can do unittests and docs, because i want to learn and understand both. Could you do the REST implementation? |
Ok. Let me try on the weekend to implement for REST. Will you do documentation and tests? |
deal! i will do my part afterwards when this is merged. |
Awesome, did not know that :) Is it only possible to use localhost with the hardcoded port or can this be overwritten? |
Yeah. It's from python3.8
Hardcoded. To flexibly set the port, you need to rewrite it here (separate the key with the URI separately into host and port (tuple/dictionary)) and process it in the constructor of manager. |
Thanks , I will try when this PR is ready |
I will merge it, we also add socks server support and your code is usefull. thanks! |
PR Details
I made a more flexible
manager
constructor so that you can replace the exchange with your own mock server.Description
To replace the exchange with your mock server, just specify
exchange=Exchanges.LOCALHOST, exchange_type='dex'
(orcex
) when initializing the manager.exchange_type
must be specified only forLocalhost
. You can also setwebsocket_base_uri
andmax_subscriptions_per_stream
otherwise the default values will be applied.Related Issue
#304
Motivation and Context
With the previous implementation - replacing the server with your own mock - was unreal.
How Has This Been Tested
Tested on localhost (MacOS) and in docker. I worked with a real exchange and with my own mock server.
Types of changes
Checklist
CONTRIBUTING
document.