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
use chrono::{DateTime, Utc, Duration};
use serde_json::Value;
use client::response::{FromResponse, ParseError};
use token::Lifetime;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Expiring {
expires: DateTime<Utc>,
}
impl Expiring {
pub fn expires(&self) -> &DateTime<Utc> { &self.expires }
}
impl Lifetime for Expiring {
fn expired(&self) -> bool { self.expires < Utc::now() }
}
impl FromResponse for Expiring {
fn from_response(json: &Value) -> Result<Self, ParseError> {
let obj = json.as_object().ok_or(ParseError::ExpectedType("object"))?;
if obj.contains_key("refresh_token") {
return Err(ParseError::UnexpectedField("refresh_token"));
}
let expires_in = obj.get("expires_in")
.and_then(Value::as_i64)
.ok_or(ParseError::ExpectedFieldType("expires_in", "i64"))?;
Ok(Expiring {
expires: Utc::now() + Duration::seconds(expires_in),
})
}
}
#[cfg(test)]
mod tests {
use chrono::{Utc, Duration};
use client::response::FromResponse;
use super::Expiring;
#[test]
fn from_response() {
let json = r#"{"expires_in":3600}"#.parse().unwrap();
let expiring = Expiring::from_response(&json).unwrap();
assert!(expiring.expires > Utc::now());
assert!(expiring.expires <= Utc::now() + Duration::seconds(3600));
}
}