예제 #1
0
파일: aio.py 프로젝트: public/sonora
    def __await__(self):
        response = yield from self._get_response().__await__()

        data = yield from response.read().__await__()

        response.release()

        if not data:
            return

        buffer = io.BytesIO(data)

        messages = protocol.unwrap_message_stream(buffer)

        trailers, _, message = next(messages)

        if trailers:
            self._trailers = protocol.unpack_trailers(message)
            return
        else:
            result = self._deserializer(message)

        try:
            trailers, _, message = next(messages)
        except StopIteration:
            pass
        else:
            if trailers:
                self._trailers = protocol.unpack_trailers(message)
            else:
                raise ValueError("UnaryUnary should only return a single message")

        protocol.raise_for_status(response.headers)

        return result
예제 #2
0
def test_unwrapping_stream():
    buffer = io.BytesIO()

    messages = [
        b"Tyger Tyger, burning bright,",
        b"In the forests of the night;",
        b"What immortal hand or eye,",
        b"Could frame thy fearful symmetry?",
    ]
    for message in messages:
        buffer.write(protocol.wrap_message(False, False, message))

    buffer.seek(0)

    resp_messages = []
    for _, _, resp in protocol.unwrap_message_stream(buffer):
        resp_messages.append(resp)

    assert resp_messages == messages
예제 #3
0
    def __call__(self):
        self._response = self._session.request(
            "POST",
            self._url,
            body=protocol.wrap_message(False, False,
                                       self._serializer(self._request)),
            headers=dict(self._metadata),
            timeout=self._timeout,
        )

        buffer = io.BytesIO(self._response.data)

        messages = protocol.unwrap_message_stream(buffer)

        try:
            trailers, _, message = next(messages)
        except StopIteration:
            protocol.raise_for_status(self._response.headers)
            return

        if trailers:
            self._trailers = protocol.unpack_trailers(message)
        else:
            result = self._deserializer(message)

        try:
            trailers, _, message = next(messages)
        except StopIteration:
            pass
        else:
            if trailers:
                self._trailers = protocol.unpack_trailers(message)
            else:
                raise ValueError(
                    "UnaryUnary should only return a single message")

        protocol.raise_for_status(self._response.headers, self._trailers)

        return result
예제 #4
0
    def __iter__(self):
        self._response = self._session.request(
            "POST",
            self._url,
            body=protocol.wrap_message(False, False,
                                       self._serializer(self._request)),
            headers=dict(self._metadata),
            timeout=self._timeout,
            preload_content=False,
        )
        self._response.auto_close = False

        stream = io.BufferedReader(self._response, buffer_size=16384)

        for trailers, _, message in protocol.unwrap_message_stream(stream):
            if trailers:
                self._trailers = protocol.unpack_trailers(message)
                break
            else:
                yield self._deserializer(message)

        self._response.release_conn()

        protocol.raise_for_status(self._response.headers, self._trailers)