ThrottledHTTPAdapter¶
HTTPAdapter that transparently applies rate limiting and bandwidth guarding.
- class scamander.api.throttled_adapter.ThrottledHTTPAdapter(rate_limiter: RateLimiter | None = None, bw_guard: BandwidthGuard | None = None, **kwargs)¶
Basisklassen:
HTTPAdapterAn
HTTPAdapterthat transparently enforces rate limiting and bandwidth constraints on every request sent through the session.Mount on an
OAuth2Session(or anyrequests.Session) to apply throttling automatically — noacquire()orrecord()calls at call sites.- Parameters:
rate_limiter – Optional
RateLimiterthat blocks before each send until a request slot is available within the sliding window.bw_guard – Optional
BandwidthGuardthat records the response body size after each send and pauses when the bandwidth cap is approached.
Example:
from scamander.api import ( BandwidthGuard, OAuth2Session, RateLimiter, ThrottledHTTPAdapter, ) session = OAuth2Session(token_url=..., client_id=..., client_secret=...) adapter = ThrottledHTTPAdapter( rate_limiter=RateLimiter(max_calls=300, period_secs=60), bw_guard=BandwidthGuard(limit_mb=350, window_secs=300), ) session.mount("https://", adapter) response = session.get("https://api.example.com/data")
- send(request, **kwargs)¶
Acquire a rate-limit slot, send the request, then record its size.
Overrides
HTTPAdapter.send; called internally byrequestsfor every request routed through this adapter, not directly by SDK users.