Skip to content

Commit d12d6a9

Browse files
authored
fix: return empty json instead of throwing error on empty response (sammchardy#1554)
* fix: return empty json instead of throwing error on empty response * fix pyright
1 parent b02989d commit d12d6a9

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

binance/async_client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ async def _handle_response(self, response: aiohttp.ClientResponse):
161161
"""
162162
if not str(response.status).startswith("2"):
163163
raise BinanceAPIException(response, response.status, await response.text())
164+
165+
if response.text == "":
166+
return {}
167+
164168
try:
165169
return await response.json()
166170
except ValueError:
@@ -306,9 +310,12 @@ async def get_all_tickers(
306310
params = {}
307311
if symbol:
308312
params["symbol"] = symbol
309-
return await self._get(
313+
response = await self._get(
310314
"ticker/price", version=self.PRIVATE_API_VERSION, data=params
311315
)
316+
if isinstance(response, list) and all(isinstance(item, dict) for item in response):
317+
return response
318+
raise TypeError("Expected a list of dictionaries")
312319

313320
get_all_tickers.__doc__ = Client.get_all_tickers.__doc__
314321

binance/client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def _handle_response(response: requests.Response):
9696
"""
9797
if not (200 <= response.status_code < 300):
9898
raise BinanceAPIException(response, response.status_code, response.text)
99+
100+
if response.text == "":
101+
return {}
102+
99103
try:
100104
return response.json()
101105
except ValueError:
@@ -382,7 +386,10 @@ def get_all_tickers(self) -> List[Dict[str, str]]:
382386
:raises: BinanceRequestException, BinanceAPIException
383387

384388
"""
385-
return self._get("ticker/price", version=self.PRIVATE_API_VERSION)
389+
response = self._get("ticker/price", version=self.PRIVATE_API_VERSION)
390+
if isinstance(response, list) and all(isinstance(item, dict) for item in response):
391+
return response
392+
raise TypeError("Expected a list of dictionaries")
386393

387394
def get_orderbook_tickers(self, **params) -> Dict:
388395
"""Best price/qty on the order book for all symbols.

0 commit comments

Comments
 (0)