Skip to content

Commit be87758

Browse files
authored
fix: json_dumps when using orjson (sammchardy#1553)
* fix: json_dumps when using orjson * add test * add orjson to test requirements
1 parent 1d3bbff commit be87758

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

binance/ws/reconnecting_websocket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ def __init__(
7676
self._https_proxy = https_proxy
7777
self._ws_kwargs = kwargs
7878

79-
def json_dumps(self, msg):
79+
def json_dumps(self, msg) -> str:
8080
if orjson:
81-
return orjson.dumps(msg)
81+
return orjson.dumps(msg).decode("utf-8")
8282
return json.dumps(msg)
8383

8484
def json_loads(self, msg):

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ tox
99
setuptools
1010
aioresponses
1111
pre-commit
12+
orjson

tests/test_async_client_ws_futures_requests.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import pytest
3+
import sys
34

45
from binance.exceptions import BinanceAPIException, BinanceWebsocketUnableToConnect
56
from .test_get_order_book import assert_ob
@@ -52,7 +53,10 @@ async def test_ws_futures_get_order_book_ticker(futuresClientAsync):
5253

5354

5455
@pytest.mark.asyncio()
55-
async def test_ws_futures_create_get_edit_cancel_order(futuresClientAsync):
56+
async def test_ws_futures_create_get_edit_cancel_order_with_orjson(futuresClientAsync):
57+
if 'orjson' not in sys.modules:
58+
raise ImportError("orjson is not available")
59+
5660
ticker = await futuresClientAsync.ws_futures_get_order_book_ticker(symbol="LTCUSDT")
5761
positions = await futuresClientAsync.ws_futures_v2_account_position(
5862
symbol="LTCUSDT"
@@ -83,6 +87,39 @@ async def test_ws_futures_create_get_edit_cancel_order(futuresClientAsync):
8387
orderid=order["orderId"], symbol=order["symbol"]
8488
)
8589

90+
@pytest.mark.asyncio()
91+
async def test_ws_futures_create_get_edit_cancel_order_without_orjson(futuresClientAsync):
92+
with patch.dict('sys.modules', {'orjson': None}):
93+
ticker = await futuresClientAsync.ws_futures_get_order_book_ticker(symbol="LTCUSDT")
94+
positions = await futuresClientAsync.ws_futures_v2_account_position(
95+
symbol="LTCUSDT"
96+
)
97+
order = await futuresClientAsync.ws_futures_create_order(
98+
symbol=ticker["symbol"],
99+
side="BUY",
100+
positionSide=positions[0]["positionSide"],
101+
type="LIMIT",
102+
timeInForce="GTC",
103+
quantity=0.1,
104+
price=str(float(ticker["bidPrice"]) - 2),
105+
)
106+
assert_contract_order(futuresClientAsync, order)
107+
order = await futuresClientAsync.ws_futures_edit_order(
108+
orderid=order["orderId"],
109+
symbol=order["symbol"],
110+
quantity=0.11,
111+
side=order["side"],
112+
price=order["price"],
113+
)
114+
assert_contract_order(futuresClientAsync, order)
115+
order = await futuresClientAsync.ws_futures_get_order(
116+
symbol="LTCUSDT", orderid=order["orderId"]
117+
)
118+
assert_contract_order(futuresClientAsync, order)
119+
order = await futuresClientAsync.ws_futures_cancel_order(
120+
orderid=order["orderId"], symbol=order["symbol"]
121+
)
122+
86123

87124
@pytest.mark.asyncio()
88125
async def test_ws_futures_v2_account_position(futuresClientAsync):

0 commit comments

Comments
 (0)