diff --git a/binance/ws/reconnecting_websocket.py b/binance/ws/reconnecting_websocket.py index 7a7cf78d7..80c6fd7d6 100644 --- a/binance/ws/reconnecting_websocket.py +++ b/binance/ws/reconnecting_websocket.py @@ -76,9 +76,9 @@ def __init__( self._https_proxy = https_proxy self._ws_kwargs = kwargs - def json_dumps(self, msg): + def json_dumps(self, msg) -> str: if orjson: - return orjson.dumps(msg) + return orjson.dumps(msg).decode("utf-8") return json.dumps(msg) def json_loads(self, msg): diff --git a/test-requirements.txt b/test-requirements.txt index ec3e381ae..ca61525fc 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -9,3 +9,4 @@ tox setuptools aioresponses pre-commit +orjson diff --git a/tests/test_async_client_ws_futures_requests.py b/tests/test_async_client_ws_futures_requests.py index 1df0d4beb..54bb4aeb4 100644 --- a/tests/test_async_client_ws_futures_requests.py +++ b/tests/test_async_client_ws_futures_requests.py @@ -1,5 +1,6 @@ import asyncio import pytest +import sys from binance.exceptions import BinanceAPIException, BinanceWebsocketUnableToConnect from .test_get_order_book import assert_ob @@ -52,7 +53,10 @@ async def test_ws_futures_get_order_book_ticker(futuresClientAsync): @pytest.mark.asyncio() -async def test_ws_futures_create_get_edit_cancel_order(futuresClientAsync): +async def test_ws_futures_create_get_edit_cancel_order_with_orjson(futuresClientAsync): + if 'orjson' not in sys.modules: + raise ImportError("orjson is not available") + ticker = await futuresClientAsync.ws_futures_get_order_book_ticker(symbol="LTCUSDT") positions = await futuresClientAsync.ws_futures_v2_account_position( symbol="LTCUSDT" @@ -83,6 +87,39 @@ async def test_ws_futures_create_get_edit_cancel_order(futuresClientAsync): orderid=order["orderId"], symbol=order["symbol"] ) +@pytest.mark.asyncio() +async def test_ws_futures_create_get_edit_cancel_order_without_orjson(futuresClientAsync): + with patch.dict('sys.modules', {'orjson': None}): + ticker = await futuresClientAsync.ws_futures_get_order_book_ticker(symbol="LTCUSDT") + positions = await futuresClientAsync.ws_futures_v2_account_position( + symbol="LTCUSDT" + ) + order = await futuresClientAsync.ws_futures_create_order( + symbol=ticker["symbol"], + side="BUY", + positionSide=positions[0]["positionSide"], + type="LIMIT", + timeInForce="GTC", + quantity=0.1, + price=str(float(ticker["bidPrice"]) - 2), + ) + assert_contract_order(futuresClientAsync, order) + order = await futuresClientAsync.ws_futures_edit_order( + orderid=order["orderId"], + symbol=order["symbol"], + quantity=0.11, + side=order["side"], + price=order["price"], + ) + assert_contract_order(futuresClientAsync, order) + order = await futuresClientAsync.ws_futures_get_order( + symbol="LTCUSDT", orderid=order["orderId"] + ) + assert_contract_order(futuresClientAsync, order) + order = await futuresClientAsync.ws_futures_cancel_order( + orderid=order["orderId"], symbol=order["symbol"] + ) + @pytest.mark.asyncio() async def test_ws_futures_v2_account_position(futuresClientAsync):