edc_connector_client/api/
assets.rs

1use crate::{
2    client::EdcConnectorClientInternal,
3    types::{
4        asset::{Asset, NewAsset},
5        context::{WithContext, WithContextRef},
6        query::Query,
7        response::IdResponse,
8    },
9    EdcResult,
10};
11
12pub struct AssetApi<'a>(&'a EdcConnectorClientInternal);
13
14impl<'a> AssetApi<'a> {
15    pub(crate) fn new(client: &'a EdcConnectorClientInternal) -> AssetApi<'a> {
16        AssetApi(client)
17    }
18
19    pub async fn create(&self, asset: &NewAsset) -> EdcResult<IdResponse<String>> {
20        let url = self.get_endpoint(&[]);
21        self.0
22            .post::<_, WithContext<IdResponse<String>>>(
23                url,
24                &WithContextRef::default_context(asset),
25            )
26            .await
27            .map(|ctx| ctx.inner)
28    }
29
30    pub async fn get(&self, id: &str) -> EdcResult<Asset> {
31        let url = self.get_endpoint(&[id]);
32        self.0
33            .get::<WithContext<Asset>>(url)
34            .await
35            .map(|ctx| ctx.inner)
36    }
37
38    pub async fn update(&self, asset: &Asset) -> EdcResult<()> {
39        let url = self.get_endpoint(&[]);
40        self.0
41            .put(url, &WithContextRef::default_context(asset))
42            .await
43    }
44
45    pub async fn query(&self, query: Query) -> EdcResult<Vec<Asset>> {
46        let url = self.get_endpoint(&["request"]);
47        self.0
48            .post::<_, Vec<WithContext<Asset>>>(url, &WithContextRef::default_context(&query))
49            .await
50            .map(|results| results.into_iter().map(|ctx| ctx.inner).collect())
51    }
52
53    pub async fn delete(&self, id: &str) -> EdcResult<()> {
54        let url = self.get_endpoint(&[id]);
55        self.0.del(url).await
56    }
57
58    fn get_endpoint(&self, paths: &[&str]) -> String {
59        [self.0.management_url.as_str(), "v3", "assets"]
60            .into_iter()
61            .chain(paths.iter().copied())
62            .collect::<Vec<_>>()
63            .join("/")
64    }
65}