Skip to content

Commit 6848bda

Browse files
committed
Add error handling for the GM API wrapper
1 parent 078d963 commit 6848bda

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

gm_api.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def post(route, body):
1414
)
1515
json = resp.json()
1616

17-
# deal with error handling later
17+
print(json)
18+
19+
# handle errors
20+
if json.get('status') != '200':
21+
return {'error': json.get('reason')}
1822

1923
return json

test_gm_api.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
import requests
33
from gm_api import GM_Api as GM_Api
44

5-
def test_post(mocker):
5+
def test_post_success(mocker):
66
mock = mocker.patch('requests.post')
7-
def json():
8-
return 'this is a mock'
9-
mock.return_value.json = json
7+
def success_json():
8+
return {'status': '200', 'data': 'this is a mock'}
9+
mock.return_value.json = success_json
1010

1111
resp = GM_Api.post('/getVehicleInfoService', {'id': '1234'})
1212
mock.assert_called_with('http://gmapi.azurewebsites.net/getVehicleInfoService', json={'id': '1234', 'responseType': 'JSON'})
13-
assert(resp == 'this is a mock')
13+
assert(resp == {'status': '200', 'data': 'this is a mock'})
14+
15+
def test_post_fail(mocker):
16+
mock = mocker.patch('requests.post')
17+
def fail_json():
18+
return {'status': '404', 'reason': 'Vehicle id: 1000 not found.'}
19+
mock.return_value.json = fail_json
20+
21+
resp = GM_Api.post('/getVehicleInfoService', {'id': '1000'})
22+
mock.assert_called_with('http://gmapi.azurewebsites.net/getVehicleInfoService', json={'id': '1000', 'responseType': 'JSON'})
23+
assert(resp == {'error': 'Vehicle id: 1000 not found.'})

0 commit comments

Comments
 (0)