mockforge_proxy/
client.rs1use mockforge_core::{Error, Result};
4use std::collections::HashMap;
5
6pub struct ProxyClient {
8 client: reqwest::Client,
10}
11
12impl ProxyClient {
13 pub fn new() -> Self {
15 let client = reqwest::Client::new();
16 Self { client }
17 }
18
19 pub fn with_options(timeout_seconds: u64, follow_redirects: bool) -> Result<Self> {
21 let redirects = if follow_redirects {
22 reqwest::redirect::Policy::limited(10)
23 } else {
24 reqwest::redirect::Policy::none()
25 };
26 let client = reqwest::Client::builder()
27 .timeout(std::time::Duration::from_secs(timeout_seconds))
28 .redirect(redirects)
29 .build()
30 .map_err(|e| Error::internal(format!("Cannot configure proxy client: {e}")))?;
31 Ok(Self { client })
32 }
33
34 pub async fn send_request(
36 &self,
37 method: reqwest::Method,
38 url: &str,
39 headers: &HashMap<String, String>,
40 body: Option<&[u8]>,
41 ) -> Result<reqwest::Response> {
42 let mut request = self.client.request(method, url);
43
44 for (key, value) in headers {
46 request = request.header(key, value);
47 }
48
49 if let Some(body_data) = body {
51 request = request.body(body_data.to_vec());
52 }
53
54 request
55 .send()
56 .await
57 .map_err(|e| Error::internal(format!("Proxy request failed: {}", e)))
58 }
59}
60
61pub struct ProxyResponse {
63 pub status_code: u16,
65 pub headers: HashMap<String, String>,
67 pub body: Option<Vec<u8>>,
69}
70
71impl Default for ProxyClient {
72 fn default() -> Self {
73 Self::new()
74 }
75}