bolt_web/
client.rs

1use bytes::Bytes;
2use http_body_util::{BodyExt, Full};
3use hyper::{Method, Request};
4
5use hyper_tls::HttpsConnector;
6use hyper_util::client::legacy::{Client as HyperClient, connect::HttpConnector};
7use hyper_util::rt::TokioExecutor;
8use serde::Serialize;
9use serde::de::DeserializeOwned;
10
11use crate::types::BoltError;
12
13#[derive(Clone)]
14#[allow(dead_code)]
15pub struct Client {
16    client: HyperClient<HttpsConnector<HttpConnector>, Full<Bytes>>,
17}
18
19#[allow(dead_code)]
20impl Client {
21    pub fn new() -> Self {
22        let https = HttpsConnector::new();
23        let client = HyperClient::builder(TokioExecutor::new()).build::<_, Full<Bytes>>(https);
24        Self { client }
25    }
26
27    pub async fn fetch(&self, url: &str) -> Result<String, BoltError> {
28        let req = Request::builder()
29            .method(Method::GET)
30            .uri(url)
31            .body(Full::new(Bytes::new()))?;
32
33        let resp = self.client.request(req).await?;
34
35        let body_bytes = resp.into_body().collect().await?.to_bytes();
36
37        Ok(String::from_utf8_lossy(&body_bytes).to_string())
38    }
39
40    async fn send_json<T: Serialize + ?Sized, U: DeserializeOwned>(
41        &self,
42        method: Method,
43        url: &str,
44        body: &T,
45    ) -> Result<U, BoltError> {
46        let body_bytes = serde_json::to_vec(body)?;
47        let req = Request::builder()
48            .method(method)
49            .uri(url)
50            .header("Content-Type", "application/json")
51            .body(Full::new(Bytes::from(body_bytes)))?;
52        let resp = self.client.request(req).await?;
53        let body_bytes = resp.into_body().collect().await?.to_bytes();
54        Ok(serde_json::from_slice(&body_bytes)?)
55    }
56
57    pub async fn get<T: DeserializeOwned>(&self, url: &str) -> Result<T, BoltError> {
58        let req = Request::builder()
59            .method(Method::GET)
60            .uri(url)
61            .body(Full::new(Bytes::new()))?;
62
63        let resp = self.client.request(req).await?;
64
65        let body_bytes = resp.into_body().collect().await?.to_bytes();
66
67        let parsed: T = serde_json::from_slice(&body_bytes)?;
68        Ok(parsed)
69    }
70
71    pub async fn post<T: Serialize + ?Sized, U: DeserializeOwned>(
72        &self,
73        url: &str,
74        body: &T,
75    ) -> Result<U, BoltError> {
76        self.send_json(Method::POST, url, body).await
77    }
78
79    pub async fn put<T: Serialize + ?Sized, U: DeserializeOwned>(
80        &self,
81        url: &str,
82        body: &T,
83    ) -> Result<U, BoltError> {
84        self.send_json(Method::PUT, url, body).await
85    }
86
87    pub async fn patch<T: Serialize + ?Sized, U: DeserializeOwned>(
88        &self,
89        url: &str,
90        body: &T,
91    ) -> Result<U, BoltError> {
92        self.send_json(Method::PATCH, url, body).await
93    }
94
95    pub async fn delete<U: DeserializeOwned>(&self, url: &str) -> Result<U, BoltError> {
96        let req = Request::builder()
97            .method(Method::DELETE)
98            .uri(url)
99            .body(Full::new(Bytes::new()))?;
100
101        let resp = self.client.request(req).await?;
102
103        let body_bytes = resp.into_body().collect().await?.to_bytes();
104
105        Ok(serde_json::from_slice(&body_bytes)?)
106    }
107
108    pub async fn delete_with_payload<T: Serialize + ?Sized, U: DeserializeOwned>(
109        &self,
110        url: &str,
111        body: &T,
112    ) -> Result<U, BoltError> {
113        self.send_json(Method::DELETE, url, body).await
114    }
115}