1
- #!/usr/bin/env python
1
+ #!/usr/bin/env python3
2
2
3
- import sys
4
3
import requests
5
- from . import generic
4
+ import urllib3
6
5
from requests .adapters import HTTPAdapter
7
6
from requests .packages .urllib3 .util .retry import Retry
8
7
from http .client import HTTPConnection
9
8
9
+ urllib3 .disable_warnings ()
10
10
11
11
class request ():
12
12
"""."""
@@ -83,6 +83,8 @@ def _get(self, url: str = None, headers: dict = {}, username: str = None, passwo
83
83
84
84
try :
85
85
return (True , self .http .get (url , ** kwargs ))
86
+ except requests .exceptions .SSLError as e :
87
+ return (False , {'error' : e })
86
88
except requests .exceptions .RequestException as e :
87
89
return (False , {'error' : e })
88
90
@@ -117,7 +119,7 @@ def _patch(self, url: str = None, headers: dict = {}, data: dict = {}, username:
117
119
except requests .exceptions .RequestException as e :
118
120
return (False , {'error' : e })
119
121
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 :
121
123
"""POST the information from provided url.
122
124
123
125
:param url: The URL we want to POST.
@@ -130,31 +132,38 @@ def _post(self, url: str = None, headers: dict = {}, data: dict = {}, username:
130
132
:type username: str
131
133
:param password: The password for the provided username.
132
134
:type password: str
135
+ :param files: The contents of the file to be posted.
136
+ :type files: obj
133
137
:rtype: tuple
134
138
:return: Succes (or not) with the request object
135
139
"""
136
140
if not url :
137
141
raise ValueError ('Please provide the URL.' )
138
-
139
142
kwargs = {
140
- "headers" : headers ,
141
- "data" : data
143
+ "headers" : headers
142
144
}
145
+
146
+ if len (data ) > 0 :
147
+ kwargs ['data' ] = data
143
148
if username is not None and password is not None :
144
149
kwargs ['auth' ] = (username , password )
150
+ if files is not None :
151
+ kwargs ['files' ] = files
145
152
146
153
try :
147
154
return (True , self .http .post (url , ** kwargs ))
148
155
except requests .exceptions .RequestException as e :
149
156
return (False , {'error' : e })
150
157
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 :
152
159
"""PUT the information from provided url.
153
160
154
161
:param url: The URL we want to PUT.
155
162
:type url: str
156
163
:param headers: The headers.
157
164
:type headers: dict
165
+ :param data: The data we want to PUT.
166
+ :type data: dict
158
167
:param username: The username that needs to be used when authentication is needed.
159
168
:type username: str
160
169
:param password: The password for the provided username.
@@ -168,6 +177,8 @@ def _put(self, url: str = None, headers: dict = {}, username: str = None, passwo
168
177
kwargs = {
169
178
"headers" : headers
170
179
}
180
+ if len (data ) > 0 :
181
+ kwargs ['data' ] = data
171
182
if username is not None and password is not None :
172
183
kwargs ['auth' ] = (username , password )
173
184
@@ -215,14 +226,18 @@ def verifyResponse(self, success: bool = False, data: dict = {}) -> dict:
215
226
:return: The combination of the default and overriden config.
216
227
"""
217
228
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
220
233
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 )
0 commit comments