Added a request and response file to common
Added two files (request.py and response.py) to marconi/common that will stage communication between the transport layer and new API layer. Also added a test to request in tests/unit/common. Partially-Implements: cross-transport-api-spec Change-Id: I2d67a15fa8c6aef23ebcde6a71dda3fcb723873a
This commit is contained in:
parent
9c87eb244d
commit
db4ac0d020
57
marconi/common/request.py
Normal file
57
marconi/common/request.py
Normal file
@ -0,0 +1,57 @@
|
||||
# Copyright (c) 2013 Rackspace, Inc.
|
||||
# Copyright (c) 2013 Red Hat, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
|
||||
from marconi.common import decorators
|
||||
|
||||
|
||||
class Request(object):
|
||||
"""General data for a Marconi request
|
||||
|
||||
Transport will generate a request object and send to this the API to be
|
||||
processed.
|
||||
:param operation: Operation to identify the API call being processed, i.e:
|
||||
- get_queues
|
||||
- get_messages
|
||||
:type operation: str
|
||||
:param content: Request's body. Default: None
|
||||
:type content: str
|
||||
:param params: Query string params. Default: None
|
||||
:type params: dict
|
||||
:param headers: Request headers. Default: None
|
||||
:type headers: dict
|
||||
:param api: Api entry point. i.e: 'queues.v1'
|
||||
:type api: `six.text_type`.
|
||||
"""
|
||||
|
||||
def __init__(self, operation='',
|
||||
content=None, params=None,
|
||||
headers=None, api=None):
|
||||
|
||||
self._api = None
|
||||
self._api_mod = api
|
||||
|
||||
self.operation = operation
|
||||
self.content = content
|
||||
self.params = params or {}
|
||||
self.headers = headers or {}
|
||||
|
||||
@decorators.lazy_property()
|
||||
def deserialized_content(self):
|
||||
if self.content is not None:
|
||||
return json.loads(self.content)
|
||||
return None
|
37
marconi/common/response.py
Normal file
37
marconi/common/response.py
Normal file
@ -0,0 +1,37 @@
|
||||
# Copyright (c) 2013 Red Hat, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
class Response(object):
|
||||
"""Common response class for Marconi.
|
||||
|
||||
All `marconi.transport.base.Transport` implementations
|
||||
will return this to the higher level API which will then build
|
||||
an object out of it.
|
||||
|
||||
:param request: The request sent to the server.
|
||||
:type: `marconi.transport.request.Request`
|
||||
:param content: Response's content
|
||||
:type: `six.string_types`
|
||||
:param headers: Optional headers returned in the response.
|
||||
:type: dict
|
||||
"""
|
||||
|
||||
__slots__ = ('request', 'content', 'headers')
|
||||
|
||||
def __init__(self, request, content, headers=None):
|
||||
self.request = request
|
||||
self.content = content
|
||||
self.headers = headers or {}
|
30
tests/unit/common/test_request.py
Normal file
30
tests/unit/common/test_request.py
Normal file
@ -0,0 +1,30 @@
|
||||
# Copyright (c) 2013 Rackspace, Inc.
|
||||
# Copyright (c) 2013 Red Hat, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from marconi.common import request
|
||||
from marconi.tests import base
|
||||
|
||||
|
||||
HREF = '/v1/queue/'
|
||||
|
||||
|
||||
class TestRequest(base.TestBase):
|
||||
|
||||
def test_request_deserialized(self):
|
||||
data = '{"data": "tons of GBs"}'
|
||||
req = request.Request(content=data)
|
||||
self.assertIsInstance(req.deserialized_content, dict)
|
Loading…
Reference in New Issue
Block a user