use std::time::Duration;
use reqwest::header::HeaderMap;
use reqwest::StatusCode;
use crate::error::parse_numeric_header;
#[derive(Debug, Clone)]
pub struct ApiResponse<T> {
pub data: T,
pub status: StatusCode,
pub headers: HeaderMap,
pub rate_limit: Option<RateLimitInfo>,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct RateLimitInfo {
pub limit: u64,
pub remaining: u64,
pub retry_after: Option<u64>,
}
impl RateLimitInfo {
pub(crate) fn from_headers(headers: &HeaderMap) -> Option<Self> {
let limit = parse_numeric_header(headers, "x-ratelimit-limit");
let remaining = parse_numeric_header(headers, "x-ratelimit-remaining");
let retry_after = parse_numeric_header(headers, "retry-after");
if limit.is_none() && remaining.is_none() && retry_after.is_none() {
return None;
}
Some(Self {
limit: limit.unwrap_or(0),
remaining: remaining.unwrap_or(0),
retry_after,
})
}
}
#[derive(Debug, Clone, Default)]
pub struct RequestOptions {
pub namespace_id: Option<String>,
pub user_id: Option<String>,
pub timeout: Option<Duration>,
pub retries: Option<u32>,
}
impl RequestOptions {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_namespace_id(mut self, namespace_id: impl Into<String>) -> Self {
self.namespace_id = Some(namespace_id.into());
self
}
#[must_use]
pub fn with_user_id(mut self, user_id: impl Into<String>) -> Self {
self.user_id = Some(user_id.into());
self
}
#[must_use]
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
#[must_use]
pub fn with_retries(mut self, retries: u32) -> Self {
self.retries = Some(retries);
self
}
}