use crate::error::{BotError, Result};
use reqwest::Client;
use std::time::Duration;
#[derive(Clone)]
pub struct HttpClient {
pub(crate) client: Client,
pub(crate) base_url: String,
pub(crate) union_app_id: Option<String>,
}
impl HttpClient {
pub fn new(timeout: u64, is_sandbox: bool) -> Result<Self> {
let client = Client::builder()
.timeout(Duration::from_secs(timeout))
.user_agent(format!("BotRS/{}", crate::VERSION))
.build()
.map_err(BotError::Http)?;
let base_url = if is_sandbox {
crate::SANDBOX_API_URL.to_string()
} else {
crate::DEFAULT_API_URL.to_string()
};
Ok(Self {
client,
base_url,
union_app_id: None,
})
}
pub(crate) fn with_union_app_id(&self, app_id: impl Into<String>) -> Self {
Self {
union_app_id: Some(app_id.into()),
..self.clone()
}
}
}
impl std::fmt::Debug for HttpClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HttpClient")
.field("base_url", &self.base_url)
.finish()
}
}