gitignore_template_generator/http_client/
impls.rs

1use crate::{ErrorKind, ProgramError, constant, http_client::api::HttpClient};
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                        constant::error_messages::BODY_PARSING_ISSUE,
22                    ),
23                    exit_status: constant::exit_status::BODY_PARSING_ISSUE,
24                    styled_message: None,
25                    error_kind: ErrorKind::Other,
26                }),
27            },
28            Err(error) => Err(ProgramError {
29                message: constant::error_messages::API_CALL_FAILURE
30                    .replace("{error}", &error.to_string()),
31                exit_status: constant::exit_status::GENERIC,
32                styled_message: None,
33                error_kind: ErrorKind::Other,
34            }),
35        }
36    }
37}
38
39impl HttpClient for MockClient {
40    fn get(&self, _url: &str) -> Result<String, ProgramError> {
41        self.response.clone()
42    }
43}