client_rust/
api_client.rs1use reqwest::{Client, Method};
2
3
4#[derive(Clone)]
5pub struct ApiClient {
6 base_url: String,
7 client: Client,
8}
9
10impl ApiClient {
11 pub fn new(base_url: String) -> Self {
12 Self {
13 base_url,
14 client: Client::new(),
15 }
16 }
17
18 pub async fn send_request(
19 &self,
20 method: Method,
21 path: &str,
22 body: Option<serde_json::Value>,
23 ) -> Result<serde_json::Value, reqwest::Error> {
24 let url = format!("{}/{}", self.base_url, path);
25 let mut request = self.client.request(method, &url);
26
27 if let Some(body) = body {
28 request = request.json(&body);
29 }
30
31 let response = request.send().await?;
32 response.json::<serde_json::Value>().await
33 }
34}