gitignore_template_generator/http_client/
impls.rs

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