Skip to main content

edgebase_admin/
functions.rs

1use std::collections::HashMap;
2use std::sync::Arc;
3
4use edgebase_core::error::Error;
5use edgebase_core::http_client::HttpClient;
6use serde_json::Value;
7
8pub struct FunctionsClient {
9    http: Arc<HttpClient>,
10}
11
12impl FunctionsClient {
13    pub(crate) fn new(http: Arc<HttpClient>) -> Self {
14        Self { http }
15    }
16
17    pub async fn call(
18        &self,
19        path: &str,
20        method: &str,
21        body: Option<&Value>,
22        query: Option<&HashMap<String, String>>,
23    ) -> Result<Value, Error> {
24        let normalized_path = format!("/api/functions/{}", path.trim_start_matches('/'));
25
26        match method.to_uppercase().as_str() {
27            "GET" => {
28                let params = query.cloned().unwrap_or_default();
29                self.http.get_with_query(&normalized_path, &params).await
30            }
31            "PUT" => self.http.put(&normalized_path, body.unwrap_or(&Value::Null)).await,
32            "PATCH" => self.http.patch(&normalized_path, body.unwrap_or(&Value::Null)).await,
33            "DELETE" => self.http.delete(&normalized_path).await,
34            _ => self.http.post(&normalized_path, body.unwrap_or(&Value::Null)).await,
35        }
36    }
37
38    pub async fn get(&self, path: &str, query: Option<&HashMap<String, String>>) -> Result<Value, Error> {
39        self.call(path, "GET", None, query).await
40    }
41
42    pub async fn post(&self, path: &str, body: Option<&Value>) -> Result<Value, Error> {
43        self.call(path, "POST", body, None).await
44    }
45
46    pub async fn put(&self, path: &str, body: Option<&Value>) -> Result<Value, Error> {
47        self.call(path, "PUT", body, None).await
48    }
49
50    pub async fn patch(&self, path: &str, body: Option<&Value>) -> Result<Value, Error> {
51        self.call(path, "PATCH", body, None).await
52    }
53
54    pub async fn delete(&self, path: &str) -> Result<Value, Error> {
55        self.call(path, "DELETE", None, None).await
56    }
57}