HTTP Layer¶
Low-level HTTP transport with rate limiting and retry logic.
HttpClient¶
- class etoropy.HttpClient(config, *, rate_limiter=None)[source]¶
Bases:
objectAsync HTTP client wrapping
httpx.AsyncClient.Every request automatically includes
x-api-keyandx-user-keyauth headers, passes through the token-bucket rate limiter, and is retried on transient failures (5xx, 429, connection errors) with exponential backoff and jitter.Pydantic
BaseModelrequest bodies are serialized withmodel_dump(by_alias=True, exclude_none=True)so PascalCase field aliases are preserved for the eToro API.- Parameters:
config (
EToroConfig)rate_limiter (
RateLimiterOptions|None|bool(default:None))
- async request(options, response_type=None)[source]¶
- Parameters:
options (
RequestOptions)
- Return type:
RequestOptions¶
RateLimiter¶
- class etoropy.RateLimiter(options=None)[source]¶
Bases:
objectToken-bucket rate limiter for outgoing HTTP requests.
Tracks request timestamps in a sliding window (default: 20 requests per 10 seconds).
acquire()blocks the caller when the bucket is full until a slot opens.On HTTP 429 responses, call
penalize(retry_after_s)to force all subsequent requests to wait for the server-specified back-off period.- Parameters:
options (
RateLimiterOptions|None(default:None))
RateLimiterOptions¶
RetryOptions¶
- class etoropy.http.RetryOptions(attempts=3, delay=1.0, backoff_multiplier=2.0, jitter=True, should_retry=<factory>, get_retry_after_s=None, on_retry=None)[source]¶
Bases:
objectConfiguration for the
retry()helper.- Parameters:
attempts (
int(default:3)) – Maximum number of tries (including the first).delay (
float(default:1.0)) – Base delay in seconds before the first retry.backoff_multiplier (
float(default:2.0)) – Multiplied to the delay after each attempt.jitter (
bool(default:True)) – IfTrue, adds +/-25% random jitter to the computed delay.should_retry (
Callable[[BaseException],bool] (default:<factory>)) – Predicate that receives the exception and returnsTrueif the request should be retried.get_retry_after_s (
Callable[[BaseException],float|None] |None(default:None)) – Optional callback that extracts a server-supplied retry-after delay from the exception (e.g. from a 429 response).on_retry (
Callable[[int,float,BaseException],None] |None(default:None)) – Optional callback fired before each retry sleep, receiving(attempt_number, wait_seconds, exception).
- should_retry: Callable[[BaseException], bool]¶