use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ResponseMetadata {
pub status_code: u16,
pub request_id: Option<String>,
pub rate_limit_remaining: Option<i64>,
pub rate_limit_reset: Option<i64>,
pub headers: std::collections::HashMap<String, String>,
}
impl ResponseMetadata {
pub fn from_parts(status_code: u16, headers: &http1::HeaderMap) -> Self {
let mut hm = std::collections::HashMap::new();
for (k, v) in headers.iter() {
if let Ok(val) = v.to_str() {
hm.insert(k.as_str().to_string(), val.to_string());
}
}
Self {
status_code,
request_id: hm.get("x-request-id").cloned(),
rate_limit_remaining: hm.get("x-ratelimit-remaining").and_then(|v| v.parse().ok()),
rate_limit_reset: hm.get("x-ratelimit-reset").and_then(|v| v.parse().ok()),
headers: hm,
}
}
}