diff --git a/plugins/inventory/foreman.py b/plugins/inventory/foreman.py index b64b4cd19d..26ecf8efa6 100644 --- a/plugins/inventory/foreman.py +++ b/plugins/inventory/foreman.py @@ -173,19 +173,14 @@ from ansible_collections.theforeman.foreman.plugins.module_utils._version import LooseVersion from time import sleep from ansible.errors import AnsibleError -from ansible.module_utils._text import to_bytes, to_native, to_text +from ansible.module_utils._text import to_native, to_text from ansible.module_utils.common._collections_compat import MutableMapping from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable, to_safe_group_name, Constructable -# 3rd party imports try: - import requests - if LooseVersion(requests.__version__) < LooseVersion('1.1.0'): - raise ImportError - from requests.auth import HTTPBasicAuth - HAS_REQUESTS = True + from ansible_collections.theforeman.foreman.plugins.module_utils.ansible_requests import RequestSession except ImportError: - HAS_REQUESTS = False + from plugins.module_utils.ansible_requests import RequestSession class InventoryModule(BaseInventoryPlugin, Cacheable, Constructable): @@ -204,9 +199,6 @@ def __init__(self): self.cache_key = None self.use_cache = None - if not HAS_REQUESTS: - raise AnsibleError('This script requires python-requests 1.1 as a minimum version') - def verify_file(self, path): valid = False @@ -219,8 +211,8 @@ def verify_file(self, path): def _get_session(self): if not self.session: - self.session = requests.session() - self.session.auth = HTTPBasicAuth(self.get_option('user'), to_bytes(self.get_option('password'))) + self.session = RequestSession() + self.session.auth = (self.get_option('user'), self.get_option('password')) self.session.verify = self.get_option('validate_certs') return self.session diff --git a/plugins/module_utils/ansible_requests.py b/plugins/module_utils/ansible_requests.py index d03705897a..8f183709cd 100644 --- a/plugins/module_utils/ansible_requests.py +++ b/plugins/module_utils/ansible_requests.py @@ -115,5 +115,8 @@ def request(self, method, url, **kwargs): result = self.open(method, url, validate_certs=validate_certs, data=data, headers=headers, **kwargs) return RequestResponse(result) + def get(self, url, **kwargs): + return self.request('GET', url, **kwargs) + def post(self, url, data=None, json=None, **kwargs): return self.request('POST', url, data=data, json=json, **kwargs)