Reference API
The following classes are exposed to the user. The HTTP-specific classes live in the
sub-package of the HTTP client that you use, httpx_limiter.httpx or
httpx_limiter.httpx2, and each requires the matching extra.
RateNumberAbstractAsyncLimiterAsyncRateLimitedTransportAbstractRateLimiterRepositoryAsyncMultiRateLimitedTransportHTTPXAsyncHTTPTransportKeywordArgumentsAsyncRateLimitedTransport(HTTPX2)AbstractRateLimiterRepository(HTTPX2)AsyncMultiRateLimitedTransport(HTTPX2)HTTPXAsyncHTTPTransportKeywordArguments(HTTPX2)AiolimiterAsyncLimiterPyrateAsyncLimiter
Core¶
httpx_limiter
¶
Provide top level symbols.
This package does not import any HTTP client. Select an integration explicitly via
httpx_limiter.httpx or httpx_limiter.httpx2.
Number = float | int
module-attribute
¶
A real number, such as a duration in seconds passed to Rate.create.
Rate
¶
Bases: NamedTuple
Define the rate.
duration: timedelta
instance-attribute
¶
magnitude: int
instance-attribute
¶
create(magnitude: int = 1, duration: timedelta | Number = 1) -> Rate
classmethod
¶
Create a rate.
in_milliseconds() -> int
¶
Return the duration in unit milliseconds.
in_seconds() -> float
¶
Return the duration in unit seconds.
AbstractAsyncLimiter
¶
Bases: ABC
Define an abstract base class for asynchronous rate limiters.
This interface dictates that all asynchronous rate limiters shall implement the context manager protocol.
HTTPX¶
httpx_limiter.httpx
¶
Provide the HTTPX integration.
Importing this package requires the httpx extra, for example
pip install "httpx-limiter[httpx]".
AsyncRateLimitedTransport
¶
Bases: AsyncBaseTransport
Define the asynchronous rate-limited transport.
This transport consists of a composed transport for handling requests and an implementation of a leaky bucket algorithm in order to rate-limit the number of requests.
__aenter__() -> Self
async
¶
Enter the transport and its underlying transport.
Entering this wrapper, for example through the client's context, also enters the composed transport, so that it can set up its resources.
__aexit__(exc_type: type[BaseException] | None = None, exc_value: BaseException | None = None, traceback: TracebackType | None = None) -> None
async
¶
Exit the transport and its underlying transport.
Exiting this wrapper, for example through the client's context, exits the composed transport with the same exception details, which lets it clean up its resources.
__init__(*, limiter: AbstractAsyncLimiter, transport: httpx.AsyncBaseTransport, **kwargs: dict[str, object]) -> None
¶
aclose() -> None
async
¶
Close the transport and its underlying transport.
Closing this wrapper also closes the composed transport, which is why sharing an inner transport between wrappers shares its lifetime.
create(limiter: AbstractAsyncLimiter, **kwargs: Unpack[HTTPXAsyncHTTPTransportKeywordArguments]) -> AsyncRateLimitedTransport
classmethod
¶
Create an instance of asynchronous rate-limited transport.
This factory method constructs the instance with an underlying
httpx.AsyncHTTPTransport.
That transport is passed any additional keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limiter
|
AbstractAsyncLimiter
|
A concrete instance of an asynchronous limiter. |
required |
**kwargs
|
Unpack[HTTPXAsyncHTTPTransportKeywordArguments]
|
Additional keyword arguments are used in the construction of an
|
{}
|
Returns:
| Type | Description |
|---|---|
AsyncRateLimitedTransport
|
A default instance of the class created from the given arguments. |
handle_async_request(request: httpx.Request) -> httpx.Response
async
¶
Handle an asynchronous request with rate limiting.
AbstractRateLimiterRepository
¶
Bases: RateLimiterRepository[Request]
Define the abstract repository for rate limiters of HTTPX requests.
This abstract base class provides a framework for managing rate limiters based on HTTPX requests. It maintains a cache of rate limiters and provides methods to retrieve request-specific identifiers and limiters.
Subclasses must implement methods to determine how requests are identified and what rate limits should be applied to them.
Methods:
| Name | Description |
|---|---|
get_identifier |
Return a request-specific identifier. |
create |
Return a request-specific rate limiter. |
get |
Return a request-specific rate limiter. |
AsyncMultiRateLimitedTransport
¶
Bases: AsyncBaseTransport
Define the asynchronous multiple rate-limited transport.
This transport consists of a composed transport for handling requests and a repository for rate limiters that are selected based on the request.
__aenter__() -> Self
async
¶
Enter the transport and its underlying transport.
Entering this wrapper, for example through the client's context, also enters the composed transport, so that it can set up its resources.
__aexit__(exc_type: type[BaseException] | None = None, exc_value: BaseException | None = None, traceback: TracebackType | None = None) -> None
async
¶
Exit the transport and its underlying transport.
Exiting this wrapper, for example through the client's context, exits the composed transport with the same exception details, which lets it clean up its resources.
__init__(*, repository: AbstractRateLimiterRepository, transport: httpx.AsyncBaseTransport, **kwargs: dict[str, object]) -> None
¶
aclose() -> None
async
¶
Close the transport and its underlying transport.
Closing this wrapper also closes the composed transport, which is why sharing an inner transport between wrappers shares its lifetime.
create(*, repository: AbstractRateLimiterRepository, **kwargs: Unpack[HTTPXAsyncHTTPTransportKeywordArguments]) -> AsyncMultiRateLimitedTransport
classmethod
¶
Create an instance of an asynchronous multiple rate-limited transport.
This factory method constructs the instance with an underlying
httpx.AsyncHTTPTransport.
That transport is passed any additional keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repository
|
AbstractRateLimiterRepository
|
The repository to use for rate limiters. |
required |
**kwargs
|
Unpack[HTTPXAsyncHTTPTransportKeywordArguments]
|
Additional keyword arguments are used in the construction of an
|
{}
|
Returns:
| Type | Description |
|---|---|
AsyncMultiRateLimitedTransport
|
A default instance of the class created from the given arguments. |
handle_async_request(request: httpx.Request) -> httpx.Response
async
¶
Handle an asynchronous request with rate limiting.
HTTPXAsyncHTTPTransportKeywordArguments
¶
Bases: TypedDict
Keyword arguments for the httpx.AsyncHTTPTransport constructor.
cert: CertTypes | None
instance-attribute
¶
http1: bool
instance-attribute
¶
http2: bool
instance-attribute
¶
limits: httpx.Limits
instance-attribute
¶
local_address: str | None
instance-attribute
¶
proxy: ProxyTypes | None
instance-attribute
¶
retries: int
instance-attribute
¶
socket_options: Iterable[SocketOption] | None
instance-attribute
¶
trust_env: bool
instance-attribute
¶
uds: str | None
instance-attribute
¶
verify: ssl.SSLContext | str | bool
instance-attribute
¶
HTTPX2¶
httpx_limiter.httpx2
¶
Provide the HTTPX2 integration.
Importing this package requires the httpx2 extra, for example
pip install "httpx-limiter[httpx2]".
AsyncRateLimitedTransport
¶
Bases: AsyncBaseTransport
Define the asynchronous rate-limited transport.
This transport consists of a composed transport for handling requests and an implementation of a leaky bucket algorithm in order to rate-limit the number of requests.
__aenter__() -> Self
async
¶
Enter the transport and its underlying transport.
Entering this wrapper, for example through the client's context, also enters the composed transport, so that it can set up its resources.
__aexit__(exc_type: type[BaseException] | None = None, exc_value: BaseException | None = None, traceback: TracebackType | None = None) -> None
async
¶
Exit the transport and its underlying transport.
Exiting this wrapper, for example through the client's context, exits the composed transport with the same exception details, which lets it clean up its resources.
__init__(*, limiter: AbstractAsyncLimiter, transport: httpx2.AsyncBaseTransport, **kwargs: dict[str, object]) -> None
¶
aclose() -> None
async
¶
Close the transport and its underlying transport.
Closing this wrapper also closes the composed transport, which is why sharing an inner transport between wrappers shares its lifetime.
create(limiter: AbstractAsyncLimiter, **kwargs: Unpack[HTTPXAsyncHTTPTransportKeywordArguments]) -> AsyncRateLimitedTransport
classmethod
¶
Create an instance of asynchronous rate-limited transport.
This factory method constructs the instance with an underlying
httpx2.AsyncHTTPTransport.
That transport is passed any additional keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limiter
|
AbstractAsyncLimiter
|
A concrete instance of an asynchronous limiter. |
required |
**kwargs
|
Unpack[HTTPXAsyncHTTPTransportKeywordArguments]
|
Additional keyword arguments are used in the construction of an
|
{}
|
Returns:
| Type | Description |
|---|---|
AsyncRateLimitedTransport
|
A default instance of the class created from the given arguments. |
handle_async_request(request: httpx2.Request) -> httpx2.Response
async
¶
Handle an asynchronous request with rate limiting.
AbstractRateLimiterRepository
¶
Bases: RateLimiterRepository[Request]
Define the abstract repository for rate limiters of HTTPX2 requests.
This abstract base class provides a framework for managing rate limiters based on HTTPX2 requests. It maintains a cache of rate limiters and provides methods to retrieve request-specific identifiers and limiters.
Subclasses must implement methods to determine how requests are identified and what rate limits should be applied to them.
Methods:
| Name | Description |
|---|---|
get_identifier |
Return a request-specific identifier. |
create |
Return a request-specific rate limiter. |
get |
Return a request-specific rate limiter. |
AsyncMultiRateLimitedTransport
¶
Bases: AsyncBaseTransport
Define the asynchronous multiple rate-limited transport.
This transport consists of a composed transport for handling requests and a repository for rate limiters that are selected based on the request.
__aenter__() -> Self
async
¶
Enter the transport and its underlying transport.
Entering this wrapper, for example through the client's context, also enters the composed transport, so that it can set up its resources.
__aexit__(exc_type: type[BaseException] | None = None, exc_value: BaseException | None = None, traceback: TracebackType | None = None) -> None
async
¶
Exit the transport and its underlying transport.
Exiting this wrapper, for example through the client's context, exits the composed transport with the same exception details, which lets it clean up its resources.
__init__(*, repository: AbstractRateLimiterRepository, transport: httpx2.AsyncBaseTransport, **kwargs: dict[str, object]) -> None
¶
aclose() -> None
async
¶
Close the transport and its underlying transport.
Closing this wrapper also closes the composed transport, which is why sharing an inner transport between wrappers shares its lifetime.
create(*, repository: AbstractRateLimiterRepository, **kwargs: Unpack[HTTPXAsyncHTTPTransportKeywordArguments]) -> AsyncMultiRateLimitedTransport
classmethod
¶
Create an instance of an asynchronous multiple rate-limited transport.
This factory method constructs the instance with an underlying
httpx2.AsyncHTTPTransport.
That transport is passed any additional keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repository
|
AbstractRateLimiterRepository
|
The repository to use for rate limiters. |
required |
**kwargs
|
Unpack[HTTPXAsyncHTTPTransportKeywordArguments]
|
Additional keyword arguments are used in the construction of an
|
{}
|
Returns:
| Type | Description |
|---|---|
AsyncMultiRateLimitedTransport
|
A default instance of the class created from the given arguments. |
handle_async_request(request: httpx2.Request) -> httpx2.Response
async
¶
Handle an asynchronous request with rate limiting.
HTTPXAsyncHTTPTransportKeywordArguments
¶
Bases: TypedDict
Keyword arguments for the httpx2.AsyncHTTPTransport constructor.
cert: CertTypes | None
instance-attribute
¶
http1: bool
instance-attribute
¶
http2: bool
instance-attribute
¶
limits: httpx2.Limits
instance-attribute
¶
local_address: str | None
instance-attribute
¶
proxy: ProxyTypes | None
instance-attribute
¶
retries: int
instance-attribute
¶
socket_options: Iterable[SocketOption] | None
instance-attribute
¶
trust_env: bool
instance-attribute
¶
uds: str | None
instance-attribute
¶
verify: ssl.SSLContext | str | bool
instance-attribute
¶
Rate limiters¶
httpx_limiter.aiolimiter
¶
Provide aiolimiter-based rate limiting functionality.
AiolimiterAsyncLimiter
¶
Bases: AbstractAsyncLimiter
Define an asynchronous limiter that composes the aiolimiter AsyncLimiter.
This class encapsulates the creation and configuration of an aiolimiter AsyncLimiter with appropriate settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limiter
|
AsyncLimiter
|
An instance of an aiolimiter AsyncLimiter. |
required |
**kwargs
|
dict[str, object]
|
Additional keyword arguments for the parent classes. |
{}
|
__aenter__() -> AiolimiterAsyncLimiter
async
¶
Acquire a token upon entering the asynchronous context.
__aexit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) -> None
async
¶
Exit the asynchronous context.
__init__(*, limiter: AsyncLimiter, **kwargs: dict[str, object]) -> None
¶
create(rate: Rate) -> AiolimiterAsyncLimiter
classmethod
¶
Create an instance of AiolimiterAsyncLimiter.
Note: aiolimiter only supports a single rate limit, unlike pyrate-limiter which supports multiple rates.
httpx_limiter.pyrate
¶
Provide pyrate-based rate limiting functionality.
PyrateAsyncLimiter
¶
Bases: AbstractAsyncLimiter
Define an asynchronous limiter that composes the pyrate limiter.
This class encapsulates the creation and configuration of a pyrate-limiter Limiter with appropriate async bucket wrapper and settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limiter
|
Limiter
|
An instance of an asynchronous pyrate limiter. |
required |
**kwargs
|
dict[str, object]
|
Additional keyword arguments for the parent classes. |
{}
|
__aenter__() -> PyrateAsyncLimiter
async
¶
Acquire a token upon entering the asynchronous context.
__init__(*, limiter: Limiter, **kwargs: dict[str, object]) -> None
¶
create(*rates: Rate, **kwargs: Unpack[PyRateLimiterKeywordArguments]) -> PyrateAsyncLimiter
classmethod
¶
Create an instance of PyrateAsyncLimiter.