auth0_integration/services/
http_client.rs1use reqwest::{Client, Response};
2use serde::Serialize;
3
4use crate::config::Auth0Config;
5use crate::error::AppError;
6
7pub struct HttpClient {
8 client: Client,
9 base_url: String,
10}
11
12impl HttpClient {
13 pub fn new(config: &Auth0Config) -> Self {
14 Self {
15 client: Client::new(),
16 base_url: format!("https://{}", config.auth0_domain),
17 }
18 }
19
20 fn url(&self, path: &str) -> String {
21 format!("{}{}", self.base_url.trim_end_matches('/'), path)
22 }
23
24 pub async fn get(&self, path: &str) -> Result<Response, AppError> {
25 Ok(self.client.get(self.url(path)).send().await?)
26 }
27
28 pub async fn get_authorized(&self, path: &str, bearer_token: &str) -> Result<Response, AppError> {
29 Ok(self
30 .client
31 .get(self.url(path))
32 .bearer_auth(bearer_token)
33 .send()
34 .await?)
35 }
36
37 pub async fn post<B: Serialize>(&self, path: &str, body: &B) -> Result<Response, AppError> {
38 Ok(self.client.post(self.url(path)).json(body).send().await?)
39 }
40
41 pub async fn post_authorized<B: Serialize>(
42 &self,
43 path: &str,
44 body: &B,
45 bearer_token: &str,
46 ) -> Result<Response, AppError> {
47 Ok(self
48 .client
49 .post(self.url(path))
50 .bearer_auth(bearer_token)
51 .json(body)
52 .send()
53 .await?)
54 }
55
56 pub async fn put<B: Serialize>(&self, path: &str, body: &B) -> Result<Response, AppError> {
57 Ok(self.client.put(self.url(path)).json(body).send().await?)
58 }
59
60 pub async fn patch<B: Serialize>(&self, path: &str, body: &B) -> Result<Response, AppError> {
61 Ok(self.client.patch(self.url(path)).json(body).send().await?)
62 }
63
64 pub async fn patch_authorized<B: Serialize>(
65 &self,
66 path: &str,
67 body: &B,
68 bearer_token: &str,
69 ) -> Result<Response, AppError> {
70 Ok(self
71 .client
72 .patch(self.url(path))
73 .bearer_auth(bearer_token)
74 .json(body)
75 .send()
76 .await?)
77 }
78
79 pub async fn delete(&self, path: &str) -> Result<Response, AppError> {
80 Ok(self.client.delete(self.url(path)).send().await?)
81 }
82
83 pub async fn delete_authorized(&self, path: &str, bearer_token: &str) -> Result<Response, AppError> {
84 Ok(self
85 .client
86 .delete(self.url(path))
87 .bearer_auth(bearer_token)
88 .send()
89 .await?)
90 }
91
92 pub async fn delete_authorized_with_body<B: Serialize>(
93 &self,
94 path: &str,
95 body: &B,
96 bearer_token: &str,
97 ) -> Result<Response, AppError> {
98 Ok(self
99 .client
100 .delete(self.url(path))
101 .bearer_auth(bearer_token)
102 .json(body)
103 .send()
104 .await?)
105 }
106}