fixed .loads error for non decoded json in Python 3 (#32065)

* fixed .loads error for non decoded json in Python 3

* fixed .loads error Python 3.5 - refactor code to one line

* fixed .loads error python 3.5 - mod to use to_text instead of .decode as per reviewer comment
This commit is contained in:
andy-pi 2017-11-18 03:51:13 +08:00 committed by Sam Doran
parent 5f36932adf
commit 67d5e1d3e7

View file

@ -274,10 +274,11 @@ import json
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves.urllib.parse import urlencode from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native, to_text
from ansible.module_utils.urls import fetch_url from ansible.module_utils.urls import fetch_url
class CloudflareAPI(object): class CloudflareAPI(object):
cf_api_endpoint = 'https://api.cloudflare.com/client/v4' cf_api_endpoint = 'https://api.cloudflare.com/client/v4'
@ -369,9 +370,9 @@ class CloudflareAPI(object):
if content: if content:
try: try:
result = json.loads(content) result = json.loads(to_text(content, errors='surrogate_then_strict'))
except json.JSONDecodeError: except (json.JSONDecodeError, UnicodeError) as e:
error_msg += "; Failed to parse API response: {0}".format(content) error_msg += "; Failed to parse API response with error {0}: {1}".format(to_native(e), content)
# received an error status but no data with details on what failed # received an error status but no data with details on what failed
if (info['status'] not in [200,304]) and (result is None): if (info['status'] not in [200,304]) and (result is None):