cognite/api/core/
assets.rs

1use serde::Serialize;
2use std::collections::HashSet;
3
4use crate::api::resource::*;
5use crate::dto::core::asset::*;
6use crate::error::Result;
7use crate::utils::lease::CleanResource;
8use crate::{IdentityList, ItemsVec, Patch};
9
10/// Assets represent objects or groups of objects from the physical world.
11/// Assets are organized in hierarchies. For example, a water pump asset can
12/// be part of a subsystem asset on an oil platform asset.
13pub type AssetsResource = Resource<Asset>;
14
15impl WithBasePath for AssetsResource {
16    const BASE_PATH: &'static str = "assets";
17}
18
19impl List<AssetQuery, Asset> for AssetsResource {}
20impl Create<AddAsset, Asset> for AssetsResource {}
21impl SearchItems<'_, AssetFilter, AssetSearch, Asset> for AssetsResource {}
22impl Update<Patch<PatchAsset>, Asset> for AssetsResource {}
23impl<R> DeleteWithRequest<DeleteAssetsRequest<IdentityList<R>>> for AssetsResource
24where
25    R: Send + Sync,
26    IdentityList<R>: Serialize,
27{
28}
29impl FilterWithRequest<FilterAssetsRequest, Asset> for AssetsResource {}
30impl<R> RetrieveWithRequest<RetrieveAssetsRequest<IdentityList<R>>, ItemsVec<Asset>>
31    for AssetsResource
32where
33    R: Send + Sync,
34    IdentityList<R>: Serialize,
35{
36}
37
38impl AssetsResource {
39    /// Retrieve a list of assets by their IDs.
40    ///
41    /// Will fail if `ignore_unknown_ids` is false and the assets are not present in CDF.
42    ///
43    /// # Arguments
44    ///
45    /// * `asset_ids` - List of IDs or external IDs to retrieve.
46    /// * `ignore_unknown_ids` - If `true`, missing assets will be ignored, instead of causing
47    ///   the request to fail.
48    /// * `aggregated_properties` - List of aggregated properties to include in response.
49    pub async fn retrieve<R>(
50        &self,
51        asset_ids: impl Into<IdentityList<R>>,
52        ignore_unknown_ids: bool,
53        aggregated_properties: Option<Vec<AssetAggregatedProperty>>,
54    ) -> Result<Vec<Asset>>
55    where
56        IdentityList<R>: Serialize,
57        R: Send + Sync,
58    {
59        let id_items = RetrieveAssetsRequest::new_with_extra_fields(
60            asset_ids.into(),
61            RetrieveAssetsRequestData {
62                ignore_unknown_ids,
63                aggregated_properties,
64            },
65        );
66        let r = RetrieveWithRequest::retrieve(self, &id_items).await?;
67        Ok(r.items)
68    }
69
70    /// Delete a list of assets by their IDs.
71    ///
72    /// Will fail if `ignore_unknown_ids` is false and the assets are not present in CDF.
73    ///
74    /// # Arguments
75    /// * `asset_ids` - List of IDs or external IDs to delete.
76    /// * `ignore_unknown_ids` - If `true`, missing assets will be ignored, instead of causing
77    ///   the request to fail.
78    /// * `recursive` - If `true`, recursively delete any children of the deleted assets.
79    pub async fn delete<R>(
80        &self,
81        asset_ids: impl Into<IdentityList<R>>,
82        ignore_unknown_ids: bool,
83        recursive: bool,
84    ) -> Result<()>
85    where
86        IdentityList<R>: Serialize,
87        R: Send + Sync,
88    {
89        let id_items = DeleteAssetsRequest::new_with_extra_fields(
90            asset_ids.into(),
91            DeleteAssetsRequestData {
92                ignore_unknown_ids,
93                recursive,
94            },
95        );
96        DeleteWithRequest::delete(self, &id_items).await
97    }
98
99    /// Compute aggregates over assets, such as getting the count of all assets in a project,
100    /// checking different names and descriptions of assets in your project, etc.
101    ///
102    /// # Arguments
103    ///
104    /// * `aggregate` - Aggregate to compute
105    ///
106    /// The returned aggregates depend on which aggregates were requested.
107    pub async fn aggregate(
108        &self,
109        aggregate: AssetAggregateRequest,
110    ) -> Result<Vec<AssetAggregateResponse>> {
111        let resp: ItemsVec<AssetAggregateResponse> =
112            self.api_client.post("assets/aggregate", &aggregate).await?;
113        Ok(resp.items)
114    }
115}
116
117impl CleanResource<Asset> for AssetsResource {
118    async fn clean_resource(&self, resources: Vec<Asset>) -> std::result::Result<(), crate::Error> {
119        let ids = resources.iter().map(|a| a.id).collect::<HashSet<i64>>();
120        self.delete(&ids.into_iter().collect::<Vec<_>>(), true, true)
121            .await
122    }
123}