A response as sent.
Attributes#
| Attribute | Type | Description |
|---|---|---|
status | = status | |
status_text | = status_text |
Functions#
__init__#
__init__(body=None, *, status=200, status_text='', headers=None) -> None| Parameter | Type | Description |
|---|---|---|
body | bytes | str | AsyncIterable[bytes] | None = None | |
status | int = 200 | |
status_text | str = '' | |
headers | HeaderLines | None = None |
def __init__(
self,
body: bytes | str | AsyncIterable[bytes] | None = None,
*,
status: int = 200,
status_text: str = "",
headers: HeaderLines | None = None,
) -> None:
lines = _lines(headers)
if isinstance(body, str) and not any(key == "content-type" for key, _ in lines):
lines = (*lines, ("content-type", "text/plain;charset=UTF-8"))
super().__init__(lines, body)
self.status = status
self.status_text = status_textjson_of#
json_of(value, *, status=200) -> WebResponseA JSON response (Web Response.json).
| Parameter | Type | Description |
|---|---|---|
value | object | |
status | int = 200 |
Returns WebResponse
@classmethod
def json_of(cls, value: object, *, status: int = 200) -> WebResponse:
"""A JSON response (Web ``Response.json``)."""
return cls(
text_encode(_json_text.stringify(value)),
status=status,
headers={"content-type": "application/json"},
)get_set_cookie#
get_set_cookie() -> list[str]Returns list[str]
def get_set_cookie(self) -> list[str]:
return [value for key, value in self.header_lines if key == "set-cookie"]get#
get(name) -> str | NoneHeader name, repeated values joined with ", ".
| Parameter | Type | Description |
|---|---|---|
name | str |
Returns str | None
def get(self, name: str) -> str | None:
"""Header ``name``, repeated values joined with ``", "``."""
name = name.lower()
values = [value for key, value in self.header_lines if key == name]
return ", ".join(values) if values else Nonehas#
has(name) -> bool| Parameter | Type | Description |
|---|---|---|
name | str |
Returns bool
def has(self, name: str) -> bool:
name = name.lower()
return any(key == name for key, _ in self.header_lines)read#
read() -> bytesThe whole body (it can be read once).
Returns bytes
async def read(self) -> bytes:
"""The whole body (it can be read once)."""
if self._used:
raise RuntimeError("body already read")
self._used = True
return await _read(self.body)buffer#
buffer() -> bytesRead the body into memory, so it can be read again (what Web
Response.clone allows).
Returns bytes
async def buffer(self) -> bytes:
"""Read the body into memory, so it can be read again (what Web
``Response.clone`` allows)."""
if self._used:
raise RuntimeError("body already read")
if not (self.body is None or isinstance(self.body, bytes)):
self.body = await _read(self.body)
return self.body or b""text#
text() -> strReturns str
async def text(self) -> str:
return (await self.read()).decode("utf-8", "replace")json#
json() -> AnyReturns Any
async def json(self) -> Any:
return _json_text.parse(await self.read())chunks#
chunks() -> AsyncIterator[bytes]The body, chunk by chunk.
Returns AsyncIterator[bytes]
def chunks(self) -> AsyncIterator[bytes]:
"""The body, chunk by chunk."""
self._used = True
body = self.body
if body is None or isinstance(body, bytes):
return _once(body)
# The body itself, so closing the iterator closes it.
return aiter(body)