| 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 |
|
| 19 |
class Response: |
| 20 |
"""Common response class for Zaqarclient. |
| 21 |
|
| 22 |
All `zaqarclient.transport.base.Transport` implementations |
| 23 |
will return this to the higher level API which will then build |
| 24 |
an object out of it. |
| 25 |
|
| 26 |
:param request: The request sent to the server. |
| 27 |
:type: `zaqarclient.transport.request.Request` |
| 28 |
:param content: Response's content |
| 29 |
:type: str |
| 30 |
:param headers: Optional headers returned in the response. |
| 31 |
:type: dict |
| 32 |
:param status_code: Optional status_code returned in the response. |
| 33 |
:type: `int` |
| 34 |
""" |
| 35 |
|
| 36 |
__slots__ = ('request', 'content', 'headers', 'status_code', |
| 37 |
'_deserialized') |
| 38 |
|
| 39 |
def __init__(self, request, content, headers=None, status_code=None): |
| 40 |
self.request = request |
| 41 |
self.content = content |
| 42 |
self.headers = headers or {} |
| 43 |
self.status_code = status_code |
| 44 |
|
| 45 |
self._deserialized = None |
| 46 |
|
| 47 |
@property |
| 48 |
def deserialized_content(self): |
| 49 |
try: |
| 50 |
if not self._deserialized and self.content: |
| 51 |
self._deserialized = json.loads(self.content) |
| 52 |
return self._deserialized |
| 53 |
except ValueError as ex: |
| 54 |
print("Response is not a JSON object.", ex) |
| 55 |
return None |