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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::collections::HashMap;
use std::default::Default;
use std::str::FromStr;
pub type HttpHeaderType = HashMap<String, Vec<String>>;
pub trait Populatable {
fn populate(raw_header: &HttpHeaderType) -> Self;
}
#[derive(Debug)]
pub struct Rate {
pub limit: u32,
pub remaining: u32,
pub reset: String
}
#[derive(Debug)]
pub struct Page {
pub number: u64,
}
#[derive(Debug)]
pub struct Response {
pub resp: HttpHeaderType,
pub next: Option<Page>,
pub last: Option<Page>,
pub first: Option<Page>,
pub prev: Option<Page>,
pub rate: Rate,
}
fn get_single_header_value<T>(raw_data: &HttpHeaderType, key: &str) -> T where T: Default + FromStr {
match str::parse(&raw_data[key][0]) {
Ok(x) => x,
Err(..) => Default::default(),
}
}
impl Populatable for Rate {
fn populate(raw_header: &HttpHeaderType) -> Rate {
Rate {
limit: get_single_header_value(raw_header, "x-ratelimit-limit"),
remaining: get_single_header_value(raw_header, "x-ratelimit-remaining"),
reset: raw_header["x-ratelimit-reset"][0].clone(),
}
}
}
impl Populatable for Response {
fn populate(raw_header: &HttpHeaderType) -> Response {
Response {
next: None,
last: None,
first: None,
prev: None,
rate: Rate::populate(raw_header),
resp: raw_header.clone(),
}
}
}