Ejemplo n.º 1
0
    def _get_message(self, get_frame, auto_decode):
        """Get and return a message using a Basic.Get frame.

        :param Basic.Get get_frame:
        :param bool auto_decode: Auto-decode strings when possible.

        :rtype: Message
        """
        message_uuid = self._channel.rpc.register_request(
            get_frame.valid_responses + ['ContentHeader', 'ContentBody'])
        try:
            self._channel.write_frame(get_frame)
            get_ok_frame = self._channel.rpc.get_request(message_uuid,
                                                         raw=True,
                                                         multiple=True)
            if isinstance(get_ok_frame, specification.Basic.GetEmpty):
                return None
            content_header = self._channel.rpc.get_request(message_uuid,
                                                           raw=True,
                                                           multiple=True)
            body = self._get_content_body(message_uuid,
                                          content_header.body_size)
        finally:
            self._channel.rpc.remove(message_uuid)
        return Message(channel=self._channel,
                       body=body,
                       method=dict(get_ok_frame),
                       properties=dict(content_header.properties),
                       auto_decode=auto_decode)
Ejemplo n.º 2
0
    def _build_message(self):
        """Fetch and build a complete Message from the inbound queue.

        :rtype: Message
        """
        with self.lock:
            if len(self._inbound) < 3:
                return None
            basic_deliver = self._inbound.pop(0)
            if not isinstance(basic_deliver, pamqp_spec.Basic.Deliver):
                LOGGER.warning('Received an out-of-order frame: %s was '
                               'expecting a Basic.Deliver frame',
                               basic_deliver)
                return None
            content_header = self._inbound.pop(0)
            if not isinstance(content_header, ContentHeader):
                LOGGER.warning('Received an out-of-order frame: %s was '
                               'expecting a ContentHeader frame',
                               content_header)
                return None
            body = self._build_message_body(content_header.body_size)
        message = Message(channel=self,
                          body=body,
                          method=dict(basic_deliver),
                          properties=dict(content_header.properties))
        return message
Ejemplo n.º 3
0
    def _get_message(self, get_frame):
        """Get and return a message using a Basic.Get frame.

        :param Basic.Get get_frame:

        :rtype: Message
        """
        uuid_get = \
            self._channel.rpc.register_request(get_frame.valid_responses)
        uuid_header = self._channel.rpc.register_request(['ContentHeader'])
        uuid_body = self._channel.rpc.register_request(['ContentBody'])
        self._channel.write_frame(get_frame)
        get_frame = self._channel.rpc.get_request(uuid_get, raw=True)

        if not isinstance(get_frame, pamqp_spec.Basic.GetOk):
            self._channel.rpc.remove(uuid_header)
            self._channel.rpc.remove(uuid_body)
            return None
        content_header = self._channel.rpc.get_request(uuid_header, raw=True)
        body = self._get_content_body(uuid_body, content_header.body_size)

        return Message(channel=self._channel,
                       body=body,
                       method=dict(get_frame),
                       properties=dict(content_header.properties))
Ejemplo n.º 4
0
    def get(self,
            queue,
            virtual_host='/',
            requeue=False,
            to_dict=False,
            count=1,
            truncate=50000,
            encoding='auto'):
        """Get Messages.

        :param str queue: Queue name
        :param str virtual_host: Virtual host name
        :param bool requeue: Re-queue message
        :param bool to_dict: Should incoming messages be converted to a
                             dictionary before delivery.
        :param int count: How many messages should we try to fetch.
        :param int truncate: The maximum length in bytes, beyond that the
                             server will truncate the message.
        :param str encoding: Message encoding.

        :raises ApiError: Raises if the remote server encountered an error.
        :raises ApiConnectionError: Raises if there was a connectivity issue.

        :rtype: list
        """
        ackmode = 'ack_requeue_false'
        if requeue:
            ackmode = 'ack_requeue_true'

        get_messages = json.dumps({
            'count': count,
            'requeue': requeue,
            'ackmode': ackmode,
            'encoding': encoding,
            'truncate': truncate,
            'vhost': virtual_host
        })
        virtual_host = quote(virtual_host, '')
        response = self.http_client.post(API_BASIC_GET_MESSAGE %
                                         (virtual_host, queue),
                                         payload=get_messages)
        if to_dict:
            return response
        messages = []
        for message in response:
            if 'payload' in message:
                message['body'] = message.pop('payload')
            messages.append(Message(channel=None, auto_decode=True, **message))
        return messages
Ejemplo n.º 5
0
    def _build_message(self):
        """Fetch and build a complete Message from the inbound queue.

        :rtype: Message
        """
        # print("_build_message call")
        with self.lock:
            if len(self._inbound) < 2:
                return None
            headers = self._build_message_headers()
            if not headers:
                return None
            basic_deliver, content_header = headers
            body = self._build_message_body(content_header.body_size)

        message = Message(channel=self,
                          body=body,
                          method=dict(basic_deliver),
                          properties=dict(content_header.properties))
        return message
Ejemplo n.º 6
0
    def _build_message(self, auto_decode):
        """Fetch and build a complete Message from the inbound queue.

        :param bool auto_decode: Auto-decode strings when possible.

        :rtype: Message
        """
        with self.lock:
            if len(self._inbound) < 2:
                return None
            headers = self._build_message_headers()
            if not headers:
                return None
            basic_deliver, content_header = headers
            body = self._build_message_body(content_header.body_size)

        message = Message(channel=self,
                          body=body,
                          method=dict(basic_deliver),
                          properties=dict(content_header.properties),
                          auto_decode=auto_decode)
        return message