def generate_messages():
            headers = {}

            messages = [
                message.Message(payload=b"hello", headers=headers),
                message.Message(payload=b"world", headers=headers),
                message.Message(payload=b"foo", headers=headers),
                message.Message(payload=b"bar", headers=headers),
            ]
            for msg in messages:
                yield msg
Exemplo n.º 2
0
def build_reply_message(payload, headers):
    """
    Convert the reply payload to bytes given the request's Accept header
    :param headers: The request header values
    :param val: the payload
    :return: bytes
    """

    reply = message.Message()
    if headers.get('correlationId', None):
        reply.headers['correlationId'].values[:] = headers['correlationId'].values[:]

    accepts = headers.get('Accepts',message.Message.HeaderValue()).values

    if len(accepts) == 0 or 'text/plain' in accepts or "*/*" in accepts:
        if type(payload) is dict:
            reply.payload = bytes(json.dumps(payload), 'UTF-8')
        else:
            if type(payload) is str:
                reply.payload = bytes(payload, 'UTF-8')
                reply.headers['Content-type'].values[:] = ["text/plain"]
            else:
                reply.payload = payload
                reply.headers['Content-type'].values[:] = ["application/octet-stream"]

    elif 'application/json' in accepts:
        if type(payload) is dict:
            reply.payload = bytes(json.dumps(payload), 'UTF-8')
            reply.headers['Content-type'].values[:] = ["application/json"]
        else:
            raise RuntimeError('Cannot convert type %s to JSON' % type(payload))
    else:
        raise RuntimeError('Unsupported Accept header %s' % accepts)

    return reply
        def generate_messages():
            headers = {
                'Content-Type':
                message.Message.HeaderValue(values=['text/plain']),
                'correlationId':
                message.Message.HeaderValue(values=[str(uuid.uuid4())])
            }

            messages = [
                message.Message(payload=bytes("hello", 'UTF-8'),
                                headers=headers),
                message.Message(payload=bytes("world", 'UTF-8'),
                                headers=headers),
                message.Message(payload=bytes("foo", 'UTF-8'),
                                headers=headers),
                message.Message(payload=bytes("bar", 'UTF-8'),
                                headers=headers),
            ]
            for msg in messages:
                yield msg
Exemplo n.º 4
0
    def Call(self, request_iterator, context):
        for request in request_iterator:
            result = self.func(self.convertRequestPayload(request))

            reply = message.Message()
            reply.payload = self.convertReplyPayload(
                request.headers['Accept'].values, result)

            reply.headers['correlationId'].values[:] = request.headers[
                'correlationId'].values[:]
            yield reply
        def generate_messages():
            headers = {
                'Content-Type':
                message.Message.HeaderValue(values=['application/json']),
                'correlationId':
                message.Message.HeaderValue(values=[str(uuid.uuid4())])
            }

            messages = [
                message.Message(payload=b'{"foo":"bar","hello":"world"}',
                                headers=headers),
            ]
            for msg in messages:
                yield msg
Exemplo n.º 6
0
 def Call(self, request_iterator, context):
     for request in request_iterator:
         reply = message.Message()
         reply.payload = request.payload.upper()
         reply.headers['correlationId'].values[:] = request.headers['correlationId'].values[:]
         yield reply