use std::sync::Arc;
use vantage_api_pool::resilient::TransportObserver;
use super::{FilterStrategy, OrderingParams, PaginationParams, ResponseShape, RestApi};
use crate::transport::AuthHeader;
#[derive(Clone, Debug)]
pub struct RestApiBuilder {
base_url: String,
auth_header: AuthHeader,
response_shape: ResponseShape,
pagination: PaginationParams,
paged: bool,
no_pagination: bool,
filter_strategy: FilterStrategy,
total_key: Option<String>,
ordering: Option<OrderingParams>,
debug: bool,
transport: crate::transport::ClientConfig,
}
impl RestApiBuilder {
pub(super) fn new(base_url: String) -> Self {
Self {
base_url,
auth_header: AuthHeader::default(),
response_shape: ResponseShape::default(),
pagination: PaginationParams::default(),
paged: false,
no_pagination: false,
filter_strategy: FilterStrategy::default(),
total_key: None,
ordering: None,
debug: false,
transport: crate::transport::ClientConfig::default(),
}
}
pub fn max_parallel(mut self, n: usize) -> Self {
self.transport.max_parallel = n.max(1);
self
}
pub fn rate_limit(mut self, per_second: f64) -> Self {
self.transport.rate_limit = Some(per_second);
self
}
pub fn request_timeout(mut self, timeout: std::time::Duration) -> Self {
self.transport.request_timeout = Some(Some(timeout));
self
}
pub fn no_request_timeout(mut self) -> Self {
self.transport.request_timeout = Some(None);
self
}
pub fn observer(
mut self,
key: impl Into<Arc<str>>,
observer: Arc<dyn TransportObserver>,
) -> Self {
self.transport.observer = Some((key.into(), observer));
self
}
pub fn http_client(mut self, client: reqwest::Client) -> Self {
self.transport.http = Some(client);
self
}
pub fn auth(mut self, auth: impl Into<String>) -> Self {
self.auth_header = AuthHeader::new(auth);
self.transport.auth_refresher = None;
self
}
pub fn auth_refresher(mut self, refresher: crate::AuthRefresher) -> Self {
self.auth_header = AuthHeader::default();
self.transport.auth_refresher = Some(refresher);
self
}
pub fn bearer_source(mut self, source: crate::BearerSource) -> Self {
self.auth_header = AuthHeader::Source(source);
self.transport.auth_refresher = None;
self
}
pub fn response_shape(mut self, shape: ResponseShape) -> Self {
self.response_shape = shape;
self
}
pub fn pagination_params(mut self, pagination: PaginationParams) -> Self {
self.pagination = pagination;
self.paged = true;
self
}
pub fn no_pagination(mut self) -> Self {
self.no_pagination = true;
self
}
pub fn filter_strategy(mut self, strategy: FilterStrategy) -> Self {
self.filter_strategy = strategy;
self
}
pub fn total_key(mut self, key: impl Into<String>) -> Self {
self.total_key = Some(key.into());
self
}
pub fn ordering(mut self, ordering: OrderingParams) -> Self {
self.ordering = Some(ordering);
self
}
pub fn debug(mut self, debug: bool) -> Self {
self.debug = debug;
self
}
pub fn build(self) -> RestApi {
RestApi {
base_url: self.base_url,
client: crate::transport::build_client(self.transport),
auth_header: self.auth_header,
response_shape: self.response_shape,
pagination: self.pagination,
paged: self.paged,
no_pagination: self.no_pagination,
filter_strategy: self.filter_strategy,
total_key: self.total_key,
ordering: self.ordering,
debug: self.debug,
}
}
}