Skip to content

Commit

Permalink
Merge pull request #6 from elyobo/more-valid-response-codes
Browse files Browse the repository at this point in the history
Treat all 2XX HTTP response codes as successful.
  • Loading branch information
toloco committed Jul 21, 2015
2 parents 87e23e4 + 8293954 commit e3ff417
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pyoanda/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __call(self, uri, params=None, method="get"):
try:
resp = getattr(self.session, method)(**kwargs)
rjson = resp.json()
assert resp.status_code == 200
assert resp.ok
except AssertionError:
msg = "OCode-{}: {}".format(resp.status_code, rjson["message"])
raise BadRequest(msg)
Expand Down Expand Up @@ -95,7 +95,7 @@ def __call_stream(self, uri, params=None, method="get"):
kwargs["data"] = params
try:
resp = getattr(self.session, method)(**kwargs)
assert resp.status_code == 200
assert resp.ok
except AssertionError:
raise BadRequest(resp.status_code)
except Exception as e:
Expand Down
6 changes: 4 additions & 2 deletions pyoanda/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_call_pass(self):
c.session = requests.Session()
obj = mock
setattr(obj, "json", lambda: 1)
setattr(obj, "status_code", 200)
setattr(obj, "ok", True)
with mock.patch.object(c.session, 'get', return_value=obj):
c._Client__call(uri="test", params={"test": "test"}, method="get")

Expand All @@ -62,6 +62,7 @@ def test_call_fail(self):
obj = mock
setattr(obj, "json", lambda: {"message": "Bad request"})
setattr(obj, "status_code", 400)
setattr(obj, "ok", False)
with mock.patch.object(c.session, 'get', return_value=obj):
with self.assertRaises(BadRequest):
c._Client__call(uri="test", params=None, method="get")
Expand All @@ -76,7 +77,7 @@ def test_call_stream_pass(self):
c.session = requests.Session()
obj = mock
setattr(obj, "json", lambda: 1)
setattr(obj, "status_code", 200)
setattr(obj, "ok", True)
with mock.patch.object(c.session, 'get', return_value=obj):
c._Client__call_stream(
uri="test",
Expand All @@ -102,6 +103,7 @@ def test_call_stream_fail(self):
obj = mock
setattr(obj, "json", lambda: {"message": "Bad request"})
setattr(obj, "status_code", 400)
setattr(obj, "ok", False)
with mock.patch.object(c.session, 'get', return_value=obj):
with self.assertRaises(BadRequest):
c._Client__call_stream(uri="test", params={"test": "test"}, method="get")
Expand Down

0 comments on commit e3ff417

Please sign in to comment.