Skip to content

HttpResponse

🛈 DocStrings

An HTTP response with its body already read.

A thin wrapper over the response of whichever httpx flavour is installed (httpx2 or httpx), so callers don't depend on the flavour.

Source code in src/anyenv/download/base.py
class HttpResponse:
    """An HTTP response with its body already read.

    A thin wrapper over the response of whichever httpx flavour is installed
    (httpx2 or httpx), so callers don't depend on the flavour.
    """

    __slots__ = ("_response",)

    def __init__(self, response: httpx.Response) -> None:
        self._response = response

    def __repr__(self) -> str:
        return f"<HttpResponse [{self.status_code} {self.reason}] {self.url}>"

    @property
    def status_code(self) -> int:
        """HTTP status code."""
        return self._response.status_code

    @property
    def reason(self) -> str:
        """Reason phrase of the status (e.g. "Not Found")."""
        return self._response.reason_phrase

    @property
    def url(self) -> str:
        """Final URL of the response (after redirects)."""
        return str(self._response.url)

    @property
    def headers(self) -> Mapping[str, str]:
        """Response headers (case-insensitive, read-only; repeated headers joined by commas)."""
        return ResponseHeaders(self._response.headers)

    @property
    def content(self) -> bytes:
        """Response body as bytes."""
        return self._response.content

    @property
    def text(self) -> str:
        """Response body decoded as text (charset from the headers, else guessed)."""
        return self._response.text

    def json(self) -> Any:
        """Response body parsed as JSON."""
        from anyenv import load_json

        return load_json(self._response.content)

    def validated_json[T](self, return_type: type[T]) -> T:
        """Response body parsed as JSON and validated against a type.

        Args:
            return_type: The type to validate the response against

        Raises:
            TypeError: If validation fails
        """
        from anyenv.validate import validate_json_data

        return validate_json_data(self.json(), return_type)

content property

content: bytes

Response body as bytes.

headers property

headers: Mapping[str, str]

Response headers (case-insensitive, read-only; repeated headers joined by commas).

reason property

reason: str

Reason phrase of the status (e.g. "Not Found").

status_code property

status_code: int

HTTP status code.

text property

text: str

Response body decoded as text (charset from the headers, else guessed).

url property

url: str

Final URL of the response (after redirects).

json

json() -> Any

Response body parsed as JSON.

Source code in src/anyenv/download/base.py
def json(self) -> Any:
    """Response body parsed as JSON."""
    from anyenv import load_json

    return load_json(self._response.content)

validated_json

validated_json(return_type: type[T]) -> T

Response body parsed as JSON and validated against a type.

Parameters:

Name Type Description Default
return_type type[T]

The type to validate the response against

required

Raises:

Type Description
TypeError

If validation fails

Source code in src/anyenv/download/base.py
def validated_json[T](self, return_type: type[T]) -> T:
    """Response body parsed as JSON and validated against a type.

    Args:
        return_type: The type to validate the response against

    Raises:
        TypeError: If validation fails
    """
    from anyenv.validate import validate_json_data

    return validate_json_data(self.json(), return_type)

:fa-brands-github: Show source on GitHub