First set of fixes for uri module to work with py3.
This fix handles changes in the response headers (no longer all lowercased) and switches from unicode() to to_text().
This commit is contained in:
parent
51313e6da8
commit
bd9e790cfe
1 changed files with 27 additions and 21 deletions
|
@ -20,19 +20,6 @@
|
||||||
#
|
#
|
||||||
# see examples/playbooks/uri.yml
|
# see examples/playbooks/uri.yml
|
||||||
|
|
||||||
import cgi
|
|
||||||
import shutil
|
|
||||||
import tempfile
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
try:
|
|
||||||
import json
|
|
||||||
except ImportError:
|
|
||||||
import simplejson as json
|
|
||||||
|
|
||||||
import ansible.module_utils.six as six
|
|
||||||
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: uri
|
module: uri
|
||||||
|
@ -216,6 +203,23 @@ EXAMPLES = '''
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
import cgi
|
||||||
|
import datetime
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
try:
|
||||||
|
import json
|
||||||
|
except ImportError:
|
||||||
|
import simplejson as json
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.pycompat24 import get_exception
|
||||||
|
import ansible.module_utils.six as six
|
||||||
|
from ansible.module_utils._text import to_text
|
||||||
|
from ansible.module_utils.urls import fetch_url, url_argument_spec
|
||||||
|
|
||||||
|
|
||||||
def write_file(module, url, dest, content):
|
def write_file(module, url, dest, content):
|
||||||
# create a tempfile with some test content
|
# create a tempfile with some test content
|
||||||
|
@ -445,11 +449,17 @@ def main():
|
||||||
|
|
||||||
# Default content_encoding to try
|
# Default content_encoding to try
|
||||||
content_encoding = 'utf-8'
|
content_encoding = 'utf-8'
|
||||||
if 'content_type' in uresp:
|
content_type_key = None
|
||||||
content_type, params = cgi.parse_header(uresp['content_type'])
|
for key in uresp:
|
||||||
|
# Py2: content_type; Py3: Content_type
|
||||||
|
if 'content_type' == key.lower():
|
||||||
|
content_type_key = key
|
||||||
|
break
|
||||||
|
if content_type_key is not None:
|
||||||
|
content_type, params = cgi.parse_header(uresp[content_type_key])
|
||||||
if 'charset' in params:
|
if 'charset' in params:
|
||||||
content_encoding = params['charset']
|
content_encoding = params['charset']
|
||||||
u_content = unicode(content, content_encoding, errors='replace')
|
u_content = to_text(content, encoding=content_encoding)
|
||||||
if 'application/json' in content_type or 'text/json' in content_type:
|
if 'application/json' in content_type or 'text/json' in content_type:
|
||||||
try:
|
try:
|
||||||
js = json.loads(u_content)
|
js = json.loads(u_content)
|
||||||
|
@ -457,7 +467,7 @@ def main():
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
u_content = unicode(content, content_encoding, errors='replace')
|
u_content = to_text(content, encoding=content_encoding)
|
||||||
|
|
||||||
if resp['status'] not in status_code:
|
if resp['status'] not in status_code:
|
||||||
uresp['msg'] = 'Status code was not %s: %s' % (status_code, uresp.get('msg', ''))
|
uresp['msg'] = 'Status code was not %s: %s' % (status_code, uresp.get('msg', ''))
|
||||||
|
@ -468,9 +478,5 @@ def main():
|
||||||
module.exit_json(changed=changed, **uresp)
|
module.exit_json(changed=changed, **uresp)
|
||||||
|
|
||||||
|
|
||||||
# import module snippets
|
|
||||||
from ansible.module_utils.basic import *
|
|
||||||
from ansible.module_utils.urls import *
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue