use crate::backend::{HttpRequest, HttpResponse};
use crate::Error;
use super::{BoxHttpService, ReqwestHttpService};
pub use tower::limit::{ConcurrencyLimitLayer, RateLimitLayer};
pub use tower::timeout::TimeoutLayer;
pub use tower::ServiceBuilder;
pub fn reqwest_service(client: reqwest::Client) -> ReqwestHttpService {
ReqwestHttpService::new(client)
}
pub fn build<F>(client: reqwest::Client, configure: F) -> BoxHttpService
where
F: FnOnce(ReqwestHttpService) -> BoxHttpService,
{
configure(ReqwestHttpService::new(client))
}
pub trait IntoBoxHttpService: Sized {
fn into_box(self) -> BoxHttpService;
}
impl<S> IntoBoxHttpService for S
where
S: tower::Service<HttpRequest, Response = HttpResponse, Error = Error> + Clone + Send + 'static,
S::Future: Send + 'static,
{
fn into_box(self) -> BoxHttpService {
BoxHttpService::new(self)
}
}
pub fn with_concurrency_limit(client: reqwest::Client, max_in_flight: usize) -> BoxHttpService {
build(client, |inner| {
ServiceBuilder::new()
.layer(ConcurrencyLimitLayer::new(max_in_flight))
.service(inner)
.into_box()
})
}
pub fn with_request_logging(client: reqwest::Client) -> BoxHttpService {
build(client, |inner| {
ServiceBuilder::new()
.map_request(|req: HttpRequest| {
tracing::debug!(method = %req.method, url = %req.url, "better-fetch transport");
req
})
.service(inner)
.into_box()
})
}