rmq_rs_admin/modules/
client.rs1use std::{collections::HashMap, error::Error, time};
2
3use reqwest::{header::HeaderMap, Response};
4
5#[derive(Clone)]
6pub struct Client {
7 pub host: String,
8 pub port: u16,
9 pub auth_token: String,
10 pub timeout: u8,
11}
12
13impl Client {
14 pub fn new(host: String, port: u16, auth_token: String, timeout: u8) -> Box<Client> {
15 Box::new(Client {
16 host,
17 port,
18 auth_token,
19 timeout,
20 })
21 }
22
23 pub async fn get(
24 &self,
25 uri: String,
26 headers: Option<HeaderMap>,
27 ) -> Result<Response, Box<dyn Error>> {
28 let client = reqwest::Client::new();
29
30 let mut _headers = HeaderMap::new();
31 match headers {
32 Some(h) => _headers.extend(h),
33 None => {}
34 }
35 _headers.insert(
36 "Authorization",
37 format!("Basic {}", self.auth_token).parse()?,
38 );
39
40 let url: String = format!("{}:{}/{}", self.host, self.port, uri).parse()?;
41
42 let client = client
43 .get(url)
44 .headers(_headers)
45 .timeout(time::Duration::from_secs(self.timeout.into()))
46 .send()
47 .await?;
48 Ok(client)
49 }
50
51 pub async fn post(
52 &self,
53 uri: String,
54 headers: Option<HeaderMap>,
55 body: Option<HashMap<String, String>>,
56 ) -> Result<Response, Box<dyn Error>> {
57 let client = reqwest::Client::new();
58
59 let mut _headers = HeaderMap::new();
60 match headers {
61 Some(h) => _headers.extend(h),
62 None => {}
63 }
64 _headers.insert(
65 "Authorization",
66 format!("Basic {}", self.auth_token).parse()?,
67 );
68
69 let url: String = format!("{}:{}/{}", self.host, self.port, uri).parse()?;
70
71 let mut _body = HashMap::new();
72 match body {
73 Some(b) => _body.extend(b),
74 None => {}
75 }
76 let response = client
77 .post(url)
78 .headers(_headers)
79 .timeout(time::Duration::from_secs(self.timeout.into()))
80 .json(&_body)
81 .send()
82 .await?;
83 Ok(response)
84 }
85}