fundamentum_sdk_api/client/
devices_api.rs

1//! "Devices" section in [Fundamentum API](https://dimonoff.atlassian.net/wiki/spaces/F2/pages/306273353781/Devices+API).
2
3use std::marker::PhantomData;
4
5use super::{errors::SdkClientError, sdk_client::SdkClient};
6use crate::models::devices::{
7    AuthorizationConfigReq, GetDeviceRes, StoreDeviceReq, StoreDeviceRes,
8};
9
10/// Project identification.
11#[derive(Debug, Clone)]
12pub struct DevicesApiConfig {
13    /// Project id on the Hub.
14    pub project_id: u32,
15    /// Region id on the Hub.
16    pub region_id: u32,
17    /// Registry id on the Hub.
18    pub registry_id: u32,
19}
20
21/// "Devices" section of the devices API.
22pub struct DevicesApi<'a, V> {
23    client: &'a SdkClient,
24    base_path: String,
25
26    // We use PhantomData to make the compiler think that we are using the V type.
27    // Read more here: <https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns>
28    _marker: PhantomData<fn() -> V>,
29}
30
31impl<'a, V> DevicesApi<'a, V> {
32    /// Create a new interface for "Devices" API.
33    #[must_use]
34    pub fn new(client: &'a SdkClient, api_config: &'a DevicesApiConfig) -> Self {
35        Self {
36            client,
37            base_path: format!(
38                "/projects/{}/regions/{}/registries/{}/devices",
39                api_config.project_id, api_config.region_id, api_config.registry_id
40            ),
41            _marker: PhantomData,
42        }
43    }
44
45    /// Get the device configuration.
46    ///
47    /// # Errors
48    ///
49    /// There may be an error to get the HTTP Response, to interpret its Json
50    /// body into the [`Response`](crate::models::api_response::ApiResponse).
51    pub async fn get_device_configuration(
52        &self,
53        serial_number: impl Into<String> + Send + Clone,
54    ) -> Result<GetDeviceRes, SdkClientError> {
55        let path = format!("{}/{}", self.base_path, serial_number.into());
56        self.client.get(path).await
57    }
58
59    /// Delete a device.
60    ///
61    /// # Errors
62    ///
63    /// There may be an error to get the HTTP Response, to interpret its Json
64    /// body into the [`Response`](crate::models::api_response::ApiResponse).
65    pub async fn delete_device(
66        &self,
67        serial_number: impl Into<String> + Send + Clone,
68    ) -> Result<(), SdkClientError> {
69        let path = format!("{}/{}", self.base_path, serial_number.into());
70        self.client.delete(path).await
71    }
72
73    /// Store a new Device.
74    ///
75    /// # Errors
76    ///
77    /// There may be an error to get the HTTP Response, to interpret its Json
78    /// body into the [`Response`](crate::models::api_response::ApiResponse).
79    pub async fn store_device(
80        &self,
81        serial_number: impl Into<String> + Send + Clone,
82        name: Option<impl Into<String> + Send>,
83        asset_type_id: i32,
84    ) -> Result<StoreDeviceRes, SdkClientError> {
85        let body = StoreDeviceReq {
86            serial_number: serial_number.clone().into(),
87            name: name.map_or(serial_number.into(), Into::into),
88            asset_type_id,
89        };
90        self.client.post_body(&self.base_path, body).await
91    }
92
93    /// Configure Authorization for a Device.
94    ///
95    /// # Errors
96    ///
97    /// There may be an error to get the HTTP Response, to interpret its Json
98    /// body into the [`Response`](crate::models::api_response::ApiResponse).
99    pub async fn configure_authorization_for_device(
100        &self,
101        serial_number: impl Into<String> + Send + Clone,
102        authorization_type: impl Into<String> + Send + Clone,
103        public_key: impl Into<String> + Send + Clone,
104    ) -> Result<serde_json::Value, SdkClientError> {
105        let path = format!("{}/{}", self.base_path, serial_number.into());
106        let body = AuthorizationConfigReq {
107            authorization_type: authorization_type.into(),
108            public_key: public_key.into(),
109        };
110
111        self.client.post_body(path, body).await
112    }
113}