fundamentum_sdk_api/client/
devices_api.rs1use std::marker::PhantomData;
4
5use super::{errors::SdkClientError, sdk_client::SdkClient};
6use crate::models::devices::{
7 AuthorizationConfigReq, GetDeviceRes, StoreDeviceReq, StoreDeviceRes,
8};
9
10#[derive(Debug, Clone)]
12pub struct DevicesApiConfig {
13 pub project_id: u32,
15 pub region_id: u32,
17 pub registry_id: u32,
19}
20
21pub struct DevicesApi<'a, V> {
23 client: &'a SdkClient,
24 base_path: String,
25
26 _marker: PhantomData<fn() -> V>,
29}
30
31impl<'a, V> DevicesApi<'a, V> {
32 #[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 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 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 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 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}