various_rust_utils/api_clients/
rest_client.rs

1use crate::api_clients::errors::ApiCallError;
2use crossterm::style::Stylize;
3
4pub enum RestApiCallType {
5    Get,
6    Post,
7}
8
9pub async fn execute_request<Req: serde::Serialize, Res: serde::de::DeserializeOwned>(
10    url: String,
11    rest_api_call_type: RestApiCallType,
12    token: Option<String>,
13    request_body: Option<Req>,
14    additional_headers: Vec<(String, String)>,
15) -> Result<Res, ApiCallError> {
16    let client = reqwest::Client::new();
17    let mut response = match rest_api_call_type {
18        RestApiCallType::Get => client.get(url),
19        RestApiCallType::Post => client.post(url),
20    };
21    if let Some(token) = token {
22        response = response.header("Authorization", format!("Bearer {}", token));
23    }
24    for additional_header in additional_headers {
25        response = response.header(additional_header.0, additional_header.1);
26    }
27    let response = match request_body {
28        Some(request_body) => response.json(&request_body).send().await,
29        None => response.send().await,
30    };
31    match response {
32        Ok(response) => map_response(response.json().await),
33        Err(error) => Err(ApiCallError::RequestFailed(error.to_string())),
34    }
35}
36
37pub async fn execute_request_and_log<Req: serde::Serialize>(
38    url: String,
39    rest_api_call_type: RestApiCallType,
40    token: Option<String>,
41    request_body: Option<Req>,
42    additional_headers: Vec<(String, String)>,
43) -> Result<(), ApiCallError> {
44    let client = reqwest::Client::new();
45    let mut response = match rest_api_call_type {
46        RestApiCallType::Get => client.get(url),
47        RestApiCallType::Post => client.post(url),
48    };
49    if let Some(token) = token {
50        response = response.header("Authorization", format!("Bearer {}", token));
51    }
52    for additional_header in additional_headers {
53        response = response.header(additional_header.0, additional_header.1);
54    }
55    match request_body {
56        Some(request_body) => {
57            let r = response.json(&request_body).send().await;
58            match r {
59                Ok(r) => {
60                    println!("{}", "Status:".red());
61                    println!("    {}", r.status());
62                    println!("{}", "Headers:".red());
63                    for x in r.headers() {
64                        if let Ok(value) = x.1.to_str() {
65                            println!("    {} - {}", x.0, value);
66                        }
67                    }
68                    if let Ok(body) = r.text().await {
69                        println!("{}", "Body:".red());
70                        println!("    {}", body);
71                    }
72                }
73                Err(e) => {
74                    println!("Error: {}", e);
75                }
76            }
77        }
78        None => {
79            let r = response.send().await;
80            match r {
81                Ok(r) => {
82                    println!("{}", "Status:".red());
83                    println!("    {}", r.status());
84                    println!("{}", "Headers:".red());
85                    for x in r.headers() {
86                        if let Ok(value) = x.1.to_str() {
87                            println!("    {} - {}", x.0, value);
88                        }
89                    }
90                    if let Ok(body) = r.text().await {
91                        println!("{}", "Body:".red());
92                        println!("    {}", body);
93                    }
94                }
95                Err(e) => {
96                    println!("Error: {}", e);
97                }
98            }
99        }
100    };
101    Ok(())
102}
103
104fn map_response<Res: serde::de::DeserializeOwned>(
105    response: reqwest::Result<Res>,
106) -> Result<Res, ApiCallError> {
107    match response {
108        Ok(response) => Ok(response),
109        Err(error) => Err(ApiCallError::JsonParse(error.to_string())),
110    }
111}