use chrono::{offset::TimeZone, DateTime, Utc};
#[derive(Clone, PartialEq, Debug, serde::Deserialize)]
pub struct Token {
pub access_token: String,
pub refresh_token: String,
pub token_type: String,
pub expires_in: Option<i64>,
pub expires_in_timestamp: Option<i64>,
}
impl Token {
pub fn has_expired(&self) -> bool {
self.access_token.is_empty() || self.expiry_date() <= Utc::now()
}
pub fn expiry_date(&self) -> DateTime<Utc> {
match self.expires_in_timestamp {
Some(ts) => Utc.timestamp(ts, 0),
None => Utc::now(),
}
}
}
impl std::convert::TryInto<http::header::HeaderValue> for Token {
type Error = crate::Error;
fn try_into(self) -> Result<http::header::HeaderValue, crate::Error> {
let auth_header_val = format!("{} {}", self.token_type, self.access_token);
let auth_header_val = bytes::Bytes::from(auth_header_val);
http::header::HeaderValue::from_shared(auth_header_val)
.map_err(|e| crate::Error::from(http::Error::from(e)))
}
}