Skip to content

Commit a1d7967

Browse files
committed
Remove use of Python requests for jbossnetworkapi
Signed-off-by: Andrew Block <andy.block@gmail.com>
1 parent 8021363 commit a1d7967

File tree

6 files changed

+35
-64
lines changed

6 files changed

+35
-64
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ Clone the repository, checkout the tag you want to build, or pick the main branc
5151

5252
#### Python:
5353

54-
* [requests](https://requests.readthedocs.io/en/latest/)
54+
* [jmespath](https://jmespath.org/)
55+
* [lxml](https://lxml.de/)
5556

5657
To install all the dependencies via galaxy:
5758

bindep.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
python3-requests [platform:rpm]

plugins/module_utils/jbossnetwork.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616

1717
__metaclass__ = type
1818

19-
try:
20-
import requests
21-
HAS_REQUESTS = True
22-
except ImportError:
23-
HAS_REQUESTS = False
24-
2519
from ansible_collections.middleware_automation.common.plugins.module_utils.constants import (
2620
QUERY_PAGE_SIZE,
2721
NEXT_CURSOR_FIELD,
@@ -36,7 +30,10 @@
3630
SEARCH_PARAM_NAME
3731
)
3832

33+
import json
3934
from ansible.module_utils._text import to_native
35+
from ansible.module_utils.urls import Request
36+
from ansible.module_utils.six.moves.urllib.parse import urlencode
4037

4138

4239
def get_authenticated_session(module, sso_url, validate_certs, client_id, client_secret):
@@ -48,26 +45,22 @@ def get_authenticated_session(module, sso_url, validate_certs, client_id, client
4845
"grant_type": "client_credentials",
4946
}
5047

51-
# Obtain Access Token
52-
if HAS_REQUESTS:
53-
token_request = requests.post(
54-
f"{sso_url}/auth/realms/redhat-external/protocol/openid-connect/token",
55-
data=token_request_data, verify=validate_certs)
48+
# Initialize Session
49+
session = Request(validate_certs=validate_certs, headers={})
5650

5751
try:
58-
token_request.raise_for_status()
52+
token_request = session.post(f"{sso_url}/auth/realms/redhat-external/protocol/openid-connect/token", data=urlencode(token_request_data))
53+
5954
except Exception as err:
6055
module.fail_json(msg="Error Retrieving SSO Access Token: %s" % (to_native(err)))
6156

62-
access_token = token_request.json()["access_token"]
57+
access_token = json.loads(token_request.read())["access_token"]
6358

6459
# Setup Session
65-
if HAS_REQUESTS:
66-
session = requests.Session()
67-
session.headers = {
68-
"Authorization": f"Bearer {access_token}",
69-
"Content-Type": "application/json",
70-
}
60+
session.headers = {
61+
"Authorization": f"Bearer {access_token}",
62+
"Content-Type": "application/json",
63+
}
7164

7265
return session
7366

@@ -107,10 +100,22 @@ def perform_search(session, url, validate_certs, params=None):
107100

108101
params.update(pagination_params)
109102

110-
query_request = session.get(url, params=params, verify=validate_certs)
111-
query_request.raise_for_status()
103+
full_url = []
104+
full_url.append(url)
105+
106+
if len(params) > 0:
107+
108+
# Remove None Keys
109+
none_keys = [k for (k, v) in params.items() if v is None]
110+
for key in none_keys:
111+
del params[key]
112+
113+
full_url.append("?")
114+
full_url.append(urlencode(params))
115+
116+
query_request = session.get("".join(full_url))
112117

113-
query_result_json = query_request.json()
118+
query_result_json = json.loads(query_request.read())
114119

115120
results.extend(
116121
query_result_json[RESULTS_FIELD])

plugins/modules/product_download.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
short_description: Downloads products from the JBoss Network API.
4444
description:
4545
- Downloads products from the JBoss Network API.
46-
requirements:
47-
- requests
4846
extends_documentation_fragment:
4947
- files
5048
- middleware_automation.common.jbossnetwork_connection_options
@@ -131,11 +129,9 @@
131129
sample: 100
132130
"""
133131

134-
import traceback
135132
import os
136133
import copy
137134
from ansible.module_utils.basic import AnsibleModule
138-
from ansible.module_utils.basic import missing_required_lib
139135
from ansible.module_utils._text import to_native
140136

141137
from ansible_collections.middleware_automation.common.plugins.module_utils.jbossnetwork import (
@@ -157,16 +153,6 @@
157153
LIST_PRODUCT_CATEGORIES_ENDPOINT
158154
)
159155

160-
try:
161-
import requests
162-
requests_version = requests.__version__
163-
HAS_REQUESTS = True
164-
except ImportError:
165-
HAS_REQUESTS = False
166-
REQUESTS_IMP_ERR = traceback.format_exc()
167-
else:
168-
REQUESTS_IMP_ERR = None
169-
170156

171157
def argspec():
172158
argument_spec = copy.deepcopy(JBOSS_NETWORK_COMMON_ARGS_SPEC)
@@ -183,9 +169,6 @@ def main():
183169
add_file_common_args=True
184170
)
185171

186-
if not HAS_REQUESTS:
187-
module.fail_json(msg=missing_required_lib("requests"), exception=REQUESTS_IMP_ERR)
188-
189172
client_id = module.params.get('client_id')
190173
client_secret = module.params.get('client_secret')
191174
api_url = module.params.get('api_url')
@@ -289,11 +272,13 @@ def main():
289272
module.fail_json(msg="Destination %s is not writable" % (os.path.dirname(dest)), **result)
290273

291274
try:
292-
with session.get(search_results[0]["download_path"], verify=validate_certs,
293-
stream=True, allow_redirects=True, headers={"User-Agent": "product_download"}) as r:
294-
r.raise_for_status()
275+
with session.get(search_results[0]["download_path"], follow_redirects=True, headers={"User-Agent": "product_download"}) as r:
276+
chunk_size = 8192
295277
with open(dest, 'wb') as f:
296-
for chunk in r.iter_content(chunk_size=8192):
278+
while True:
279+
chunk = r.read(chunk_size)
280+
if not chunk:
281+
break
297282
f.write(chunk)
298283

299284
result['changed'] = True

plugins/modules/product_search.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
short_description: Searches products from the JBoss Network API.
4444
description:
4545
- Searches products from the JBoss Network API.
46-
requirements:
47-
- requests
4846
extends_documentation_fragment:
4947
- middleware_automation.common.jbossnetwork_connection_options
5048
"""
@@ -116,11 +114,9 @@
116114
type: str
117115
"""
118116

119-
import traceback
120117
import os
121118
import copy
122119
from ansible.module_utils.basic import AnsibleModule
123-
from ansible.module_utils.basic import missing_required_lib
124120
from ansible.module_utils._text import to_native
125121

126122
from ansible_collections.middleware_automation.common.plugins.module_utils.jbossnetwork import (
@@ -143,17 +139,6 @@
143139
)
144140

145141

146-
try:
147-
import requests
148-
requests_version = requests.__version__
149-
HAS_REQUESTS = True
150-
except ImportError:
151-
HAS_REQUESTS = False
152-
REQUESTS_IMP_ERR = traceback.format_exc()
153-
else:
154-
REQUESTS_IMP_ERR = None
155-
156-
157142
def argspec():
158143
argument_spec = copy.deepcopy(JBOSS_NETWORK_COMMON_ARGS_SPEC)
159144
argument_spec.update(copy.deepcopy(JBOSS_NETWORK_SEARCH_ARGS_SPEC))
@@ -167,9 +152,6 @@ def main():
167152
add_file_common_args=False
168153
)
169154

170-
if not HAS_REQUESTS:
171-
module.fail_json(msg=missing_required_lib("requests"), exception=REQUESTS_IMP_ERR)
172-
173155
client_id = module.params.get('client_id')
174156
client_secret = module.params.get('client_secret')
175157
api_url = module.params.get('api_url')

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
requests
21
lxml
32
jmespath

0 commit comments

Comments
 (0)