Skip to content

wasat.response

Response class for Gemini protocol requests.

ReaderProtocol

Bases: Protocol

Protocol for async reader streams.

Response

Response(
    status: StatusCode,
    meta: str,
    reader: ReaderProtocol | None = None,
    uri: GeminiURI | None = None,
    history: list[Response] | None = None,
    requested_uri: GeminiURI | None = None,
    client_cert_path: Path | None = None,
)

Represents a response from a Gemini server.

Parameters:

Name Type Description Default

status

StatusCode

The Gemini status code.

required

meta

str

The extra metadata line.

required

reader

ReaderProtocol | None

The stream reader for reading the response body.

None

uri

GeminiURI | None

The Gemini URI of the response.

None

history

list[Response] | None

A history of response objects from any redirections.

None

requested_uri

GeminiURI | None

The originally requested Gemini URI.

None

client_cert_path

Path | None

The path to the client certificate used for the connection.

None

charset property

charset: str

The charset parameter from the MIME type, defaulting to 'utf-8'.

client_cert_path property

client_cert_path: Path | None

The path to the client certificate used for the connection, or None.

client_cert_used property

client_cert_used: bool

Whether a client certificate was used for the connection.

content_type property

content_type: str

The base content type (e.g., 'text/gemini' or 'text/plain').

history property

history: list[Response]

The history of response objects from any redirections, ordered from oldest to newest.

meta property

meta: str

The extra info/meta string from the response line.

For status 20, this is the MIME type. For other status codes, it contains error messages, instructions, or redirect URIs.

mime_type property

mime_type: str

The raw MIME type of the response.

Only relevant for 2x SUCCESS status codes. Defaults to 'text/gemini; charset=utf-8'.

requested_uri property

requested_uri: GeminiURI | None

The originally requested Gemini URI, or None if not set.

status property

status: StatusCode

The response status code.

uri property

uri: GeminiURI | None

The Gemini URI associated with the response, or None if not set.

__aenter__ async

__aenter__() -> Self

Enter the async context manager.

__aexit__ async

__aexit__(
    exception_type: type[BaseException] | None,
    exception_value: BaseException | None,
    exception_traceback: object,
) -> 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

The maximum size of each chunk.

4096

Yields:

Type Description
AsyncIterator[bytes]

Bytes chunks from the response body.

Raises:

Type Description
ConnectionError

If the server connection drops during reading.

read async

read() -> bytes

Read and return the entire response body.

Returns:

Type Description
bytes

The raw response body bytes.

Raises:

Type Description
ConnectionError

If the server connection drops during reading.

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 the response MIME type.

None

Returns:

Type Description
str

The decoded response body text.

Raises:

Type Description
ProtocolError

If the response body cannot be decoded using the specified encoding.