Client¶
asyncly.client.base.BaseHttpClient
¶
BaseHttpClient(
url: URL | str,
session: ClientSession,
client_name: str,
*,
proxy: URL | str | None = None,
proxy_auth: BasicAuth | None = None,
)
Typed base class for building async HTTP API clients.
Subclass it and add one method per endpoint, delegating to _make_req
with a mapping of status codes to response handlers. The
aiohttp.ClientSession is injected, so connection pooling and lifecycle
stay under your control.
Example
Initialize the client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
URL | str
|
Base URL the client's endpoints are resolved against. |
required |
session
|
ClientSession
|
The |
required |
client_name
|
str
|
Identifier used in metrics labels and error messages. |
required |
proxy
|
URL | str | None
|
Default proxy URL for every request. Can be overridden
per request by passing |
None
|
proxy_auth
|
BasicAuth | None
|
Default |
None
|
Source code in asyncly/client/base.py
asyncly.client.metrics.instrumentable_client.InstrumentableHttpClient
¶
InstrumentableHttpClient(
url: URL | str,
session: ClientSession,
client_name: str,
*,
proxy: URL | str | None = None,
proxy_auth: BasicAuth | None = None,
)
Bases: BaseHttpClient
BaseHttpClient that records request metrics through a pluggable sink.
Behaves exactly like BaseHttpClient until a sink
is enabled. Each completed request reports its client name, method, resolved
route, logical operation, status, outcome, duration, and error type to the
active MetricsSink.
Source code in asyncly/client/metrics/instrumentable_client.py
enable_metrics
¶
enable_metrics(
sink: MetricsSink,
*,
route_resolver: RouteResolver | None = None,
) -> None
Start emitting metrics to sink.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sink
|
MetricsSink
|
The metrics sink to report each request to. |
required |
route_resolver
|
RouteResolver | None
|
Optional override for how request URLs are normalized into low-cardinality route labels. |
None
|
Source code in asyncly/client/metrics/instrumentable_client.py
disable_metrics
¶
Stop emitting metrics (revert to the no-op sink).
Source code in asyncly/client/metrics/instrumentable_client.py
instrument
¶
instrument(
sink: MetricsSink,
*,
route_resolver: RouteResolver | None = None,
)
Context manager that enables sink for the duration of a block.
Restores the previous sink and route resolver on exit.
Source code in asyncly/client/metrics/instrumentable_client.py
Retries¶
asyncly.client.retry.RetryPolicy
dataclass
¶
RetryPolicy(
max_attempts: int = 3,
statuses: Collection[int] = (
lambda: DEFAULT_RETRY_STATUSES
)(),
exceptions: tuple[
type[BaseException], ...
] = DEFAULT_RETRY_EXCEPTIONS,
methods: Collection[str] = (
lambda: IDEMPOTENT_METHODS
)(),
backoff: BackoffStrategy = full_jitter_backoff,
respect_retry_after: bool = True,
)
Policy controlling whether and when an HTTP request is retried.
should_retry
¶
should_retry(context: RetryContext) -> bool
Return whether context is eligible for another attempt.
Source code in asyncly/client/retry.py
get_delay
¶
get_delay(
context: RetryContext,
*,
retry_after: str | None = None,
now: datetime | None = None,
) -> float
Return the delay before the next attempt.
A valid HTTP Retry-After value takes precedence when enabled;
otherwise the configured backoff strategy is used.
Source code in asyncly/client/retry.py
asyncly.client.retry.RetryContext
dataclass
¶
RetryContext(
method: str,
url: URL,
attempt: int,
max_attempts: int,
replayable: bool = True,
response_status: int | None = None,
exception: BaseException | None = None,
)
Immutable description of one physical request attempt.
attempt is one-based and describes the attempt that just produced
response_status or exception.
asyncly.client.retry.RetryEvent
dataclass
¶
RetryEvent(
kind: RetryEventKind,
context: RetryContext,
delay: float = 0.0,
reason: RetryReason = "status",
)
Notification emitted for a retry decision.
asyncly.client.retry.full_jitter_backoff
¶
full_jitter_backoff(context: RetryContext) -> float
Return capped exponential backoff with full jitter.
The cap starts at 0.5 seconds after the first failed attempt and doubles to
a maximum of 30 seconds. Pass a deterministic callable to RetryPolicy
in tests when real jitter is undesirable.
Source code in asyncly/client/retry.py
Timeouts¶
asyncly.client.timeout.get_timeout
¶
get_timeout(t: TimeoutType) -> ClientTimeout
asyncly.client.timeout.TimeoutType
module-attribute
¶
Typing¶
asyncly.client.typing.ResponseHandlersType
module-attribute
¶
asyncly.client.typing.MethodType
module-attribute
¶
MethodType = (
HTTPMethod
| Literal[
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
"HEAD",
"OPTIONS",
"TRACE",
"CONNECT",
]
)