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,
)

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

charset property

charset: str

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

content_type property

content_type: str

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

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'.

status property

status: StatusCode

The response status code.

__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.