Skip to content

Commit 1dbc8f2

Browse files
author
Werner Dijkerman
authored
Added some more options to http lib (#54)
* Added some more options to http lib
1 parent 5c99c68 commit 1dbc8f2

5 files changed

+47
-39
lines changed

.pre-commit-config.yaml

+1-9
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,9 @@ repos:
55
- id: shellcheck
66
- id: markdown-toc
77
- id: verify-yaml
8-
- id: no-commit-on-branch
9-
args: ['-b master,main']
10-
11-
# - repo: https://gitlab.com/pycqa/flake8
12-
# rev: 3.8.4
13-
# hooks:
14-
# - id: flake8
15-
# additional_dependencies: [flake8-typing-imports==1.7.0]
168

179
- repo: https://github.com/pre-commit/pre-commit-hooks
18-
rev: v3.4.0
10+
rev: v4.6.0
1911
hooks:
2012
- id: end-of-file-fixer
2113
- id: trailing-whitespace

CONTRIBUTORS

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Werner Dijkerman
22
Werner Dijkerman [GH bot]
3+
dependabot[bot]

lib/djWasabi/http.py

+33-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

3-
import sys
43
import requests
5-
from . import generic
4+
import urllib3
65
from requests.adapters import HTTPAdapter
76
from requests.packages.urllib3.util.retry import Retry
87
from http.client import HTTPConnection
98

9+
urllib3.disable_warnings()
1010

1111
class request():
1212
"""."""
@@ -83,6 +83,8 @@ def _get(self, url: str = None, headers: dict = {}, username: str = None, passwo
8383

8484
try:
8585
return (True, self.http.get(url, **kwargs))
86+
except requests.exceptions.SSLError as e:
87+
return (False, {'error': e})
8688
except requests.exceptions.RequestException as e:
8789
return (False, {'error': e})
8890

@@ -117,7 +119,7 @@ def _patch(self, url: str = None, headers: dict = {}, data: dict = {}, username:
117119
except requests.exceptions.RequestException as e:
118120
return (False, {'error': e})
119121

120-
def _post(self, url: str = None, headers: dict = {}, data: dict = {}, username: str = None, password: str = None) -> tuple:
122+
def _post(self, url: str = None, headers: dict = {}, data: dict = {}, username: str = None, password: str = None, files: str = None) -> tuple:
121123
"""POST the information from provided url.
122124
123125
:param url: The URL we want to POST.
@@ -130,31 +132,38 @@ def _post(self, url: str = None, headers: dict = {}, data: dict = {}, username:
130132
:type username: str
131133
:param password: The password for the provided username.
132134
:type password: str
135+
:param files: The contents of the file to be posted.
136+
:type files: obj
133137
:rtype: tuple
134138
:return: Succes (or not) with the request object
135139
"""
136140
if not url:
137141
raise ValueError('Please provide the URL.')
138-
139142
kwargs = {
140-
"headers": headers,
141-
"data": data
143+
"headers": headers
142144
}
145+
146+
if len(data) > 0:
147+
kwargs['data'] = data
143148
if username is not None and password is not None:
144149
kwargs['auth'] = (username, password)
150+
if files is not None:
151+
kwargs['files'] = files
145152

146153
try:
147154
return (True, self.http.post(url, **kwargs))
148155
except requests.exceptions.RequestException as e:
149156
return (False, {'error': e})
150157

151-
def _put(self, url: str = None, headers: dict = {}, username: str = None, password: str = None) -> tuple:
158+
def _put(self, url: str = None, headers: dict = {}, data: dict = {}, username: str = None, password: str = None) -> tuple:
152159
"""PUT the information from provided url.
153160
154161
:param url: The URL we want to PUT.
155162
:type url: str
156163
:param headers: The headers.
157164
:type headers: dict
165+
:param data: The data we want to PUT.
166+
:type data: dict
158167
:param username: The username that needs to be used when authentication is needed.
159168
:type username: str
160169
:param password: The password for the provided username.
@@ -168,6 +177,8 @@ def _put(self, url: str = None, headers: dict = {}, username: str = None, passwo
168177
kwargs = {
169178
"headers": headers
170179
}
180+
if len(data) > 0:
181+
kwargs['data'] = data
171182
if username is not None and password is not None:
172183
kwargs['auth'] = (username, password)
173184

@@ -215,14 +226,18 @@ def verifyResponse(self, success: bool = False, data: dict = {}) -> dict:
215226
:return: The combination of the default and overriden config.
216227
"""
217228
if success and data.ok:
218-
generic.debugLog(debug=self.debug, message="We have successful request, returning json data.")
219-
return data.json()
229+
try:
230+
return data.json()
231+
except:
232+
return data.content
220233
else:
221-
error = {
222-
"status": data.status_code,
223-
"headers": data.headers,
224-
"url": data.url,
225-
"text": data.text
226-
}
227-
print(error)
228-
sys.exit(1)
234+
error = {"error": True}
235+
if 'headers' in data:
236+
error['headers'] = data.headers
237+
if 'url' in data:
238+
error['url'] = data.url
239+
if 'text' in data:
240+
error['text'] = data.text
241+
if 'status_code' in data:
242+
error["status"] = data.status_code
243+
return(error)

requirements-djwasabi.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pytest-cov==2.11.1
2-
responses==0.12.1
3-
PyYAML==5.4.1
1+
pytest-cov==5.0.0
2+
responses==0.25.3
3+
PyYAML==6.0.1

requirements.txt

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
ansible-compat==2.2.1
2-
ansible-core==2.14.5
3-
ansible-lint==6.15.0
4-
ansible==7.5.0
1+
ansible-compat==24.7.0
2+
ansible-core==2.17.1
3+
ansible-lint==24.7.0
4+
ansible==10.1.0
55
dependencies==7.7.0
6-
docker==6.0.1
7-
dockerfile-parse==2.0.0
6+
docker==7.1.0
7+
dockerfile-parse==2.0.1
88
ipaddr==2.2.0
99
ipaddress==1.0.23
1010
molecule-docker==2.1.0
1111
molecule-vagrant==2.0.0
12-
molecule==5.0.0
13-
netaddr==0.8.0
14-
pytest-testinfra==7.0.0
12+
molecule==24.7.0
13+
netaddr==1.3.0
14+
pytest-testinfra==10.1.1

0 commit comments

Comments
 (0)