Skip to content

sybaritic.response

Response class for Spartan protocol requests.

ReaderProtocol

Bases: Protocol

Protocol for async reader streams.

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

Status

The Spartan status code.

required

meta

str

The extra metadata line.

required

reader

ReaderProtocol | None

The stream reader for reading the response body.

None

uri

SpartanURI | None

The Spartan URI of the response.

None

history

list[Response] | None

A history of response objects from any redirections.

None

requested_uri

SpartanURI | None

The originally requested Spartan URI.

None

content

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

history: list[Response]

The history of response objects leading to this response via redirections.

is_client_error property

is_client_error: bool

Return True if response is status 4 (Client Error).

is_error property

is_error: bool

Return True if response is status 4 or 5.

is_redirect property

is_redirect: bool

Return True if response is status 3 (Redirect).

is_redirected property

is_redirected: bool

Return True if this response is the result of following redirects.

is_server_error property

is_server_error: bool

Return True if response is status 5 (Server Error).

is_success property

is_success: bool

Return True if response is status 2 (Success).

meta property

meta: str

The extra info/meta string from the response header line.

mime_type property

mime_type: str

Return the raw MIME type for a successful response, defaulting to 'text/gemini; charset=utf-8'.

reader property

reader: ReaderProtocol | None

The stream reader for the response body.

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.

status property

status: Status

The response status code.

uri property writable

uri: SpartanURI | None

The SpartanURI of the response, or None if not set.

__aenter__ async

__aenter__() -> Self

Enter the async context manager.

__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

chunk_size

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

text(encoding: str | None = None) -> str

Read and return the entire response body as a decoded string.

Parameters:

Name Type Description Default

encoding

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.