rate-limits
A crate for parsing HTTP rate limit headers as per the IETF draft. Unofficial implementations like the Github rate limit headers are also supported, with vendor detection driven by exact (case-sensitive) header name matching for stronger disambiguation between APIs that share header names. APIs that aren't explicitly listed still parse via a generic fallback on a best-effort basis. See vendor list for the set of recognized vendors.
The easiest way to use this crate is with http::HeaderMap (which is the standard used by reqwest, hyper, and axum).
use FromStr;
use OffsetDateTime;
use Duration;
use ;
use HeaderMap;
let mut headers = new;
headers.insert;
headers.insert;
headers.insert;
// Because these headers are generic, the crate safely identifies multiple candidates
assert_eq!;
Unique Vendor Matching
If the API response includes highly specific "extra" headers, the state machine's specificity scoring will identify the exact vendor.
You can parse rate limits directly from raw string iterators or via FromStr:
use indoc;
use FromStr;
use OffsetDateTime;
use Duration;
use ;
let headers = indoc! ;
// The parser observes that multiple vendors match the generic limit/remaining/reset headers
// (which populate the `candidates` mask). However, the presence of `x-ratelimit-used` and
// `x-ratelimit-resource` gives GitHub a higher specificity score, unambiguously identifying it
// in the `vendor` field!
assert_eq!;
Also takes the Retry-After header into account when calculating the reset
time.
Further development
There is a new IETF draft which supersedes the old "polli" draft.
It introduces a new RateLimit-Policy header which specifies the rate limit
quota policy. The goal is to support this new draft in this crate as well.
Other resources:
License: Apache-2.0/MIT