forked from gibizer/openstack-rest-proxy-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.py
More file actions
65 lines (52 loc) · 1.88 KB
/
proxy.py
File metadata and controls
65 lines (52 loc) · 1.88 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
import json
import logging
import proxy
from openstack_rest_proxy import adapters
LOG = logging.getLogger(__name__)
class ResponseAdapterPlugin(proxy.HttpProxyBasePlugin):
"""Proxy plugin that converts OpenStack API responses
"""
# Response adapters are executed in the define order. The adapters are
# chained so the next adapter always works on the output of the previous
# adapter
RESPONSE_ADAPTERS = [adapters.RemovePasswordExpiresAt()]
def before_upstream_connection(self):
return False
def on_upstream_connection(self):
pass
def handle_upstream_response(self, raw):
rsp = proxy.HttpParser(proxy.HttpParserTypes.RESPONSE_PARSER)
rsp.parse(raw)
if rsp.body is not None:
try:
json_body = json.loads(rsp.body)
json_body = self._apply_adapters(rsp, json_body)
rsp.body = json.dumps(json_body).encode("utf-8")
except json.JSONDecodeError:
# Adapters only support responses with json body at the moment
# other responses transparently returned
pass
return proxy.build_http_response(
status_code=int(rsp.code),
headers={
rsp.headers[k][0]: rsp.headers[k][1] for k in rsp.headers
},
body=rsp.body,
)
def on_upstream_connection_close(self):
pass
def _apply_adapters(self, rsp, json_body):
code = rsp.code
headers = rsp.headers
for adapter in self.RESPONSE_ADAPTERS:
code, headers, json_body = adapter.adapt(
self.request.method,
self.request.url,
self.request.headers,
code,
headers,
json_body,
)
rsp.code = code
rsp.headers = headers
return json_body