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)
|
headers: Mapping[str, str]
Response headers (case-insensitive, read-only; repeated headers joined by commas).
reason
property
Reason phrase of the status (e.g. "Not Found").
text
property
Response body decoded as text (charset from the headers, else guessed).
url
property
Final URL of the response (after redirects).
json
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
|
|
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)
|