Skip to main content

edc_web_components/models/
asset_item.rs

1use crate::models::{Creator, Thumbnail};
2use edc_connector_client::types::asset::Asset;
3use edc_connector_client::types::data_address::DataAddress;
4use edc_connector_client::types::properties::Properties;
5
6#[derive(Clone, Debug, PartialEq)]
7pub struct AssetItem {
8  pub id: String,
9  pub name: String,
10  pub version: Option<semver::Version>,
11  pub description: Option<String>,
12  pub creator: Option<Creator>,
13  pub thumbnail: Option<Thumbnail>,
14  pub keywords: Vec<String>,
15  pub base_url: String,
16  pub proxy_path: bool,
17  pub proxy_query_params: bool,
18  pub proxy_method: bool,
19  pub proxy_body: bool,
20}
21
22impl From<Asset> for AssetItem {
23  fn from(asset: Asset) -> Self {
24    let id = asset.id().to_string();
25    let name = get_property(asset.properties(), "name");
26    let description = asset
27      .properties()
28      .get::<String>("http://www.w3.org/2000/01/rdf-schema#comment")
29      .unwrap_or_default();
30
31    let version = asset
32      .properties()
33      .get::<String>("http://www.w3.org/ns/dcat#version")
34      .unwrap_or_default()
35      .and_then(|version| semver::Version::parse(version.as_str()).ok());
36
37    let creator = asset
38      .properties()
39      .get_raw("http://purl.org/dc/terms/creator")
40      .and_then(|property_value| serde_json::from_value::<Creator>(property_value.0.clone()).ok());
41
42    let thumbnail = asset
43      .properties()
44      .get_raw("http://xmlns.com/foaf/0.1/thumbnail")
45      .and_then(|property_value| {
46        serde_json::from_value::<Thumbnail>(property_value.0.clone()).ok()
47      });
48
49    let keywords = asset
50      .properties()
51      .get_raw("http://www.w3.org/ns/dcat#keyword")
52      .and_then(|property_value| {
53        serde_json::from_value::<Vec<String>>(property_value.0.clone()).ok()
54      })
55      .unwrap_or_default();
56
57    let base_url = asset
58      .data_address()
59      .property("baseUrl")
60      .unwrap_or_default()
61      .unwrap_or_default();
62
63    let proxy_path = get_boolean_property(asset.data_address(), "proxyPath");
64    let proxy_query_params = get_boolean_property(asset.data_address(), "proxyQueryParams");
65    let proxy_method = get_boolean_property(asset.data_address(), "proxyMethod");
66    let proxy_body = get_boolean_property(asset.data_address(), "proxyBody");
67
68    AssetItem {
69      id,
70      name,
71      version,
72      description,
73      creator,
74      thumbnail,
75      keywords,
76      base_url,
77      proxy_path,
78      proxy_query_params,
79      proxy_method,
80      proxy_body,
81    }
82  }
83}
84
85fn get_property(properties: &Properties, name: &str) -> String {
86  properties
87    .get::<String>(name)
88    .unwrap_or_default()
89    .unwrap_or_default()
90}
91
92fn get_boolean_property(data_address: &DataAddress, name: &str) -> bool {
93  data_address
94    .property::<String>(name)
95    .unwrap_or_default()
96    .unwrap_or_default()
97    == "true"
98}