forked from timotheus/ebaysdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.py
316 lines (234 loc) · 9.36 KB
/
connection.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# -*- coding: utf-8 -*-
'''
© 2012-2013 eBay Software Foundation
Authored by: Tim Keefer
Licensed under CDDL 1.0
'''
from ebaysdk import log
import re
import time
import uuid
import webbrowser
from requests import Request, Session
from requests.adapters import HTTPAdapter
from xml.dom.minidom import parseString
from xml.parsers.expat import ExpatError
from ebaysdk import set_stream_logger, UserAgent
from ebaysdk.utils import getNodeText as getNodeTextUtils
from ebaysdk.utils import getValue
from ebaysdk.response import Response
from ebaysdk.exception import ConnectionError, ConnectionResponseError
HTTP_SSL = {
False: 'http',
True: 'https',
}
class BaseConnection(object):
"""Base Connection Class."""
def __init__(self, debug=False, method='GET',
proxy_host=None, timeout=20, proxy_port=80,
parallel=None, **kwargs):
if debug:
set_stream_logger()
self.response = None
self.request = None
self.verb = None
self.debug = debug
self.method = method
self.timeout = timeout
self.proxy_host = proxy_host
self.proxy_port = proxy_port
self.datetime_nodes = []
self._list_nodes = []
self.proxies = dict()
if self.proxy_host:
proxy = 'http://%s:%s' % (self.proxy_host, self.proxy_port)
self.proxies = {
'http': proxy,
'https': proxy
}
self.session = Session()
self.session.mount('http://', HTTPAdapter(max_retries=3))
self.session.mount('https://', HTTPAdapter(max_retries=3))
self.parallel = parallel
self.base_list_nodes = []
self.datetime_nodes = []
self._reset()
def debug_callback(self, debug_type, debug_message):
log.debug('type: ' + str(debug_type) + ' message' + str(debug_message))
def v(self, *args, **kwargs):
return getValue(self.response.dict(), *args, **kwargs)
def getNodeText(self, nodelist):
return getNodeTextUtils(nodelist)
def _reset(self):
self.response = None
self.request = None
self.verb = None
self._list_nodes = []
self._request_id = None
self._request_dict = {}
self._time = time.time()
self._response_content = None
self._response_dom = None
self._response_obj = None
self._response_soup = None
self._response_dict = None
self._response_error = None
self._resp_body_errors = []
self._resp_body_warnings = []
self._resp_codes = []
def _add_prefix(self, nodes, verb):
if verb:
for i, v in enumerate(nodes):
if not nodes[i].startswith(verb.lower()):
nodes[i] = "%sresponse.%s" % (verb.lower(), nodes[i].lower())
def execute(self, verb, data=None, list_nodes=[], verb_attrs=None):
"Executes the HTTP request."
log.debug('execute: verb=%s data=%s' % (verb, data))
self._reset()
self._list_nodes += list_nodes
self._add_prefix(self._list_nodes, verb)
if hasattr(self, 'base_list_nodes'):
self._list_nodes += self.base_list_nodes
self.build_request(verb, data, verb_attrs)
self.execute_request()
if hasattr(self.response, 'content'):
self.process_response()
self.error_check()
log.debug('total time=%s' % (time.time() - self._time))
return self.response
def build_request(self, verb, data, verb_attrs):
self.verb = verb
self._request_dict = data
self._request_id = uuid.uuid4()
url = "%s://%s%s" % (
HTTP_SSL[self.config.get('https', False)],
self.config.get('domain'),
self.config.get('uri')
)
headers = self.build_request_headers(verb)
headers.update({'User-Agent': UserAgent,
'X-EBAY-SDK-REQUEST-ID': str(self._request_id)})
request = Request(self.method,
url,
data=self.build_request_data(verb, data, verb_attrs),
headers=headers,
)
self.request = request.prepare()
def execute_request(self):
log.debug("REQUEST (%s): %s %s" \
% (self._request_id, self.request.method, self.request.url))
log.debug('headers=%s' % self.request.headers)
log.debug('body=%s' % self.request.body)
if self.parallel:
self.parallel._add_request(self)
return None
self.response = self.session.send(self.request,
verify=False,
proxies=self.proxies,
timeout=self.timeout,
allow_redirects=True
)
log.debug('RESPONSE (%s):' % self._request_id)
log.debug('elapsed time=%s' % self.response.elapsed)
log.debug('status code=%s' % self.response.status_code)
log.debug('headers=%s' % self.response.headers)
log.debug('content=%s' % self.response.text)
def process_response(self, parse_response=True):
"""Post processing of the response"""
self.response = Response(self.response,
verb=self.verb,
list_nodes=self._list_nodes,
datetime_nodes=self.datetime_nodes,
parse_response=parse_response)
# set for backward compatibility
self._response_content = self.response.content
if self.response.status_code != 200:
self._response_error = self.response.reason
def error_check(self):
estr = self.error()
if estr and self.config.get('errors', True):
log.error(estr)
raise ConnectionError(estr, self.response)
def response_codes(self):
return self._resp_codes
def response_status(self):
"Retuns the HTTP response status string."
return self.response.reason
def response_code(self):
"Returns the HTTP response status code."
return self.response.status_code
def response_content(self):
return self.response.content
def response_soup(self):
"Returns a BeautifulSoup object of the response."
if not self._response_soup:
try:
from bs4 import BeautifulStoneSoup
except ImportError:
from BeautifulSoup import BeautifulStoneSoup
log.warn('DeprecationWarning: BeautifulSoup 3 or earlier is deprecated; install bs4 instead\n')
self._response_soup = BeautifulStoneSoup(
self.response_content.decode('utf-8')
)
return self._response_soup
def response_obj(self):
log.warn('response_obj() DEPRECATED, use response.reply instead')
return self.response.reply
def response_dom(self):
""" Deprecated: use self.response.dom() instead
Returns the response DOM (xml.dom.minidom).
"""
log.warn('response_dom() DEPRECATED, use response.dom instead')
if not self._response_dom:
dom = None
content = None
try:
if self.response.content:
regex = re.compile(b'xmlns="[^"]+"')
content = regex.sub(b'', self.response.content)
else:
content = "<%sResponse></%sResponse>" % (self.verb, self.verb)
dom = parseString(content)
self._response_dom = dom.getElementsByTagName(
self.verb + 'Response')[0]
except ExpatError as e:
raise ConnectionResponseError("Invalid Verb: %s (%s)" % (self.verb, e), self.response)
except IndexError:
self._response_dom = dom
return self._response_dom
def response_dict(self):
"Returns the response dictionary."
log.warn('response_dict() DEPRECATED, use response.dict() or response.reply instead')
return self.response.reply
def response_json(self):
"Returns the response JSON."
log.warn('response_json() DEPRECATED, use response.json() instead')
return self.response.json()
def _get_resp_body_errors(self):
"""Parses the response content to pull errors.
Child classes should override this method based on what the errors in the
XML response body look like. They can choose to look at the 'ack',
'Errors', 'errorMessage' or whatever other fields the service returns.
the implementation below is the original code that was part of error()
"""
if self._resp_body_errors and len(self._resp_body_errors) > 0:
return self._resp_body_errors
errors = []
if self.verb is None:
return errors
dom = self.response.dom()
if dom is None:
return errors
return []
def error(self):
"Builds and returns the api error message."
error_array = []
if self._response_error:
error_array.append(self._response_error)
error_array.extend(self._get_resp_body_errors())
if len(error_array) > 0:
error_string = u"%s: %s" % (self.verb, u", ".join(error_array))
return error_string
return None
def opendoc(self):
webbrowser.open(self.config.get('doc_url'))