openapp_sdk_core/resources/
entities.rs1use 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 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 pub async fn metadata_definition(&self, id: &str) -> Result<JsonValue, SdkError> {
131 let path = format!("/entities/{id}/metadata-definition");
132 self.transport
133 .request_json::<(), JsonValue>(RequestSpec {
134 method: Method::GET,
135 path: &path,
136 ..Default::default()
137 })
138 .await
139 }
140
141 pub async fn by_device(&self, device_id: &str) -> Result<Vec<JsonValue>, SdkError> {
142 let path = format!("/devices/{device_id}/entities");
143 self.transport
144 .request_json::<(), Vec<JsonValue>>(RequestSpec {
145 method: Method::GET,
146 path: &path,
147 ..Default::default()
148 })
149 .await
150 }
151
152 pub async fn device_entities_metadata_definition(
153 &self,
154 device_id: &str,
155 ) -> Result<JsonValue, SdkError> {
156 let path = format!("/devices/{device_id}/entities/metadata-definition");
157 self.transport
158 .request_json::<(), JsonValue>(RequestSpec {
159 method: Method::GET,
160 path: &path,
161 ..Default::default()
162 })
163 .await
164 }
165
166 pub async fn apartment_floors(&self, device_id: &str) -> Result<JsonValue, SdkError> {
167 let path = format!("/devices/{device_id}/apartment-floors");
168 self.transport
169 .request_json::<(), JsonValue>(RequestSpec {
170 method: Method::GET,
171 path: &path,
172 ..Default::default()
173 })
174 .await
175 }
176}