fundamentum_sdk_api/client/
shelf_devices_api.rs

1//! "Shelf 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::shelf_devices::{
7    DeleteShelfDeviceResponse, DeprovisionShelfDeviceResponse, GetShelfDeviceResponse,
8    ProvisionShelfDeviceRequest, ProvisionShelfDeviceResponse, StoreShelfDeviceRequest,
9    StoreShelfDeviceResponse,
10};
11
12/// Project identification.
13#[derive(Debug, Clone)]
14pub struct ShelfDevicesApiConfig {
15    /// Project id on the Hub.
16    pub project_id: u32,
17    /// Region id on the Hub.
18    pub region_id: u32,
19    /// Asset type of the device.
20    pub asset_type_id: i32,
21}
22
23/// "Shelf Devices" section of the devices API.
24pub struct ShelfDevicesApi<'a, V> {
25    client: &'a SdkClient,
26    base_path: String,
27
28    // We use PhantomData to make the compiler think that we are using the V type.
29    // Read more here: <https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns>
30    _marker: PhantomData<fn() -> V>,
31}
32
33impl<'a, V> ShelfDevicesApi<'a, V> {
34    /// Create a new interface for "Shelf Devices" API.
35    #[must_use]
36    pub fn new(client: &'a SdkClient, config: &ShelfDevicesApiConfig) -> Self {
37        Self {
38            client,
39            base_path: format!(
40                "/projects/{}/regions/{}/asset-types/{}/shelf-devices",
41                config.project_id, config.region_id, config.asset_type_id
42            ),
43            _marker: PhantomData,
44        }
45    }
46
47    /// Get a Shelf Device's information.
48    ///
49    /// # Errors
50    ///
51    /// There may be an error to get the HTTP Response, to interpret its Json
52    /// body into the [`Response`](crate::models::shelf_devices::GetShelfDeviceResponse).
53    pub async fn get(
54        &self,
55        serial_number: impl Into<String> + Send + Clone,
56    ) -> Result<GetShelfDeviceResponse, SdkClientError> {
57        let path = format!("{}/{}", self.base_path, serial_number.into());
58        self.client.get(path).await
59    }
60
61    /// Get a Shelf Device's registry ID.
62    ///
63    /// After a device has been registered as shelf, it must periodically
64    /// poll this route to check if it has been provisioned to a registry.
65    ///
66    /// This route requires a JWT generated from the private key.
67    ///
68    /// # Errors
69    ///
70    /// There may be an error to get the HTTP Response, to interpret its Json
71    /// body into the [`Response`](crate::models::shelf_devices::GetShelfDeviceResponse).
72    pub async fn get_registry_id(
73        &self,
74        serial_number: impl Into<String> + Send + Clone,
75    ) -> Result<GetShelfDeviceResponse, SdkClientError> {
76        let path = format!("{}/{}/registry-id", self.base_path, serial_number.into());
77        self.client.get(path).await
78    }
79
80    /// Store a new Shelf Device.
81    ///
82    /// # Errors
83    ///
84    /// There may be an error to get the HTTP Response, to interpret its Json
85    /// body into the [`Response`](crate::models::shelf_devices::StoreShelfDeviceResponse).
86    pub async fn store(
87        &self,
88        serial_number: impl Into<String> + Send + Clone,
89        secret: impl Into<String> + Send + Clone,
90    ) -> Result<StoreShelfDeviceResponse, SdkClientError> {
91        let body = StoreShelfDeviceRequest {
92            serial_number: serial_number.into(),
93            secret: secret.into(),
94        };
95        self.client.post_body(&self.base_path, body).await
96    }
97
98    /// Provision an existing Shelf Device to a registry.
99    ///
100    /// # Errors
101    ///
102    /// There may be an error to get the HTTP Response, to interpret its Json
103    /// body into the [`Response`](crate::models::shelf_devices::ProvisionShelfDeviceResponse).
104    pub async fn provision(
105        &self,
106        serial_number: impl Into<String> + Send + Clone,
107        registry_id: u32,
108    ) -> Result<ProvisionShelfDeviceResponse, SdkClientError> {
109        let path = format!("{}/{}/provision", self.base_path, serial_number.into());
110        let body = ProvisionShelfDeviceRequest { registry_id };
111        self.client.post_body(path, body).await
112    }
113
114    /// Deprovision a provisioned Shelf Device.
115    ///
116    /// # Errors
117    ///
118    /// There may be an error to get the HTTP Response, to interpret its Json
119    /// body into the [`Response`](crate::models::shelf_devices::DeprovisionShelfDeviceResponse).
120    pub async fn deprovision(
121        &self,
122        serial_number: impl Into<String> + Send + Clone,
123    ) -> Result<DeprovisionShelfDeviceResponse, SdkClientError> {
124        let path = format!("{}/{}/provision", self.base_path, serial_number.into());
125        self.client.delete(path).await
126    }
127
128    /// Delete a Shelf Device.
129    ///
130    /// # Errors
131    ///
132    /// There may be an error to get the HTTP Response, to interpret its Json
133    /// body into the [`Response`](crate::models::shelf_devices::DeleteShelfDeviceResponse).
134    pub async fn delete(
135        &self,
136        serial_number: impl Into<String> + Send + Clone,
137    ) -> Result<DeleteShelfDeviceResponse, SdkClientError> {
138        let path = format!("{}/{}", self.base_path, serial_number.into());
139        self.client.delete(path).await
140    }
141}