Skip to main content

netspeed_cli/
http_client.rs

1//! Abstraction over HTTP client used by phases.
2
3use crate::error::Error;
4use async_trait::async_trait;
5use reqwest::Response;
6
7/// Minimal HTTP client interface required by the test phases.
8#[async_trait]
9pub trait HttpClient: Send + Sync {
10    async fn get(&self, url: &str) -> Result<Response, Error>;
11    async fn post(
12        &self,
13        url: &str,
14        body: impl Into<reqwest::Body> + Send,
15    ) -> Result<Response, Error>;
16}
17
18/// Production implementation that forwards to `reqwest::Client`.
19pub struct ReqwestClient(pub reqwest::Client);
20
21impl ReqwestClient {
22    // Convenience wrapper used by legacy tests
23    pub async fn get(&self, url: &str) -> Result<reqwest::Response, crate::error::Error> {
24        self.0
25            .get(url)
26            .send()
27            .await
28            .map_err(crate::error::Error::from)
29    }
30}
31
32#[async_trait]
33impl HttpClient for ReqwestClient {
34    async fn get(&self, url: &str) -> Result<Response, Error> {
35        self.0.get(url).send().await.map_err(Error::NetworkError)
36    }
37    async fn post(
38        &self,
39        url: &str,
40        body: impl Into<reqwest::Body> + Send,
41    ) -> Result<Response, Error> {
42        self.0
43            .post(url)
44            .body(body)
45            .send()
46            .await
47            .map_err(Error::NetworkError)
48    }
49}