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
101 lines (91 loc) · 3.67 KB
/
transports.py
File metadata and controls
101 lines (91 loc) · 3.67 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
"""
SoftLayer.transports
~~~~~~~~~~~~~~~~~~~~
XML-RPC transport layer that uses the requests library.
:license: MIT, see LICENSE for more details.
"""
from SoftLayer.exceptions import (
SoftLayerAPIError, NotWellFormed, UnsupportedEncoding, InvalidCharacter,
SpecViolation, MethodNotFound, InvalidMethodParameters, InternalError,
ApplicationError, RemoteSystemError, TransportError)
import xmlrpclib
import logging
import requests
import json
log = logging.getLogger(__name__)
def make_xml_rpc_api_call(uri, method, args=None, headers=None,
http_headers=None, timeout=None):
""" Makes a SoftLayer API call against the XML-RPC endpoint
:param string uri: endpoint URL
:param string method: method to call E.G.: 'getObject'
:param dict headers: XML-RPC headers to use for the request
:param dict http_headers: HTTP headers to use for the request
:param int timeout: number of seconds to use as a timeout
"""
if args is None:
args = tuple()
try:
largs = list(args)
largs.insert(0, {'headers': headers})
payload = xmlrpclib.dumps(tuple(largs), methodname=method,
allow_none=True)
session = requests.Session()
req = requests.Request('POST', uri, data=payload,
headers=http_headers).prepare()
log.debug("=== REQUEST ===")
log.info('POST %s', uri)
log.debug(req.headers)
log.debug(payload)
response = session.send(req, timeout=timeout)
log.debug("=== RESPONSE ===")
log.debug(response.headers)
log.debug(response.content)
response.raise_for_status()
result = xmlrpclib.loads(response.content,)[0][0]
return result
except xmlrpclib.Fault as e:
# These exceptions are formed from the XML-RPC spec
# http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
error_mapping = {
'-32700': NotWellFormed,
'-32701': UnsupportedEncoding,
'-32702': InvalidCharacter,
'-32600': SpecViolation,
'-32601': MethodNotFound,
'-32602': InvalidMethodParameters,
'-32603': InternalError,
'-32500': ApplicationError,
'-32400': RemoteSystemError,
'-32300': TransportError,
}
raise error_mapping.get(e.faultCode, SoftLayerAPIError)(
e.faultCode, e.faultString)
except requests.HTTPError as e:
raise TransportError(e.response.status_code, str(e))
except requests.RequestException as e:
raise TransportError(0, str(e))
def make_rest_api_call(method, url, http_headers=None, timeout=None):
""" Makes a SoftLayer API call against the REST endpoint
:param string method: HTTP method: GET, POST, PUT, DELETE
:param string url: endpoint URL
:param dict http_headers: HTTP headers to use for the request
:param int timeout: number of seconds to use as a timeout
"""
log.info('%s %s', method, url)
try:
resp = requests.request(
method, url, headers=http_headers, timeout=timeout)
resp.raise_for_status()
log.debug(resp.content)
if url.endswith('.json'):
return json.loads(resp.content)
else:
return resp.text
except requests.HTTPError as e:
if url.endswith('.json'):
content = json.loads(e.response.content)
raise SoftLayerAPIError(e.response.status_code, content['error'])
else:
raise SoftLayerAPIError(e.response.status_code, e.response.text)
except requests.RequestException as e:
raise TransportError(0, str(e))