use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use bytes::Bytes;
use crate::{Client, Error, NativeRequestOptions, NativeResponseBody};
#[derive(Clone, Debug)]
pub struct NativeHttpService {
client: Client,
options: NativeRequestOptions,
}
impl NativeHttpService {
#[must_use]
pub fn new(client: Client) -> Self {
Self {
client,
options: NativeRequestOptions::default(),
}
}
#[must_use]
pub fn with_options(mut self, options: NativeRequestOptions) -> Self {
self.options = options;
self
}
}
impl Client {
#[must_use]
pub fn native_service(&self) -> NativeHttpService {
NativeHttpService::new(self.clone())
}
}
impl<B> tower_service::Service<http::Request<B>> for NativeHttpService
where
B: http_body::Body<Data = Bytes> + Send + 'static,
B::Error: std::error::Error + Send + Sync + 'static,
{
type Response = http::Response<NativeResponseBody>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, request: http::Request<B>) -> Self::Future {
let client = self.client.clone();
let options = self.options.clone();
Box::pin(async move { client.execute_http_body(request, options).await })
}
}