use mockforge_core::{Error, Result};
use std::collections::HashMap;
pub struct ProxyClient {
client: reqwest::Client,
}
impl ProxyClient {
pub fn new() -> Self {
let client = reqwest::Client::new();
Self { client }
}
pub fn with_options(timeout_seconds: u64, follow_redirects: bool) -> Result<Self> {
let redirects = if follow_redirects {
reqwest::redirect::Policy::limited(10)
} else {
reqwest::redirect::Policy::none()
};
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(timeout_seconds))
.redirect(redirects)
.build()
.map_err(|e| Error::internal(format!("Cannot configure proxy client: {e}")))?;
Ok(Self { client })
}
pub async fn send_request(
&self,
method: reqwest::Method,
url: &str,
headers: &HashMap<String, String>,
body: Option<&[u8]>,
) -> Result<reqwest::Response> {
let mut request = self.client.request(method, url);
for (key, value) in headers {
request = request.header(key, value);
}
if let Some(body_data) = body {
request = request.body(body_data.to_vec());
}
request
.send()
.await
.map_err(|e| Error::internal(format!("Proxy request failed: {}", e)))
}
}
pub struct ProxyResponse {
pub status_code: u16,
pub headers: HashMap<String, String>,
pub body: Option<Vec<u8>>,
}
impl Default for ProxyClient {
fn default() -> Self {
Self::new()
}
}