forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransports.py
More file actions
299 lines (236 loc) · 9.8 KB
/
transports.py
File metadata and controls
299 lines (236 loc) · 9.8 KB
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
"""
SoftLayer.transports
~~~~~~~~~~~~~~~~~~~~
XML-RPC transport layer that uses the requests library.
:license: MIT, see LICENSE for more details.
"""
from SoftLayer import consts
from SoftLayer import exceptions
from SoftLayer import utils
import importlib
import json
import logging
import time
import requests
LOGGER = logging.getLogger(__name__)
# transports.Request does have a lot of instance attributes. :(
# pylint: disable=too-many-instance-attributes
__all__ = [
'Request',
'XmlRpcTransport',
'RestTransport',
'TimingTransport',
'FixtureTransport',
]
class Request(object):
"""Transport request object."""
def __init__(self):
#: API service name. E.G. SoftLayer_Account
self.service = None
#: API method name. E.G. getObject
self.method = None
#: API Parameters.
self.args = tuple()
#: API headers, used for authentication, masks, limits, offsets, etc.
self.headers = {}
#: Transport user.
self.transport_user = None
#: Transport password.
self.transport_password = None
#: Transport headers.
self.transport_headers = {}
#: Boolean specifying if the server certificate should be verified.
self.verify = True
#: Client certificate file path.
self.cert = None
#: InitParameter/identifier of an object.
self.identifier = None
#: SoftLayer mask (dict or string).
self.mask = None
#: SoftLayer Filter (dict).
self.filter = None
#: Integer result limit.
self.limit = None
#: Integer result offset.
self.offset = None
class XmlRpcTransport(object):
"""XML-RPC transport."""
def __init__(self,
endpoint_url=None,
timeout=None,
proxy=None,
user_agent=None):
self.endpoint_url = (endpoint_url or
consts.API_PUBLIC_ENDPOINT).rstrip('/')
self.timeout = timeout or None
self.proxy = proxy
self.user_agent = user_agent or consts.USER_AGENT
def __call__(self, request):
"""Makes a SoftLayer API call against the XML-RPC endpoint.
:param request request: Request object
"""
largs = list(request.args)
headers = request.headers
if request.identifier is not None:
header_name = request.service + 'InitParameters'
headers[header_name] = {'id': request.identifier}
if request.mask is not None:
headers.update(_format_object_mask(request.mask, request.service))
if request.filter is not None:
headers['%sObjectFilter' % request.service] = request.filter
if request.limit:
headers['resultLimit'] = {
'limit': request.limit,
'offset': request.offset or 0,
}
largs.insert(0, {'headers': headers})
request.transport_headers.setdefault('Content-Type', 'application/xml')
request.transport_headers.setdefault('User-Agent', self.user_agent)
url = '/'.join([self.endpoint_url, request.service])
payload = utils.xmlrpc_client.dumps(tuple(largs),
methodname=request.method,
allow_none=True)
LOGGER.debug("=== REQUEST ===")
LOGGER.info('POST %s', url)
LOGGER.debug(request.transport_headers)
LOGGER.debug(payload)
try:
response = requests.request('POST', url,
data=payload,
headers=request.transport_headers,
timeout=self.timeout,
verify=request.verify,
cert=request.cert,
proxies=_proxies_dict(self.proxy))
LOGGER.debug("=== RESPONSE ===")
LOGGER.debug(response.headers)
LOGGER.debug(response.content)
response.raise_for_status()
return utils.xmlrpc_client.loads(response.content)[0][0]
except utils.xmlrpc_client.Fault as ex:
# These exceptions are formed from the XML-RPC spec
# http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
error_mapping = {
'-32700': exceptions.NotWellFormed,
'-32701': exceptions.UnsupportedEncoding,
'-32702': exceptions.InvalidCharacter,
'-32600': exceptions.SpecViolation,
'-32601': exceptions.MethodNotFound,
'-32602': exceptions.InvalidMethodParameters,
'-32603': exceptions.InternalError,
'-32500': exceptions.ApplicationError,
'-32400': exceptions.RemoteSystemError,
'-32300': exceptions.TransportError,
}
_ex = error_mapping.get(ex.faultCode, exceptions.SoftLayerAPIError)
raise _ex(ex.faultCode, ex.faultString)
except requests.HTTPError as ex:
raise exceptions.TransportError(ex.response.status_code, str(ex))
except requests.RequestException as ex:
raise exceptions.TransportError(0, str(ex))
class RestTransport(object):
"""REST transport.
Currently only supports GET requests (no POST, PUT, DELETE) and lacks
support for masks, filters, limits and offsets.
"""
def __init__(self,
endpoint_url=None,
timeout=None,
proxy=None,
user_agent=None):
self.endpoint_url = (endpoint_url or
consts.API_PUBLIC_ENDPOINT_REST).rstrip('/')
self.timeout = timeout or None
self.proxy = proxy
self.user_agent = user_agent or consts.USER_AGENT
def __call__(self, request):
"""Makes a SoftLayer API call against the REST endpoint.
This currently only works with GET requests
:param request request: Request object
"""
url_parts = [self.endpoint_url, request.service]
if request.identifier is not None:
url_parts.append(str(request.identifier))
if request.method is not None:
url_parts.append(request.method)
for arg in request.args:
url_parts.append(str(arg))
request.transport_headers.setdefault('Content-Type',
'application/json')
request.transport_headers.setdefault('User-Agent', self.user_agent)
url = '%s.%s' % ('/'.join(url_parts), 'json')
LOGGER.debug("=== REQUEST ===")
LOGGER.info(url)
LOGGER.debug(request.transport_headers)
try:
resp = requests.request('GET', url,
headers=request.transport_headers,
timeout=self.timeout,
verify=request.verify,
cert=request.cert,
proxies=_proxies_dict(self.proxy))
LOGGER.debug("=== RESPONSE ===")
LOGGER.debug(resp.headers)
LOGGER.debug(resp.content)
resp.raise_for_status()
return json.loads(resp.content)
except requests.HTTPError as ex:
content = json.loads(ex.response.content)
raise exceptions.SoftLayerAPIError(ex.response.status_code,
content['error'])
except requests.RequestException as ex:
raise exceptions.TransportError(0, str(ex))
class TimingTransport(object):
"""Transport that records API call timings."""
def __init__(self, transport):
self.transport = transport
self.last_calls = []
def __call__(self, call):
"""See Client.call for documentation."""
start_time = time.time()
result = self.transport(call)
end_time = time.time()
self.last_calls.append((call, start_time, end_time - start_time))
return result
def get_last_calls(self):
"""Retrieves the last_calls property.
This property will contain a list of tuples in the form
(Request, initiated_utc_timestamp, execution_time)
"""
last_calls = self.last_calls
self.last_calls = []
return last_calls
class FixtureTransport(object):
"""Implements a transport which returns fixtures."""
def __call__(self, call):
"""Load fixture from the default fixture path."""
try:
module_path = 'SoftLayer.fixtures.%s' % call.service
module = importlib.import_module(module_path)
except ImportError:
raise NotImplementedError('%s fixture is not implemented'
% call.service)
try:
return getattr(module, call.method)
except AttributeError:
raise NotImplementedError('%s::%s fixture is not implemented'
% (call.service, call.method))
def _proxies_dict(proxy):
"""Makes a proxy dict appropriate to pass to requests."""
if not proxy:
return None
return {'http': proxy, 'https': proxy}
def _format_object_mask(objectmask, service):
"""Format new and old style object masks into proper headers.
:param objectmask: a string- or dict-based object mask
:param service: a SoftLayer API service name
"""
if isinstance(objectmask, dict):
mheader = '%sObjectMask' % service
else:
mheader = 'SoftLayer_ObjectMask'
objectmask = objectmask.strip()
if (not objectmask.startswith('mask') and
not objectmask.startswith('[')):
objectmask = "mask[%s]" % objectmask
return {mheader: {'mask': objectmask}}