diff --git a/marconi/common/request.py b/marconi/common/request.py new file mode 100644 index 000000000..74facadca --- /dev/null +++ b/marconi/common/request.py @@ -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 diff --git a/marconi/common/response.py b/marconi/common/response.py new file mode 100644 index 000000000..34b3b7110 --- /dev/null +++ b/marconi/common/response.py @@ -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 {} diff --git a/tests/unit/common/test_request.py b/tests/unit/common/test_request.py new file mode 100644 index 000000000..e692d3345 --- /dev/null +++ b/tests/unit/common/test_request.py @@ -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)