Skip to content

Commit

Permalink
Merge pull request #220 from mbish/string-response
Browse files Browse the repository at this point in the history
Fix issue parsing API responses that are just a string
  • Loading branch information
yizshi authored May 4, 2023
2 parents cb2eb87 + 0a0dc34 commit d21ceaf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion duo_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def raise_error(msg):
raise_error('Received error response: %s' % data)
response = data['response']
metadata = data.get('metadata', {})
if not metadata and not isinstance(response, list):
if not metadata and isinstance(response, dict):
metadata = response.get('metadata', {})

return (response, metadata)
Expand Down
32 changes: 32 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,38 @@ def test_response_contains_invalid_json(self):
self.assertEqual(e.exception.reason, api_res.reason)
self.assertEqual(e.exception.data, response)

def test_response_is_a_string(self):
"""
Some API requests return just a string in their response.
We want to make sure we handle those correctly
"""
api_res = self.APIResponse(200, 'Fake reason')
response = {
'response': "just a string",
'stat': 'OK'
}
try:
self.client.parse_json_response_and_metadata(api_res, json.dumps(response))
except Exception:
self.fail("parsing raised exception for string response")

def test_response_is_a_list(self):
"""
Some API requests return just a list in their response. with
metadata included at the top level.
We want to make sure we handle those correctly
"""
api_res = self.APIResponse(200, "Fake reason")
expected_metadata = { "offset": 4 }
expected_response = ["multiple", "elements"]
response = {
"response": expected_response,
"metadata": expected_metadata,
"stat": "OK"
}
response, metadata = self.client.parse_json_response_and_metadata(api_res, json.dumps(response))
self.assertEqual(metadata, expected_metadata)

def test_response_stat_isnot_OK(self):
api_res = self.APIResponse(200, 'Fake reason')

Expand Down

0 comments on commit d21ceaf

Please sign in to comment.