mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-25 14:20:22 -07:00
Fix unicode errors in json.load method
HTTPConnection returns bytes in Python 2. An explicit decoding is needed in Python 3 to ensure compatibility.
This commit is contained in:
parent
cfa8bb4255
commit
66040e0c9d
1 changed files with 13 additions and 11 deletions
|
@ -27,24 +27,23 @@
|
||||||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
||||||
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
import socket
|
||||||
|
import ssl
|
||||||
|
|
||||||
from ansible.module_utils.urls import generic_urlparse
|
from ansible.module_utils.urls import generic_urlparse
|
||||||
from ansible.module_utils.six.moves.urllib.parse import urlparse
|
from ansible.module_utils.six.moves.urllib.parse import urlparse
|
||||||
|
from ansible.module_utils.six.moves import http_client
|
||||||
|
from ansible.module_utils.six import PY2
|
||||||
|
|
||||||
|
# httplib/http.client connection using unix domain socket
|
||||||
|
HTTPConnection = http_client.HTTPConnection
|
||||||
|
HTTPSConnection = http_client.HTTPSConnection
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
|
||||||
# httplib/http.client connection using unix domain socket
|
|
||||||
import socket
|
|
||||||
import ssl
|
|
||||||
|
|
||||||
try:
|
|
||||||
from httplib import HTTPConnection, HTTPSConnection
|
|
||||||
except ImportError:
|
|
||||||
# Python 3
|
|
||||||
from http.client import HTTPConnection, HTTPSConnection
|
|
||||||
|
|
||||||
|
|
||||||
class UnixHTTPConnection(HTTPConnection):
|
class UnixHTTPConnection(HTTPConnection):
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
|
@ -110,7 +109,10 @@ class LXDClient(object):
|
||||||
body = json.dumps(body_json)
|
body = json.dumps(body_json)
|
||||||
self.connection.request(method, url, body=body)
|
self.connection.request(method, url, body=body)
|
||||||
resp = self.connection.getresponse()
|
resp = self.connection.getresponse()
|
||||||
resp_json = json.loads(resp.read())
|
resp_data = resp.read()
|
||||||
|
if not PY2:
|
||||||
|
resp_data = resp_data.decode('utf-8')
|
||||||
|
resp_json = json.loads(resp_data)
|
||||||
self.logs.append({
|
self.logs.append({
|
||||||
'type': 'sent request',
|
'type': 'sent request',
|
||||||
'request': {'method': method, 'url': url, 'json': body_json, 'timeout': timeout},
|
'request': {'method': method, 'url': url, 'json': body_json, 'timeout': timeout},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue