Example #1
0
    def test_next_page(self):
        messages = {
            'links': [],
            'messages': [{
                'href': '/v1/queues/mine/messages/123123423',
                'ttl': 800,
                'age': 790,
                'body': {
                    'event': 'ActivateAccount',
                    'mode': 'active'
                }
            }]
        }

        with mock.patch.object(self.transport, 'send',
                               autospec=True) as send_method:

            resp = response.Response(None, json.dumps(messages))
            send_method.return_value = resp

            # NOTE(flaper87): The first iteration will return 1 message
            # and then call `_next_page` which will use the rel-next link
            # to get a new set of messages.
            link = {
                'rel': 'next',
                'href': "/v1/queues/mine/messages?marker=6244-244224-783"
            }
            messages['links'].append(link)

            iterator = message._MessageIterator(self.queue, messages)
            iterated = [msg for msg in iterator]
            self.assertEqual(len(iterated), 2)
    def test_next_page(self):
        messages = {
            "links": [],
            "messages": [
                {
                    "href": "/v1/queues/mine/messages/123123423",
                    "ttl": 800,
                    "age": 790,
                    "body": {"event": "ActivateAccount", "mode": "active"},
                }
            ],
        }

        with mock.patch.object(self.transport, "send", autospec=True) as send_method:

            resp = response.Response(None, json.dumps(messages))
            send_method.return_value = resp

            # NOTE(flaper87): The first iteration will return 1 message
            # and then call `_next_page` which will use the rel-next link
            # to get a new set of messages.
            link = {"rel": "next", "href": "/v1/queues/mine/messages?marker=6244-244224-783"}
            messages["links"].append(link)

            iterator = message._MessageIterator(self.queue, messages)
            iterated = [msg for msg in iterator]
            self.assertEqual(len(iterated), 2)
    def test_no_next_iteration(self):
        messages = {
            "links": [],
            "messages": [
                {
                    "href": "/v1/queues/mine/messages/123123423",
                    "ttl": 800,
                    "age": 790,
                    "body": {"event": "ActivateAccount", "mode": "active"},
                }
            ],
        }

        iterator = message._MessageIterator(self.queue, messages)
        iterated = [msg for msg in iterator]
        self.assertEqual(len(iterated), 1)
Example #4
0
    def test_no_next_iteration(self):
        messages = {
            'links': [],
            'messages': [{
                'href': '/v1/queues/mine/messages/123123423',
                'ttl': 800,
                'age': 790,
                'body': {
                    'event': 'ActivateAccount',
                    'mode': 'active'
                }
            }]
        }

        iterator = message._MessageIterator(self.queue, messages)
        iterated = [msg for msg in iterator]
        self.assertEqual(len(iterated), 1)
Example #5
0
    def messages(self, *messages, **params):
        """Gets a list of messages from the server

        This method returns a list of messages, it can be
        used to retrieve a set of messages by id or to
        walk through the active messages by using the
        collection endpoint.

        The `messages` and `params` params are mutually exclusive
        and the former has the priority.

        :param messages: List of messages' ids to retrieve.
        :type messages: *args of `six.string_type`

        :param params: Filters to use for getting messages
        :type params: **kwargs dict.

        :returns: List of messages
        :rtype: `list`
        """
        req, trans = self.client._request_and_transport()

        # TODO(flaper87): Return a MessageIterator.
        # This iterator should handle limits, pagination
        # and messages deserialization.

        if messages:
            msgs = core.message_get_many(trans, req,
                                         self._name, messages)
        else:
            # NOTE(flaper87): It's safe to access messages
            # directly. If something wrong happens, the core
            # API will raise the right exceptions.
            msgs = core.message_list(trans, req,
                                     self._name,
                                     **params)

        return message._MessageIterator(self, msgs)