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: HTTPAdapter

An HTTPAdapter that transparently enforces rate limiting and bandwidth constraints on every request sent through the session.

Mount on an OAuth2Session (or any requests.Session) to apply throttling automatically — no acquire() or record() calls at call sites.

Parameters:
  • rate_limiter – Optional RateLimiter that blocks before each send until a request slot is available within the sliding window.

  • bw_guard – Optional BandwidthGuard that 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 by requests for every request routed through this adapter, not directly by SDK users.