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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::borrow::Cow;
use std::fmt;
use hyper::client::response::Response;
use hyper::header;
use response::NamedResponse;
use response;
#[derive(Deserialize)]
pub struct HeaderOnly {
#[serde(rename="content-type")]
pub content_type: String,
pub status: String,
#[serde(rename="ratelimit-limit")]
pub ratelimit_limit: f64,
#[serde(rename="ratelimit-remaining")]
pub ratelimit_remaining: f64,
#[serde(rename="ratelimit-reset")]
pub ratelimit_reset: f64
}
impl response::NotArray for HeaderOnly {}
impl HeaderOnly {
pub fn from_response(r: Response) -> Result<HeaderOnly, String> {
let c_type = match r.headers.get::<header::ContentType>() {
Some(c) => c,
None => return Err("No content-type provided".to_owned())
};
let raw_status = r.status_raw();
let status = format!("{} {}", raw_status.0, raw_status.1);
let rl_limit_raw = match r.headers.get_raw("ratelimit-limit") {
Some(c) => c,
None => return Err("No ratelimit-limit provided".to_owned())
};
let rl_remain_raw = match r.headers.get_raw("ratelimit-remaining") {
Some(c) => c,
None => return Err("No ratelimit-remaining provided".to_owned())
};
let rl_reset_raw = match r.headers.get_raw("ratelimit-reset") {
Some(c) => c,
None => return Err("No ratelimit-reset provided".to_owned())
};
let rl_limit_str = String::from_utf8_lossy(&rl_limit_raw[0][..]);
let rl_remain_str = String::from_utf8_lossy(&rl_remain_raw[0][..]);
let rl_reset_str = String::from_utf8_lossy(&rl_reset_raw[0][..]);
let rl_limit = match rl_limit_str.as_ref().parse::<f64>() {
Ok(n) => n,
Err(e) => return Err(e.to_string())
};
let rl_remain = match rl_remain_str.as_ref().parse::<f64>() {
Ok(n) => n,
Err(e) => return Err(e.to_string())
};
let rl_reset = match rl_reset_str.as_ref().parse::<f64>() {
Ok(n) => n,
Err(e) => return Err(e.to_string())
};
Ok(HeaderOnly {
content_type: c_type.to_string(),
status: status,
ratelimit_limit: rl_limit,
ratelimit_remaining: rl_remain,
ratelimit_reset: rl_reset
})
}
}
impl NamedResponse for HeaderOnly {
fn name<'a>() -> Cow<'a, str> {
"header".into()
}
}
impl fmt::Display for HeaderOnly {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Status: {}\n\
Request Limit: {:.0}\n\
Requests Remaining: {:.0}\n\
Time to Reset: {:.0}\n",
self.status,
self.ratelimit_limit,
self.ratelimit_remaining,
self.ratelimit_reset
)
}
}
impl fmt::Debug for HeaderOnly {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "content-type: {:?}\n\
status: {:?}\n\
ratelimit-limit: {:.0}\n\
ratelimit-remaining: {:.0}\n\
ratelimit_reset: {:.0}\n",
self.content_type,
self.status,
self.ratelimit_limit,
self.ratelimit_remaining,
self.ratelimit_reset
)
}
}