The answer to a query or exec, its body not yet read (read it
once): text, json or the rows of stream.
Attributes#
| Attribute | Type | Description |
|---|---|---|
response | = response | |
query_id | = query_id | |
format | = format | |
response_headers | Headers |
Functions#
__init__#
__init__(response, query_id, format, parse=_json_text.parse) -> None| Parameter | Type | Description |
|---|---|---|
response | HttpClientResponse | |
query_id | str | |
format | str | None | |
parse | Callable[[str], object] = _json_text.parse |
def __init__(
self,
response: HttpClientResponse,
query_id: str,
format: str | None,
parse: Callable[[str], object] = _json_text.parse,
) -> None:
self.response = response
self.query_id = query_id
self.format = format
self._parse = parsetext#
text() -> Effect[str, ClickhouseRequestError]The whole body; the server's error if it ended with one (also when the server then cut the body short).
Returns Effect[str, ClickhouseRequestError]
def text(self) -> Effect[str, ClickhouseRequestError]:
"""The whole body; the server's error if it ended with one (also
when the server then cut the body short)."""
received = bytearray()
tag = self._tag
def add(chunk: bytes) -> Effect[None]:
received.extend(chunk)
return succeed(None)
def ended(cut: HttpClientError | None) -> Effect[str, ClickhouseRequestError]:
error = body_error(bytes(received), tag) or cut
return succeed(received.decode()) if error is None else fail(error)
read = self.response.stream.run_for_each(add)
return read.match_effect(on_failure=ended, on_success=lambda _: ended(None))json#
json() -> Effect[Any, ClickhouseRequestError | ValueError]The body as JSON: the document of a single-document format, the
list of rows of a format with a value per line. A body that is not
JSON fails with the parser's ValueError (the config's json
parser reads it).
Returns Effect[Any, ClickhouseRequestError | ValueError]
def json(self) -> Effect[Any, ClickhouseRequestError | ValueError]:
"""The body as JSON: the document of a single-document format, the
list of rows of a format with a value per line. A body that is not
JSON fails with the parser's ``ValueError`` (the config's ``json``
parser reads it)."""
if self.format in STREAMABLE_JSON_FORMATS:
return self.stream().map(self._parse).run_collect().map(list)
def parse(text: str) -> Effect[Any, ValueError]:
try:
return succeed(self._parse(text))
except ValueError as error:
return fail(error)
return self.text().flat_map(parse)stream#
stream() -> Stream[str, ClickhouseRequestError]The body's rows, a line each, as they arrive; the server's error if one ends the body.
Returns Stream[str, ClickhouseRequestError]
def stream(self) -> Stream[str, ClickhouseRequestError]:
"""The body's rows, a line each, as they arrive; the server's error
if one ends the body."""
from pyfect import stream as stream_module
def rows() -> Stream[str, ClickhouseRequestError]:
splitter = RowSplitter(self._tag)
def finish() -> Stream[str, ClickhouseRequestError]:
try:
return stream_module.from_iterable(splitter.finish())
except ClickhouseError as error:
return stream_module.fail(error)
def cut(error: HttpClientError) -> Stream[str, ClickhouseRequestError]:
return stream_module.fail(splitter.failure() or error)
body: Stream[str, ClickhouseRequestError] = (
self.response.stream.map(splitter.feed).flatten_iterable().catch_all(cut)
)
return body.concat(stream_module.suspend(finish))
return stream_module.suspend(rows)