api: Fix running with requests 1.0.x.

I would prefer to be testing the attribute itself rather than the
version, but it's not easy to access without an actual request object,
and I'd prefer to compute this once-and-for-all on startup, rather
than on each request, since the latter just seems fragile.

(imported from commit dd74cadb1b2359faeb3e1b482faeee4003dfad77)
This commit is contained in:
Tim Abbott 2013-02-05 15:08:48 -05:00 committed by Luke Faraone
parent e4d6a6d3cd
commit a5f2bec873
1 changed files with 9 additions and 4 deletions

View File

@ -38,8 +38,9 @@ __version__ = "0.1.0"
# Check that we have a recent enough version # Check that we have a recent enough version
# Older versions don't provide the 'json' attribute on responses. # Older versions don't provide the 'json' attribute on responses.
assert(requests.__version__ >= '0.12.1') assert(requests.__version__ >= '0.12.1')
# And versions in the 1.x series aren't backwards compatible # In newer versions, the 'json' attribute is a function, not a property
assert(requests.__version__ < '1.0.0') requests_json_is_function = isinstance(requests.Response.json, property)
API_VERSTRING = "/api/v1/" API_VERSTRING = "/api/v1/"
def generate_option_group(parser): def generate_option_group(parser):
@ -168,9 +169,13 @@ class Client(object):
return {'msg': "Unexpected error:\n%s" % traceback.format_exc(), return {'msg': "Unexpected error:\n%s" % traceback.format_exc(),
"result": "unexpected-error"} "result": "unexpected-error"}
if res.json is not None: if requests_json_is_function:
json_result = res.json()
else:
json_result = res.json
if json_result is not None:
end_error_retry(True) end_error_retry(True)
return res.json return json_result
end_error_retry(False) end_error_retry(False)
return {'msg': res.text, "result": "http-error", return {'msg': res.text, "result": "http-error",
"status_code": res.status_code} "status_code": res.status_code}