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                }),
28            },
29            Err(error) => Err(ProgramError {
30                message: constant::error_messages::API_CALL_FAILURE
31                    .replace("{error}", &error.to_string()),
32                exit_status: constant::exit_status::GENERIC,
33            }),
34        }
35    }
36}
37
38impl HttpClient for MockClient {
39    fn get(&self, _url: &str) -> Result<String, ProgramError> {
40        self.response.clone()
41    }
42}