use std::sync::LazyLock;
use reqwest::{Client, redirect::Policy};
use serde::de::DeserializeOwned;
use crate::{Payload, Request};
#[cfg(feature = "cookies")]
pub static COOKIE_JAR: LazyLock<std::sync::Arc<reqwest::cookie::Jar>> =
LazyLock::new(|| std::sync::Arc::new(reqwest::cookie::Jar::default()));
pub trait ApiCaller {
const BASE_URL: &'static str;
fn request<P, O>(payload: P) -> Request<P, O, ()>
where
P: Payload,
O: DeserializeOwned,
{
Request::new(payload, Self::BASE_URL.to_string(), Self::client())
}
#[cfg(feature = "stream")]
fn stream<P>(payload: P) -> Request<P, crate::RespStream, ((),)>
where
P: Payload,
{
Request::new(payload, Self::BASE_URL.to_string(), Self::client())
}
fn client() -> Client {
static CLIENT: LazyLock<Client> = LazyLock::new(|| {
let builder = Client::builder().redirect(Policy::none());
#[cfg(feature = "cookies")]
let builder = builder.cookie_provider(COOKIE_JAR.clone());
builder.build().unwrap()
});
CLIENT.clone()
}
}