functional
Class info¶
🛈 DocStrings¶
High-level interface for HTTP requests and downloads.
Inside an async with anyenv.http_session(...) block the async helpers use the
session's client (connection reuse); otherwise each call opens its own client.
The *_sync helpers always use a client of their own.
download
async
¶
download(
url: str,
path: StrPath,
*,
headers: HeaderType | None = None,
progress_callback: ProgressCallback | None = None,
timeout: float | None = _UNSET,
cache: bool | None = None,
cache_dir: StrPath | None = None,
cache_ttl: int | str | None = None,
) -> None
Download a file, written to <path>.part and renamed once complete.
In httpx the read timeout applies per read (not to the whole download), so large downloads keep working as long as data keeps arriving.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to download |
required |
path
|
StrPath
|
Path where to save the file (an existing file stays if it fails) |
required |
headers
|
HeaderType | None
|
Request headers |
None
|
progress_callback
|
ProgressCallback | None
|
Called (sync or async) with (bytes so far, total) |
None
|
timeout
|
float | None
|
Timeout in seconds. When omitted, uses the open session's timeout,
or |
_UNSET
|
cache
|
bool | None
|
Cache the response (default: as the open session does, else no) |
None
|
cache_dir
|
StrPath | None
|
Directory of the cache database |
None
|
cache_ttl
|
int | str | None
|
How long cached responses are used: seconds or e.g. "1h", "2d" |
None
|
Source code in src/anyenv/download/functional.py
download_sync
¶
download_sync(
url: str,
path: StrPath,
*,
headers: HeaderType | None = None,
progress_callback: ProgressCallback | None = None,
timeout: float | None = _UNSET,
cache: bool | None = None,
cache_dir: StrPath | None = None,
cache_ttl: int | str | None = None,
) -> None
Synchronous version of download.
Source code in src/anyenv/download/functional.py
get
async
¶
get(
url: str,
*,
params: ParamsType | None = None,
headers: HeaderType | None = None,
timeout: float | None = _UNSET,
cache: bool | None = None,
cache_dir: StrPath | None = None,
cache_ttl: int | str | None = None,
api_key: str | SecretStr | None = None,
auth_type: AuthType = "bearer",
auth_username: str | None = None,
auth_header_name: str | None = None,
) -> HttpResponse
Make a GET request (options as for request).
Source code in src/anyenv/download/functional.py
get_bytes
async
¶
get_bytes(url: str, **kwargs: Unpack[DataRetrievalOptions]) -> bytes
GET a URL and return the body as bytes (options as for request).
get_bytes_sync
¶
get_bytes_sync(url: str, **kwargs: Unpack[DataRetrievalOptions]) -> bytes
Synchronous version of get_bytes.
get_json
async
¶
get_json(
url: str, *, return_type: type[T] | None = None, **kwargs: Unpack[DataRetrievalOptions]
) -> T
GET a URL and return the body parsed as JSON (options as for request).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to request |
required |
return_type
|
type[T] | None
|
Type to validate the data against (pydantic models, dataclasses, TypedDicts, ...); without it the data is returned as parsed |
None
|
kwargs
|
Unpack[DataRetrievalOptions]
|
Options as for |
{}
|
Source code in src/anyenv/download/functional.py
get_json_sync
¶
get_json_sync(
url: str, *, return_type: type[T] | None = None, **kwargs: Unpack[DataRetrievalOptions]
) -> T
Synchronous version of get_json.
Source code in src/anyenv/download/functional.py
get_sync
¶
get_sync(url: str, **kwargs: Unpack[DataRetrievalOptions]) -> HttpResponse
get_text
async
¶
get_text(url: str, **kwargs: Unpack[DataRetrievalOptions]) -> str
GET a URL and return the body as text (options as for request).
get_text_sync
¶
get_text_sync(url: str, **kwargs: Unpack[DataRetrievalOptions]) -> str
Synchronous version of get_text.
post
async
¶
post(
url: str,
*,
params: ParamsType | None = None,
headers: HeaderType | None = None,
json: Any = None,
data: Any = None,
files: FilesType | None = None,
timeout: float | None = _UNSET,
cache: bool | None = None,
cache_dir: StrPath | None = None,
cache_ttl: int | str | None = None,
api_key: str | SecretStr | None = None,
auth_type: AuthType = "bearer",
auth_username: str | None = None,
auth_header_name: str | None = None,
) -> HttpResponse
Make a POST request (options as for request).
Source code in src/anyenv/download/functional.py
post_json
async
¶
post_json(
url: str,
json_data: Any,
*,
files: FilesType | None = None,
return_type: type[T] | None = None,
**kwargs: Unpack[DataRetrievalOptions],
) -> T
POST JSON and return the response parsed as JSON (options as for request).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to request |
required |
json_data
|
Any
|
Data sent as JSON body (as form fields when |
required |
files
|
FilesType | None
|
Files to upload (multipart) |
None
|
return_type
|
type[T] | None
|
Type to validate the response against |
None
|
kwargs
|
Unpack[DataRetrievalOptions]
|
Options as for |
{}
|
Source code in src/anyenv/download/functional.py
post_json_sync
¶
post_json_sync(
url: str,
json_data: Any,
*,
files: FilesType | None = None,
return_type: type[T] | None = None,
**kwargs: Unpack[DataRetrievalOptions],
) -> T
Synchronous version of post_json.
Source code in src/anyenv/download/functional.py
post_sync
¶
post_sync(
url: str,
*,
json: Any = None,
data: Any = None,
files: FilesType | None = None,
**kwargs: Unpack[DataRetrievalOptions],
) -> HttpResponse
Synchronous version of post.
Source code in src/anyenv/download/functional.py
request
async
¶
request(
method: Method,
url: str,
*,
params: ParamsType | None = None,
headers: HeaderType | None = None,
json: Any = None,
data: Any = None,
files: FilesType | None = None,
timeout: float | None = _UNSET,
cache: bool | None = None,
cache_dir: StrPath | None = None,
cache_ttl: int | str | None = None,
api_key: str | SecretStr | None = None,
auth_type: AuthType = "bearer",
auth_username: str | None = None,
auth_header_name: str | None = None,
) -> HttpResponse
Make an HTTP request; the response's body is read already.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Method
|
HTTP method to use |
required |
url
|
str
|
URL to request (relative URLs resolve against the session's base_url) |
required |
params
|
ParamsType | None
|
Query parameters |
None
|
headers
|
HeaderType | None
|
Request headers |
None
|
json
|
Any
|
JSON body |
None
|
data
|
Any
|
Form data or raw body |
None
|
files
|
FilesType | None
|
Files to upload (multipart) |
None
|
timeout
|
float | None
|
Timeout in seconds. When omitted, uses the open session's timeout,
or |
_UNSET
|
cache
|
bool | None
|
Cache responses (default: as the open session does, else no) |
None
|
cache_dir
|
StrPath | None
|
Directory of the cache database |
None
|
cache_ttl
|
int | str | None
|
How long cached responses are used: seconds or e.g. "1h", "2d" |
None
|
api_key
|
str | SecretStr | None
|
API key or token for authentication |
None
|
auth_type
|
AuthType
|
How the api_key is sent ("bearer", "basic", "header", "query") |
'bearer'
|
auth_username
|
str | None
|
Username for basic authentication |
None
|
auth_header_name
|
str | None
|
Header name for auth_type="header" (default: X-API-Key) |
None
|
Raises:
| Type | Description |
|---|---|
RequestError
|
The request failed (connection, timeout) |
ResponseError
|
The response has a 4xx / 5xx status |
Source code in src/anyenv/download/functional.py
request_sync
¶
request_sync(
method: Method,
url: str,
*,
json: Any = None,
data: Any = None,
files: FilesType | None = None,
**kwargs: Unpack[DataRetrievalOptions],
) -> HttpResponse
Synchronous version of request.