Skip to content

Commit 4ad00b8

Browse files
author
Sam McHardy
committed
Add tests to check response exceptions
1 parent 4643524 commit 4ad00b8

4 files changed

Lines changed: 43 additions & 2 deletions

File tree

binance/client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import requests
77
import six
88
import time
9-
from .exceptions import BinanceAPIException, BinanceWithdrawException
9+
from .exceptions import BinanceAPIException, BinanceRequestException, BinanceWithdrawException
1010
from .validation import validate_order
1111
from .enums import TIME_IN_FORCE_GTC, SIDE_BUY, SIDE_SELL, ORDER_TYPE_LIMIT, ORDER_TYPE_MARKET
1212

@@ -129,7 +129,10 @@ def _handle_response(self, response):
129129
"""
130130
if not str(response.status_code).startswith('2'):
131131
raise BinanceAPIException(response)
132-
return response.json()
132+
try:
133+
return response.json()
134+
except ValueError:
135+
raise BinanceRequestException('Invalid Response: %s' % response.text)
133136

134137
def _get(self, path, signed=False, **kwargs):
135138
return self._request_api('get', path, signed, **kwargs)

binance/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ def __str__(self): # pragma: no cover
1515
return 'APIError(code=%s): %s' % (self.code, self.message)
1616

1717

18+
class BinanceRequestException(Exception):
19+
def __init__(self, message):
20+
self.message = message
21+
22+
def __str__(self):
23+
return 'BinanceRequestException: %s' % self.message
24+
25+
1826
class BinanceOrderException(Exception):
1927

2028
def __init__(self, code, message):

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ pytest==3.2.1
44
pytest-cov==2.5.1
55
pytest-pep8==1.0.6
66
python-coveralls==2.9.1
7+
requests-mock==1.3.0
78
tox==2.7.0
89
setuptools==36.6.0

tests/test_api_request.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
from binance.client import Client
5+
from binance.exceptions import BinanceAPIException, BinanceRequestException
6+
import pytest
7+
import requests_mock
8+
9+
10+
client = Client('api_key', 'api_secret')
11+
12+
13+
def test_invalid_json():
14+
"""Test Invalid response Exception"""
15+
16+
with pytest.raises(BinanceRequestException):
17+
with requests_mock.mock() as m:
18+
m.get('https://www.binance.com/exchange/public/product', text='<head></html>')
19+
client.get_products()
20+
21+
22+
def test_api_exception():
23+
"""Test API response Exception"""
24+
25+
with pytest.raises(BinanceAPIException):
26+
with requests_mock.mock() as m:
27+
json_obj = {"code": 1002, "msg": "Invalid API call"}
28+
m.get('https://www.binance.com/api/v1/time', json=json_obj, status_code=400)
29+
client.get_server_time()

0 commit comments

Comments
 (0)