|
27 | 27 |
|
28 | 28 | def wrap_raise_for_status(http_client):
|
29 | 29 |
|
30 |
| - def wrapper(response_instance): |
31 |
| - |
32 |
| - raise_for_status = response_instance.raise_for_status |
33 |
| - |
34 |
| - def wrapped(): |
35 |
| - try: |
36 |
| - raise_for_status(allow_redirects=False) |
37 |
| - except requests.HTTPError, ex: |
38 |
| - if ex.response.status_code in REDIRECT_STATI: |
39 |
| - redirection = exc.MoreInformationRequiredError('%s' % ex) |
40 |
| - redirection.status_code = ex.response.status_code |
41 |
| - redirection.response = ex.response |
42 |
| - redirection.redirect_uri = ex.response.headers['Location'] |
43 |
| - raise redirection |
44 |
| - deserialized = http_client.deserialize( |
45 |
| - response_instance |
46 |
| - ) |
47 |
| - response_instance.deserialized = deserialized |
48 |
| - extra = deserialized.get('additional') or '' |
49 |
| - if extra: |
50 |
| - extra = ' -- {0}.'.format(extra) |
51 |
| - error_msg = '{name}: {code}: {msg} {extra}'.format( |
52 |
| - name=deserialized['status'], |
53 |
| - code=deserialized['status_code'], |
54 |
| - msg=deserialized['description'].encode('utf8'), |
55 |
| - extra=extra.encode('utf8'), |
56 |
| - ) |
57 |
| - category_code = deserialized.get('category_code', None) |
58 |
| - error_cls = exc.category_code_map.get( |
59 |
| - category_code, exc.HTTPError) |
60 |
| - http_error = error_cls(error_msg) |
61 |
| - for error, value in deserialized.iteritems(): |
62 |
| - setattr(http_error, error, value) |
63 |
| - raise http_error |
64 |
| - |
65 |
| - response_instance.raise_for_status = wrapped |
| 30 | + def handle_exception(response): |
| 31 | + deserialized = http_client.deserialize( |
| 32 | + response |
| 33 | + ) |
| 34 | + response.deserialized = deserialized |
| 35 | + extra = deserialized.get('additional') or '' |
| 36 | + if extra: |
| 37 | + extra = ' -- {0}.'.format(extra) |
| 38 | + error_msg = '{name}: {code}: {msg} {extra}'.format( |
| 39 | + name=deserialized['status'], |
| 40 | + code=deserialized['status_code'], |
| 41 | + msg=deserialized['description'].encode('utf8'), |
| 42 | + extra=extra.encode('utf8'), |
| 43 | + ) |
| 44 | + category_code = deserialized.get('category_code', None) |
| 45 | + error_cls = exc.category_code_map.get( |
| 46 | + category_code, exc.HTTPError) |
| 47 | + http_error = error_cls(error_msg) |
| 48 | + for error, value in deserialized.iteritems(): |
| 49 | + setattr(http_error, error, value) |
| 50 | + raise http_error |
| 51 | + |
| 52 | + def handle_redirect(response): |
| 53 | + reason = '%s Client Error: %s' % ( |
| 54 | + response.status_code, |
| 55 | + response.reason, |
| 56 | + ) |
| 57 | + redirection = exc.MoreInformationRequiredError(reason) |
| 58 | + redirection.status_code = response.status_code |
| 59 | + redirection.response = response |
| 60 | + redirection.redirect_uri = response.headers['Location'] |
| 61 | + raise redirection |
| 62 | + |
| 63 | + def wrapper(response): |
| 64 | + |
| 65 | + try: |
| 66 | + response.raise_for_status() |
| 67 | + except requests.HTTPError: |
| 68 | + handle_exception(response) |
| 69 | + else: |
| 70 | + if response.status_code in REDIRECT_STATI: |
| 71 | + handle_redirect(response) |
66 | 72 |
|
67 | 73 | return wrapper
|
68 | 74 |
|
@@ -103,8 +109,11 @@ def make_absolute_url(client, url, **kwargs):
|
103 | 109 | request_body.update(fixed_up_body)
|
104 | 110 | kwargs['data'] = request_body
|
105 | 111 | # TODO: merge config dictionaries if it exists.
|
106 |
| - kwargs['config'] = client.config.requests.copy() |
| 112 | + headers = kwargs.pop('headers', {}) |
| 113 | + headers.update(client.config.requests['base_headers']) |
| 114 | + kwargs['headers'] = headers |
107 | 115 | kwargs['allow_redirects'] = False
|
| 116 | + |
108 | 117 | kwargs['hooks'] = {
|
109 | 118 | 'response': wrap_raise_for_status(client)
|
110 | 119 | }
|
@@ -137,31 +146,28 @@ def __init__(self, keep_alive=True, *args, **kwargs):
|
137 | 146 | def get(self, uri, **kwargs):
|
138 | 147 | kwargs = self.serialize(kwargs.copy())
|
139 | 148 | resp = self.interface.get(uri, **kwargs)
|
140 |
| - if kwargs.get('return_response', True): |
141 |
| - resp.deserialized = self.deserialize(resp) |
| 149 | + resp.deserialized = self.deserialize(resp) |
142 | 150 | return resp
|
143 | 151 |
|
144 | 152 | @munge_request
|
145 | 153 | def post(self, uri, data=None, **kwargs):
|
146 | 154 | data = self.serialize({'data': data}).pop('data')
|
147 | 155 | resp = self.interface.post(uri, data=data, **kwargs)
|
148 |
| - if kwargs.get('return_response', True): |
149 |
| - resp.deserialized = self.deserialize(resp) |
| 156 | + resp.deserialized = self.deserialize(resp) |
150 | 157 | return resp
|
151 | 158 |
|
152 | 159 | @munge_request
|
153 | 160 | def put(self, uri, data=None, **kwargs):
|
154 | 161 | data = self.serialize({'data': data}).pop('data')
|
155 | 162 | resp = self.interface.put(uri, data=data, **kwargs)
|
156 |
| - if kwargs.get('return_response', True): |
157 |
| - resp.deserialized = self.deserialize(resp) |
| 163 | + resp.deserialized = self.deserialize(resp) |
158 | 164 | return resp
|
159 | 165 |
|
160 | 166 | @munge_request
|
161 | 167 | def delete(self, uri, **kwargs):
|
162 | 168 | kwargs = self.serialize(kwargs.copy())
|
163 | 169 | resp = self.interface.delete(uri, **kwargs)
|
164 |
| - if kwargs.get('return_response', True) and resp.status_code != 204: |
| 170 | + if resp.status_code != 204: |
165 | 171 | resp.deserialized = self.deserialize(resp)
|
166 | 172 | return resp
|
167 | 173 |
|
|
0 commit comments