sybaritic.response
Response class for Spartan protocol requests.
ReaderProtocol
Response
Response(
status: Status,
meta: str,
reader: ReaderProtocol | None = None,
uri: SpartanURI | None = None,
history: list[Response] | None = None,
requested_uri: SpartanURI | None = None,
content: bytes | None = None,
)
Represents a response from a Spartan server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Status
|
The Spartan status code. |
required |
|
str
|
The extra metadata line. |
required |
|
ReaderProtocol | None
|
The stream reader for reading the response body. |
None
|
|
SpartanURI | None
|
The Spartan URI of the response. |
None
|
|
list[Response] | None
|
A history of response objects from any redirections. |
None
|
|
SpartanURI | None
|
The originally requested Spartan URI. |
None
|
|
bytes | None
|
Pre-loaded raw response body bytes, if available. |
None
|
charset
property
charset: str
Return the character encoding extracted from MIME type parameters, defaulting to 'utf-8'.
content
property
content: bytes
Return cached response body bytes if already read, otherwise empty bytes.
content_type
property
content_type: str
Return the base content type (e.g. 'text/gemini' or 'text/plain').
error_message
property
error_message: str | None
Return the error message if status is 4 or 5, otherwise None.
history
property
writable
The history of response objects leading to this response via redirections.
is_redirected
property
is_redirected: bool
Return True if this response is the result of following redirects.
mime_type
property
mime_type: str
Return the raw MIME type for a successful response, defaulting to 'text/gemini; charset=utf-8'.
redirect_path
property
redirect_path: str | None
Return the target redirect path if status is 3, otherwise None.
requested_uri
property
writable
requested_uri: SpartanURI | None
The originally requested SpartanURI, or None if not set.
__aexit__
async
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Exit the async context manager and close the connection.
close
async
close() -> None
Close the underlying connection if it is still open.
iter_chunks
async
iter_chunks(chunk_size: int = 4096) -> AsyncIterator[bytes]
Iterate over the response body in chunks as they arrive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
Maximum size of each chunk. |
4096
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[bytes]
|
Bytes chunks from the response body. |
Raises:
| Type | Description |
|---|---|
ResponseError
|
If reading a response body chunk fails. |
read
async
read() -> bytes
Read and return the entire response body.
Returns:
| Type | Description |
|---|---|
bytes
|
The raw response body bytes. |
Raises:
| Type | Description |
|---|---|
ResponseError
|
If reading the response body fails. |
text
async
Read and return the entire response body as a decoded string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | None
|
The text encoding to use. If None, uses the charset from response MIME type. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The decoded response body text. |
Raises:
| Type | Description |
|---|---|
ResponseError
|
If the response body cannot be decoded using the specified encoding. |