-
Notifications
You must be signed in to change notification settings - Fork 39
Add stream RPC client #119
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
Merged
Merged
Changes from 37 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
228f6ff
Add stream RPC client
alexex10 ce0d3f6
Add stream RPC client
alexex10 ebd76db
Merge branch 'starknet' of ex10.github.com:x10xchange/python_sdk into…
alexex10 17e61ae
Add stream RPC client
alexex10 dc238e5
Add stream RPC client
alexex10 ca9361d
Add stream RPC client
alexex10 f0b6c8b
Add stream RPC client
alexex10 1aef5a4
Add stream RPC client
alexex10 3f2f456
Add stream RPC client
alexex10 3b9ec8c
Add stream RPC client
alexex10 a194bed
Add stream RPC client
alexex10 0a06d95
Add stream RPC client
alexex10 9e2d539
Add stream RPC client
alexex10 d120ff3
Add stream RPC client
alexex10 057a82b
Add stream RPC client
alexex10 71fd53c
Add stream RPC client
alexex10 7b2b3f0
Add stream RPC client
alexex10 45a5689
Add stream RPC client
alexex10 aa4ddd6
Add stream RPC client
alexex10 d084087
Add stream RPC client
alexex10 ac4b788
Add stream RPC client
alexex10 b7bd847
Add stream RPC client
alexex10 e82d12d
Add stream RPC client
alexex10 8611301
Add stream RPC client
alexex10 c79ae1c
Add stream RPC client
alexex10 a4047b0
Add stream RPC client
alexex10 ab7c0ef
Add stream RPC client
alexex10 d18a5f8
Add stream RPC client
alexex10 cdd1084
Add stream RPC client
alexex10 205ee3a
Add stream RPC client
alexex10 e8dbd9b
Add stream RPC client
alexex10 5f2d901
Add stream RPC client
alexex10 47b9019
Add stream RPC client
alexex10 09ca323
Add stream RPC client
alexex10 fb72667
Add stream RPC client
alexex10 787aa8d
Add stream RPC client
alexex10 4078e53
Add stream RPC client
alexex10 91afaee
Add stream RPC client
alexex10 d992741
Add stream RPC client
alexex10 c8bda94
Add stream RPC client
alexex10 9b63f31
Add stream RPC client
alexex10 b17c01d
Add stream RPC client
alexex10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import asyncio | ||
| import logging | ||
| from asyncio import run | ||
| from signal import SIGINT, SIGTERM | ||
|
|
||
| from examples.utils import BTC_USD_MARKET, create_stream_rpc_client, init_env | ||
| from x10.clients.streamrpc.subscription_params import ( | ||
| CandlesParams, | ||
| PricesParams, | ||
| TradesParams, | ||
| ) | ||
| from x10.config import get_config_by_name | ||
| from x10.models.stream_rpc import StreamRpcResponseModel | ||
|
|
||
| LOGGER = logging.getLogger() | ||
| MARKET_NAME = BTC_USD_MARKET | ||
|
|
||
|
|
||
| def on_message(message: StreamRpcResponseModel) -> None: | ||
| LOGGER.info("Received message: %s", message) | ||
|
|
||
|
|
||
| async def subscribe_to_rpc_stream(stop_event: asyncio.Event): | ||
| env_config = init_env() | ||
| client_config = get_config_by_name(env_config.client_config_name) | ||
|
|
||
| async with create_stream_rpc_client(client_config) as client: | ||
| await client.ping() | ||
|
|
||
| subscriptions_before = await client.list_subscriptions() | ||
|
|
||
| LOGGER.info("Active subscriptions: %s", subscriptions_before) | ||
|
|
||
| await client.subscribe(params=TradesParams(market="BTC-USD"), handler=on_message) | ||
| await client.subscribe(params=TradesParams(market="ETH-USD"), handler=on_message) | ||
| await client.subscribe(params=PricesParams(price_type="index", market="ETH-USD"), handler=on_message) | ||
| await client.subscribe( | ||
| params=CandlesParams(candle_type="index", market="ETH-USD", interval="PT1M"), handler=on_message | ||
| ) | ||
|
|
||
| subscriptions_after = await client.list_subscriptions() | ||
|
|
||
| LOGGER.info("Active subscriptions: %s", subscriptions_after) | ||
|
|
||
| await stop_event.wait() | ||
|
|
||
|
|
||
| async def run_example(): | ||
| stop_event = asyncio.Event() | ||
| loop = asyncio.get_running_loop() | ||
|
|
||
| def signal_handler(): | ||
| LOGGER.info("Signal received, stopping...") | ||
| stop_event.set() | ||
|
|
||
| loop.add_signal_handler(SIGINT, signal_handler) | ||
| loop.add_signal_handler(SIGTERM, signal_handler) | ||
|
|
||
| await subscribe_to_rpc_stream(stop_event) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| run(main=run_example()) |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import asyncio | ||
| import json | ||
|
|
||
| import pytest | ||
| import websockets | ||
| from hamcrest import assert_that, equal_to | ||
| from websockets import WebSocketServer | ||
|
|
||
|
|
||
| def get_url_from_server(server: WebSocketServer): | ||
| host, port = server.sockets[0].getsockname() # type: ignore[index] | ||
| return f"ws://{host}:{port}" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_candle_stream(): | ||
| from tests.fixtures.candle import create_candle_stream_rpc_message | ||
| from x10.clients.streamrpc.streamrpc_client import StreamRpcClient | ||
| from x10.clients.streamrpc.subscription_params import CandlesParams | ||
|
|
||
| message_model = create_candle_stream_rpc_message() | ||
| received_messages: asyncio.Queue = asyncio.Queue() | ||
|
|
||
| async def subscription_handler(msg): | ||
| await received_messages.put(msg) | ||
|
|
||
| async def mock_server(websocket): | ||
| subscribe_msg_raw = await websocket.recv() | ||
| subscribe_msg = json.loads(subscribe_msg_raw) | ||
|
|
||
| assert_that(subscribe_msg["method"], equal_to("subscribe")) | ||
|
|
||
| await websocket.send( | ||
| json.dumps( | ||
| { | ||
| "id": subscribe_msg["id"], | ||
| "result": {"subscription": message_model.subscription}, | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| await websocket.send(json.dumps(message_model.to_api_request_json())) | ||
|
|
||
| unsubscribe_msg_raw = await websocket.recv() | ||
| unsubscribe_msg = json.loads(unsubscribe_msg_raw) | ||
|
|
||
| assert_that(unsubscribe_msg["method"], equal_to("unsubscribe")) | ||
|
|
||
| await websocket.send( | ||
| json.dumps( | ||
| { | ||
| "id": unsubscribe_msg["id"], | ||
| "result": {"method": "unsubscribe", "status": "OK"}, | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| async with websockets.serve(mock_server, "127.0.0.1", 0) as server: | ||
| client = StreamRpcClient(api_url=get_url_from_server(server)) | ||
| await client.connect() | ||
|
|
||
| subscription_params = CandlesParams(candle_type="last", market="BTC-USD", interval="PT1M") | ||
| subscription_id = await client.subscribe(params=subscription_params, handler=subscription_handler) | ||
|
|
||
| msg = await asyncio.wait_for(received_messages.get(), timeout=5) | ||
|
|
||
| await client.unsubscribe(subscription_id) | ||
| await client.close() | ||
|
|
||
| assert_that( | ||
| msg.to_api_request_json(), | ||
| equal_to( | ||
| { | ||
| "type": "CANDLE", | ||
| "data": [ | ||
| {"o": "3458.64", "l": "3399.07", "h": "3476.89", "c": "3414.85", "v": "3.938", "T": 1721106000000} | ||
| ], | ||
| "error": None, | ||
| "ts": 1721283121979, | ||
| "seq": 1, | ||
| "subscription": "candles.last.BTC-USD.PT1M", | ||
| } | ||
| ), | ||
| ) | ||
This file contains hidden or 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 hidden or 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
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.