Skip to main content

edc_connector_client/api/
celexpressions.rs

1use crate::client::EdcConnectorClientInternal;
2use crate::types::common_expression_language::{
3    CommonExpressionLanguage, NewCommonExpressionLanguage,
4};
5use crate::types::context::WithContext;
6use crate::types::query::Query;
7use crate::types::response::IdResponse;
8use crate::{EdcConnectorApiVersion, EdcResult};
9
10const CEL_EXPRESSIONS_PATH: &str = "celexpressions";
11
12pub struct CommonExpressionLanguageApi<'a> {
13    client: &'a EdcConnectorClientInternal,
14    version: EdcConnectorApiVersion,
15}
16
17impl<'a> CommonExpressionLanguageApi<'a> {
18    pub(crate) fn new(
19        client: &'a EdcConnectorClientInternal,
20        version: EdcConnectorApiVersion,
21    ) -> CommonExpressionLanguageApi<'a> {
22        CommonExpressionLanguageApi { client, version }
23    }
24
25    pub async fn create(
26        &self,
27        common_expression_language: &NewCommonExpressionLanguage,
28    ) -> EdcResult<IdResponse<String>> {
29        let url = self.client.path_for(self.version, &[CEL_EXPRESSIONS_PATH]);
30        self.client
31            .post::<_, WithContext<IdResponse<String>>>(
32                url,
33                &self
34                    .client
35                    .context_for(self.version, common_expression_language),
36            )
37            .await
38            .map(|ctx| ctx.inner)
39    }
40
41    pub async fn get(&self, id: &str) -> EdcResult<CommonExpressionLanguage> {
42        let url = self
43            .client
44            .path_for(self.version, &[CEL_EXPRESSIONS_PATH, id]);
45        self.client
46            .get::<WithContext<CommonExpressionLanguage>>(url)
47            .await
48            .map(|ctx| ctx.inner)
49    }
50
51    pub async fn update(
52        &self,
53        common_expression_language: &CommonExpressionLanguage,
54    ) -> EdcResult<()> {
55        let url = self.client.path_for(self.version, &[CEL_EXPRESSIONS_PATH]);
56        self.client
57            .put(
58                url,
59                &self
60                    .client
61                    .context_for(self.version, common_expression_language),
62            )
63            .await
64    }
65
66    pub async fn query(&self, query: Query) -> EdcResult<Vec<CommonExpressionLanguage>> {
67        let url = self
68            .client
69            .path_for(self.version, &[CEL_EXPRESSIONS_PATH, "request"]);
70        self.client
71            .post::<_, Vec<WithContext<CommonExpressionLanguage>>>(
72                url,
73                &self.client.context_for(self.version, &query),
74            )
75            .await
76            .map(|results| results.into_iter().map(|ctx| ctx.inner).collect())
77    }
78
79    pub async fn delete(&self, id: &str) -> EdcResult<()> {
80        let url = self
81            .client
82            .path_for(self.version, &[CEL_EXPRESSIONS_PATH, id]);
83        self.client.del(url).await
84    }
85}