| 1 |
# Copyright (c) 2013 Red Hat, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 |
# implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
import json |
| 17 |
|
| 18 |
from oslo_utils import importutils |
| 19 |
|
| 20 |
from zaqarclient.common import http |
| 21 |
from zaqarclient.transport import base |
| 22 |
from zaqarclient.transport import response |
| 23 |
|
| 24 |
osprofiler_web = importutils.try_import("osprofiler.web") |
| 25 |
|
| 26 |
|
| 27 |
class HttpTransport(base.Transport): |
| 28 |
|
| 29 |
def __init__(self, options): |
| 30 |
super().__init__(options) |
| 31 |
self.client = http.Client() |
| 32 |
|
| 33 |
def _prepare(self, request): |
| 34 |
if not request.api: |
| 35 |
return request.endpoint, 'GET', request |
| 36 |
|
| 37 |
# TODO(flaper87): Validate if the user |
| 38 |
# explicitly wants so. Validation must |
| 39 |
# happen before any other operation here. |
| 40 |
# request.validate() |
| 41 |
|
| 42 |
schema = {} |
| 43 |
ref_params = {} |
| 44 |
ref = request.ref |
| 45 |
|
| 46 |
if request.operation: |
| 47 |
schema = request.api.get_schema(request.operation) |
| 48 |
ref = ref or schema.get('ref', '') |
| 49 |
|
| 50 |
# FIXME(flaper87): We expect the endpoint |
| 51 |
# to have the API version label already, |
| 52 |
# however in a follow-your-nose implementation |
| 53 |
# it should be the other way around. |
| 54 |
ref = ref.lstrip('/' + request.api.label) |
| 55 |
|
| 56 |
for param in list(request.params.keys()): |
| 57 |
if '{{{0}}}'.format(param) in ref: |
| 58 |
value = request.params.pop(param) |
| 59 |
|
| 60 |
# NOTE(flaper87): Zaqar API parses |
| 61 |
# sequences encoded as '1,2,3,4'. Let's |
| 62 |
# encode lists, tuples and sets before |
| 63 |
# sending them to the server. |
| 64 |
if isinstance(value, (list, tuple, set)): |
| 65 |
value = ','.join(value) |
| 66 |
|
| 67 |
ref_params[param] = value |
| 68 |
|
| 69 |
url = '{}/{}/{}'.format(request.endpoint.rstrip('/'), |
| 70 |
request.api.label, |
| 71 |
ref.format(**ref_params)) |
| 72 |
return url, schema.get('method', 'GET'), request |
| 73 |
|
| 74 |
def send(self, request): |
| 75 |
url, method, request = self._prepare(request) |
| 76 |
|
| 77 |
# NOTE(flape87): Do not modify |
| 78 |
# request's headers directly. |
| 79 |
headers = request.headers.copy() |
| 80 |
if request.operation == 'queue_update': |
| 81 |
headers['content-type'] = \ |
| 82 |
'application/openstack-messaging-v2.0-json-patch' |
| 83 |
else: |
| 84 |
headers['content-type'] = 'application/json' |
| 85 |
|
| 86 |
if osprofiler_web: |
| 87 |
headers.update(osprofiler_web.get_trace_id_headers()) |
| 88 |
|
| 89 |
if request.verify: |
| 90 |
if request.cert: |
| 91 |
verify = request.cert |
| 92 |
else: |
| 93 |
verify = True |
| 94 |
else: |
| 95 |
verify = False |
| 96 |
resp = self.client.request(method, |
| 97 |
url=url, |
| 98 |
params=request.params, |
| 99 |
headers=headers, |
| 100 |
data=request.content, |
| 101 |
verify=verify) |
| 102 |
|
| 103 |
if resp.status_code in self.http_to_zaqar: |
| 104 |
kwargs = {} |
| 105 |
try: |
| 106 |
error_body = json.loads(resp.text) |
| 107 |
kwargs['title'] = error_body['title'] |
| 108 |
kwargs['description'] = error_body['description'] |
| 109 |
except Exception: |
| 110 |
# TODO(flaper87): Log this exception |
| 111 |
# but don't stop raising the corresponding |
| 112 |
# exception |
| 113 |
# Note(Eva-i): most of the error responses from Zaqar have |
| 114 |
# dict with title and description in their bodies. If it's not |
| 115 |
# the case, let's just show body text. |
| 116 |
kwargs['text'] = resp.text |
| 117 |
raise self.http_to_zaqar[resp.status_code](**kwargs) |
| 118 |
|
| 119 |
# NOTE(flaper87): This reads the whole content |
| 120 |
# and will consume any attempt of streaming. |
| 121 |
return response.Response(request, resp.text, |
| 122 |
headers=resp.headers, |
| 123 |
status_code=resp.status_code) |