1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use http::HeaderMap;
/// The endpoints rate limit
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
pub struct RateLimit {
/// the endpoint's limit
pub limit: u16,
/// how many requests you are allowed to make in the remaining time
pub remaining: u16,
/// a UNIX timestamp in seconds of when the rate limit resets
pub reset: u64,
}
impl RateLimit {
pub(crate) fn new(headers: &HeaderMap) -> Option<Self> {
let remaining = headers
.get("x-ratelimit-remaining")?
.to_str()
.ok()?
.parse()
.ok()?;
let reset = headers
.get("x-ratelimit-reset")?
.to_str()
.ok()?
.parse()
.ok()?;
let limit = headers
.get("x-ratelimit-limit")?
.to_str()
.ok()?
.parse()
.ok()?;
let slf = Self {
limit,
remaining,
reset,
};
Some(slf)
}
}