use crate::common::DEFAULT_PROVIDER_TIMEOUT_SECS;
use crate::error::LarpshellError;
use reqwest::Client;
use std::time::Duration;
pub fn create_http_client() -> Result<Client, LarpshellError> {
Client::builder()
.timeout(Duration::from_secs(DEFAULT_PROVIDER_TIMEOUT_SECS))
.build()
.map_err(|e| LarpshellError::ConfigError(e.to_string()))
}
pub(crate) fn strip_url_for_display(url: &str) -> &str {
url.trim_start_matches("http://")
.trim_start_matches("https://")
.trim_end_matches("/*")
.trim_end_matches('/')
}
pub struct BaseProvider {
pub(crate) client: Client,
}
impl BaseProvider {
pub fn new() -> Result<Self, LarpshellError> {
Ok(Self {
client: create_http_client()?,
})
}
pub async fn check_response(
response: reqwest::Response,
provider: &str,
) -> Result<reqwest::Response, LarpshellError> {
if !response.status().is_success() {
let status = response.status();
let error_text = response
.text()
.await
.unwrap_or_else(|_| "unknown error".to_string());
return Err(LarpshellError::from_http_status(
status,
provider,
&error_text,
));
}
Ok(response)
}
}