示例#1
0
    def _format_request(rpc, to_topic, reply_topic, response_required,
                        **kwargs):
        """
        Format a request to send over kafka
        :param rpc: Requested remote API
        :param to_topic: Topic to send the request
        :param reply_topic: Topic to receive the resulting response, if any
        :param kwargs: Dictionary of key-value pairs to pass as arguments to
        the remote rpc API.
        :return: A InterContainerMessage message type on success or None on
        failure
        """
        try:
            transaction_id = uuid4().hex
            request = InterContainerMessage()
            request_body = InterContainerRequestBody()
            request.header.id = transaction_id
            request.header.type = MessageType.Value("REQUEST")
            request.header.from_topic = reply_topic
            request.header.to_topic = to_topic

            if reply_topic and response_required:
                request_body.reply_to_topic = reply_topic
                request_body.response_required = True
            else:
                request_body.response_required = False

            request.header.timestamp.GetCurrentTime()
            request_body.rpc = rpc
            for key, value in kwargs.items():
                arg = Argument()
                arg.key = key
                try:
                    arg.value.Pack(value)
                    request_body.args.extend([arg])

                except Exception as e:
                    log.exception("Failed-parsing-value", e=e, key=key)

            request.body.Pack(request_body)
            return request, transaction_id

        except Exception as e:
            log.exception("formatting-request-failed",
                          rpc=rpc,
                          to_topic=to_topic,
                          reply_topic=reply_topic,
                          response_required=response_required,
                          args=kwargs)
            return None, None
示例#2
0
 def _augment_args_with_from_topic(args, from_topic):
     arg = Argument(key=ARG_FROM_TOPIC)
     arg.value.Pack(StrType(val=from_topic))
     args.extend([arg])
     return args