Previous fix to this failed to account for open_url returning a filehandle (#20097)

* Previous fix to this failed to account for open_url returning a filehandle

Fixes the bugs introduced by c6fb355

* read() from HTTPError for python-3.6+

HTTPError is funny.  It contains a filehandle to read the response from
and also makes it available via a read() method.  On earlier versions of
python (2 and 3) the read() method was enough to make it work with
json.load().  The newer version of json.load() needs a more complete
file interface than this and has stopped working.  Read the bytes,
transform to str and pass it in manually to fix it.
This commit is contained in:
Toshio Kuratomi 2017-01-10 18:14:20 -08:00 committed by GitHub
parent f340b8d14f
commit 1786c81a65

View file

@ -93,9 +93,9 @@ class GalaxyAPI(object):
display.vvv(url)
resp = open_url(url, data=args, validate_certs=self._validate_certs, headers=headers, method=method,
timeout=20)
data = json.load(to_text(resp, errors='surrogate_or_strict'))
data = json.loads(to_text(resp.read(), errors='surrogate_or_strict'))
except HTTPError as e:
res = json.load(e)
res = json.loads(to_text(e.fp.read(), errors='surrogate_or_strict'))
raise AnsibleError(res['detail'])
return data
@ -119,7 +119,7 @@ class GalaxyAPI(object):
raise AnsibleError("Failed to get data from the API server (%s): %s " % (url, to_native(e)))
try:
data = json.load(to_text(return_data, errors='surrogate_or_strict'))
data = json.loads(to_text(return_data.read(), errors='surrogate_or_strict'))
except Exception as e:
raise AnsibleError("Could not process data from the API server (%s): %s " % (url, to_native(e)))
@ -136,7 +136,7 @@ class GalaxyAPI(object):
url = '%s/tokens/' % self.baseurl
args = urlencode({"github_token": github_token})
resp = open_url(url, data=args, validate_certs=self._validate_certs, method="POST")
data = json.load(to_text(resp, errors='surrogate_or_strict'))
data = json.loads(to_text(resp.read(), errors='surrogate_or_strict'))
return data
@g_connect