Skip to main content

openapp_sdk_core/resources/
entities.rs

1//! `Entities` resource group.
2
3use std::sync::Arc;
4
5use reqwest::Method;
6
7use super::JsonValue;
8use crate::{
9    error::SdkError,
10    transport::{RequestSpec, Transport},
11};
12
13#[derive(Debug, Clone)]
14pub struct EntitiesClient {
15    transport: Arc<Transport>,
16}
17
18impl EntitiesClient {
19    pub(crate) fn new(transport: Arc<Transport>) -> Self {
20        Self { transport }
21    }
22
23    pub async fn list(&self) -> Result<Vec<JsonValue>, SdkError> {
24        self.transport
25            .request_json::<(), Vec<JsonValue>>(RequestSpec {
26                method: Method::GET,
27                path: "/entities",
28                ..Default::default()
29            })
30            .await
31    }
32
33    pub async fn create(&self, body: &JsonValue) -> Result<JsonValue, SdkError> {
34        self.transport
35            .request_json::<JsonValue, JsonValue>(RequestSpec {
36                method: Method::POST,
37                path: "/entities",
38                body: Some(body),
39                ..Default::default()
40            })
41            .await
42    }
43
44    pub async fn get(&self, id: &str) -> Result<JsonValue, SdkError> {
45        let path = format!("/entities/{id}");
46        self.transport
47            .request_json::<(), JsonValue>(RequestSpec {
48                method: Method::GET,
49                path: &path,
50                ..Default::default()
51            })
52            .await
53    }
54
55    pub async fn update(&self, id: &str, body: &JsonValue) -> Result<JsonValue, SdkError> {
56        let path = format!("/entities/{id}");
57        self.transport
58            .request_json::<JsonValue, JsonValue>(RequestSpec {
59                method: Method::PUT,
60                path: &path,
61                body: Some(body),
62                ..Default::default()
63            })
64            .await
65    }
66
67    pub async fn patch(&self, id: &str, body: &JsonValue) -> Result<JsonValue, SdkError> {
68        let path = format!("/entities/{id}");
69        self.transport
70            .request_json::<JsonValue, JsonValue>(RequestSpec {
71                method: Method::PATCH,
72                path: &path,
73                body: Some(body),
74                ..Default::default()
75            })
76            .await
77    }
78
79    pub async fn delete(&self, id: &str) -> Result<(), SdkError> {
80        let path = format!("/entities/{id}");
81        self.transport
82            .request_json::<(), ()>(RequestSpec {
83                method: Method::DELETE,
84                path: &path,
85                ..Default::default()
86            })
87            .await
88    }
89
90    pub async fn purge(&self, id: &str) -> Result<(), SdkError> {
91        let path = format!("/entities/{id}/purge");
92        self.transport
93            .request_json::<(), ()>(RequestSpec {
94                method: Method::DELETE,
95                path: &path,
96                ..Default::default()
97            })
98            .await
99    }
100
101    pub async fn restore(&self, id: &str) -> Result<JsonValue, SdkError> {
102        let path = format!("/entities/{id}/restore");
103        self.transport
104            .request_json::<(), JsonValue>(RequestSpec {
105                method: Method::POST,
106                path: &path,
107                ..Default::default()
108            })
109            .await
110    }
111
112    /// `POST /entities/{id}/actions/{action_id}` — invoke a DSL/entity action.
113    pub async fn invoke_action(
114        &self,
115        id: &str,
116        action_id: &str,
117        body: &JsonValue,
118    ) -> Result<JsonValue, SdkError> {
119        let path = format!("/entities/{id}/actions/{action_id}");
120        self.transport
121            .request_json::<JsonValue, JsonValue>(RequestSpec {
122                method: Method::POST,
123                path: &path,
124                body: Some(body),
125                ..Default::default()
126            })
127            .await
128    }
129
130    /// `POST /entities/{id}/actions/{action_id}/preview` — Cedar dry-run, no hardware call.
131    pub async fn preview_action(
132        &self,
133        id: &str,
134        action_id: &str,
135        body: &JsonValue,
136    ) -> Result<JsonValue, SdkError> {
137        let path = format!("/entities/{id}/actions/{action_id}/preview");
138        self.transport
139            .request_json::<JsonValue, JsonValue>(RequestSpec {
140                method: Method::POST,
141                path: &path,
142                body: Some(body),
143                ..Default::default()
144            })
145            .await
146    }
147
148    /// `POST /entities/{id}/actions/{action_id}` with optional idempotency / confirm headers.
149    pub async fn invoke_action_with_headers(
150        &self,
151        id: &str,
152        action_id: &str,
153        body: &JsonValue,
154        extra_headers: &[(&str, String)],
155    ) -> Result<JsonValue, SdkError> {
156        let path = format!("/entities/{id}/actions/{action_id}");
157        self.transport
158            .request_json::<JsonValue, JsonValue>(RequestSpec {
159                method: Method::POST,
160                path: &path,
161                body: Some(body),
162                extra_headers,
163                ..Default::default()
164            })
165            .await
166    }
167
168    pub async fn metadata_definition(&self, id: &str) -> Result<JsonValue, SdkError> {
169        let path = format!("/entities/{id}/metadata-definition");
170        self.transport
171            .request_json::<(), JsonValue>(RequestSpec {
172                method: Method::GET,
173                path: &path,
174                ..Default::default()
175            })
176            .await
177    }
178
179    pub async fn by_device(&self, device_id: &str) -> Result<Vec<JsonValue>, SdkError> {
180        let path = format!("/devices/{device_id}/entities");
181        self.transport
182            .request_json::<(), Vec<JsonValue>>(RequestSpec {
183                method: Method::GET,
184                path: &path,
185                ..Default::default()
186            })
187            .await
188    }
189
190    pub async fn device_entities_metadata_definition(
191        &self,
192        device_id: &str,
193    ) -> Result<JsonValue, SdkError> {
194        let path = format!("/devices/{device_id}/entities/metadata-definition");
195        self.transport
196            .request_json::<(), JsonValue>(RequestSpec {
197                method: Method::GET,
198                path: &path,
199                ..Default::default()
200            })
201            .await
202    }
203
204    pub async fn directory_floors(&self, device_id: &str) -> Result<JsonValue, SdkError> {
205        let path = format!("/devices/{device_id}/directory-floors");
206        self.transport
207            .request_json::<(), JsonValue>(RequestSpec {
208                method: Method::GET,
209                path: &path,
210                ..Default::default()
211            })
212            .await
213    }
214}