Skip to content

wasat.response

Response class for Gemini protocol requests.

VerificationMethod

VerificationMethod = Literal['ca', 'tofu', 'off']

Type alias for server certificate verification method.

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,
    server_cert_der: bytes | None = None,
    verification_method: VerificationMethod | 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

server_cert_der

bytes | None

The raw DER-encoded server TLS certificate, or None.

None

verification_method

VerificationMethod | None

The method used to verify the server TLS certificate, or None.

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.

server_cert property

server_cert: ServerCertificate | None

The parsed server TLS certificate information, or None if unavailable.

server_cert_der property

server_cert_der: bytes | None

The raw DER-encoded server TLS certificate, or None if unavailable.

server_cert_fingerprint property

server_cert_fingerprint: str | None

The SHA-256 fingerprint of the server certificate, or None if unavailable.

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.

verification_method property

verification_method: VerificationMethod | None

The method used to verify the server TLS certificate, or None.

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