This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
request.py
280 lines (218 loc) · 9.57 KB
/
request.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import os
import requests
import json
from .exception import *
#
# The MIT License (MIT)
#
# Copyright (C) 2014 hellosign.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
class HSRequest(object):
''' Object to handle HTTP requests
Although we have greate requests package which can handle the HTTP request
beautifully, we need this class to fit better our need like sending the
requests with authentication information, download files, check HTTP
errors...
Attributes:
DEFAULT_ENCODING (str): Default encoding for requests
USER_AGENT (str): HTTP User agent used when sending requests
parameters (dict): Some parameters for GET requests
headers (dict): Custome headers for every requests
http_status_code (int): HTTP status code returned of requests
'''
DEFAULT_ENCODING = "UTF-8"
USER_AGENT = "hellosign-python-sdk"
parameters = None
http_status_code = 0
verify_ssl = True
warnings = None
response_callback = None
headers = None
def __init__(self, auth, env="production"):
self.auth = auth
self.env = env
self.debug = (self.env != 'production')
self.verify_ssl = (not self.debug)
def get_warnings(self):
''' Return the list of warnings associated with this request, or None if there aren't any '''
if self.warnings and len(self.warnings) > 0:
return self.warnings
def get_file(self, url, path_or_file=None, headers=None, filename=None):
''' Get a file from a url and save it as `filename`
Args:
url (str): URL to send the request to
path_or_file (str or file): A writable File-like object or a path to save the file to.
filename (str): [DEPRECATED] File name to save the file as, this can be either
a full path or a relative path
headers (str, optional): custom headers
Returns:
True if file is downloaded and written successfully, False
otherwise.
'''
path_or_file = path_or_file or filename
if self.debug:
print("GET FILE: %s, headers=%s" % (url, headers))
self.headers = self._get_default_headers()
if headers is not None:
self.headers.update(headers)
response = requests.get(url, headers=self.headers, auth=self.auth, verify=self.verify_ssl)
self.http_status_code = response.status_code
try:
# No need to check for warnings here
self._check_error(response)
try:
path_or_file.write(response.content)
except AttributeError:
fd = os.open(path_or_file, os.O_CREAT | os.O_RDWR)
with os.fdopen(fd, "w+b") as f:
f.write(response.content)
except:
return False
return True
def get(self, url, headers=None, parameters=None, get_json=True):
''' Send a GET request with custome headers and parameters
Args:
url (str): URL to send the request to
headers (str, optional): custom headers
parameters (str, optional): optional parameters
Returns:
A JSON object of the returned response if `get_json` is True,
Requests' response object otherwise
'''
if self.debug:
print("GET: %s, headers=%s" % (url, headers))
self.headers = self._get_default_headers()
get_parameters = self.parameters
if get_parameters is None:
# In case self.parameters is still empty
get_parameters = {}
if headers is not None:
self.headers.update(headers)
if parameters is not None:
get_parameters.update(parameters)
response = requests.get(url, headers=self.headers, params=get_parameters, auth=self.auth, verify=self.verify_ssl)
json_response = self._process_json_response(response)
return json_response if get_json is True else response
def post(self, url, data=None, files=None, headers=None, get_json=True):
''' Make POST request to a url
Args:
url (str): URL to send the request to
data (dict, optional): Data to send
files (dict, optional): Files to send with the request
headers (str, optional): custom headers
Returns:
A JSON object of the returned response if `get_json` is True,
Requests' response object otherwise
'''
if self.debug:
print("POST: %s, headers=%s" % (url, headers))
self.headers = self._get_default_headers()
if headers is not None:
self.headers.update(headers)
response = requests.post(url, headers=self.headers, data=data, auth=self.auth, files=files, verify=self.verify_ssl)
json_response = self._process_json_response(response)
return json_response if get_json is True else response
#### HELPERS ########################################
def _get_json_response(self, resp):
''' Parse a JSON response '''
if resp is not None and resp.text is not None:
try:
text = resp.text.strip('\n')
if len(text) > 0:
return json.loads(text)
except ValueError as e:
if self.debug:
print("Could not decode JSON response: \"%s\"" % resp.text)
raise e
@classmethod
def _get_user_agent(cls):
''' Get the user agent to be sent '''
from hellosign_sdk import HSClient
return cls.USER_AGENT + '/' + HSClient.version
def _get_default_headers(self):
''' Return the default headers to send '''
return {
'User-Agent': HSRequest._get_user_agent()
}
def _process_json_response(self, response):
''' Process a given response '''
json_response = self._get_json_response(response)
if self.response_callback is not None:
json_response = self.response_callback(json_response)
response._content = json.dumps(json_response)
self.http_status_code = response.status_code
self._check_error(response, json_response)
self._check_warnings(json_response)
return json_response
def _check_error(self, response, json_response=None):
''' Check for HTTP error code from the response, raise exception if there's any
Args:
response (object): Object returned by requests' `get` and `post`
methods
json_response (dict): JSON response, if applicable
Raises:
HTTPError: If the status code of response is either 4xx or 5xx
Returns:
True if status code is not error code
'''
# If status code is 4xx or 5xx, that should be an error
if response.status_code >= 400:
json_response = json_response or self._get_json_response(response)
err_cls = self._check_http_error_code(response.status_code)
try:
raise err_cls("%s error: %s" % (response.status_code, json_response["error"]["error_msg"]), response.status_code)
# This is to catch error when we post get oauth data
except TypeError:
raise err_cls("%s error: %s" % (response.status_code, json_response["error_description"]), response.status_code)
# Return True if everything is OK
return True
def _check_warnings(self, json_response):
''' Extract warnings from the response to make them accessible
Args:
json_response (dict): JSON response
'''
self.warnings = None
if json_response:
self.warnings = json_response.get('warnings')
if self.debug and self.warnings:
for w in self.warnings:
print("WARNING: %s - %s" % (w['warning_name'], w['warning_msg']))
def _check_http_error_code(self, code):
return {
400: BadRequest,
401: Unauthorized,
402: PaymentRequired,
403: Forbidden,
404: NotFound,
405: MethodNotAllowed,
406: NotAcceptable,
408: RequestTimeout,
409: Conflict,
410: Gone,
414: RequestURITooLong,
415: UnsupportedMediaType,
416: RequestedRangeNotSatisfiable,
500: InternalServerError,
501: MethodNotImplemented,
502: BadGateway,
503: ServiceUnavailable,
504: GatewayTimeout
}.get(code, HTTPError)