Skip to main content

tin_nacos_wrapper/
http.rs

1use reqwest::{Client, Error, Response};
2use std::collections::HashMap;
3
4pub struct HttpClient {
5    client: Client,
6}
7
8impl Default for HttpClient {
9    fn default() -> Self {
10        Self::new()
11    }
12}
13
14impl HttpClient {
15    pub fn new() -> Self {
16        HttpClient {
17            client: Client::new(),
18        }
19    }
20
21    pub async fn get(
22        &self,
23        url: &str,
24        headers: Option<HashMap<String, String>>,
25    ) -> Result<Response, Error> {
26        let mut req = self.client.get(url);
27        if let Some(hdrs) = headers {
28            for (k, v) in hdrs {
29                req = req.header(&k, &v);
30            }
31        }
32        req.send().await
33    }
34
35    pub async fn post<T: serde::Serialize + ?Sized>(
36        &self,
37        url: &str,
38        body: &T,
39        headers: Option<HashMap<String, String>>,
40    ) -> Result<Response, Error> {
41        let mut req = self.client.post(url).json(body);
42        if let Some(hdrs) = headers {
43            for (k, v) in hdrs {
44                req = req.header(&k, &v);
45            }
46        }
47        req.send().await
48    }
49}