gitignore_template_generator/http_client/
impls.rs

1use crate::http_client::api::{HttpClient, ProgramError};
2
3#[derive(Default)]
4pub struct UreqClient {
5    pub server_url: String,
6}
7pub struct MockClient {
8    pub response: Result<String, ProgramError>,
9}
10
11impl HttpClient for UreqClient {
12    fn get(&self, url: &str) -> Result<String, ProgramError> {
13        let full_url = format!("{}{url}", self.server_url);
14        let result = ureq::get(full_url).call();
15
16        match result {
17            Ok(mut response) => match response.body_mut().read_to_string() {
18                Ok(body) => Ok(body),
19                Err(_error) => Err(ProgramError {
20                    message: String::from(
21                        "An error occurred during body parsing",
22                    ),
23                    exit_status: 3,
24                }),
25            },
26            Err(error) => Err(ProgramError {
27                message: format!(
28                    "An error occurred during the API call: {error}"
29                ),
30                exit_status: 2,
31            }),
32        }
33    }
34}
35
36impl HttpClient for MockClient {
37    fn get(&self, _url: &str) -> Result<String, ProgramError> {
38        self.response.clone()
39    }
40}