1use crate::{
2 core::{
3 api_req::ApiRequest, api_resp::ApiResponseTrait, config::Config,
4 constants::AccessTokenType, endpoints::EndpointBuilder, http::Transport,
5 },
6 service::contact::models::*,
7};
8use serde::{Deserialize, Serialize};
9
10pub struct UnitService {
12 config: Config,
13}
14
15impl UnitService {
16 pub fn new(config: Config) -> Self {
17 Self { config }
18 }
19
20 pub async fn create(
22 &self,
23 req: &CreateUnitRequest,
24 ) -> crate::core::SDKResult<CreateUnitResponse> {
25 let api_req = ApiRequest {
26 http_method: reqwest::Method::POST,
27 api_path: crate::core::endpoints::contact::CONTACT_V3_UNITS.to_string(),
28 supported_access_token_types: vec![AccessTokenType::Tenant],
29 body: serde_json::to_vec(req)?,
30 ..Default::default()
31 };
32
33 let resp = Transport::<CreateUnitResponse>::request(api_req, &self.config, None).await?;
34 Ok(resp.data.unwrap_or_default())
35 }
36
37 pub async fn patch(
39 &self,
40 unit_id: &str,
41 req: &PatchUnitRequest,
42 ) -> crate::core::SDKResult<PatchUnitResponse> {
43 let api_req = ApiRequest {
44 http_method: reqwest::Method::PATCH,
45 api_path: EndpointBuilder::replace_param(
46 crate::core::endpoints::contact::CONTACT_V3_UNIT_GET,
47 "unit_id",
48 unit_id,
49 ),
50 supported_access_token_types: vec![AccessTokenType::Tenant],
51 body: serde_json::to_vec(req)?,
52 ..Default::default()
53 };
54
55 let resp = Transport::<PatchUnitResponse>::request(api_req, &self.config, None).await?;
56 Ok(resp.data.unwrap_or_default())
57 }
58
59 pub async fn bind_department(
61 &self,
62 unit_id: &str,
63 req: &BindDepartmentRequest,
64 ) -> crate::core::SDKResult<BindDepartmentResponse> {
65 let api_req = ApiRequest {
66 http_method: reqwest::Method::POST,
67 api_path: EndpointBuilder::replace_param(
68 crate::core::endpoints::contact::CONTACT_V3_UNIT_BIND_DEPARTMENT,
69 "unit_id",
70 unit_id,
71 ),
72 supported_access_token_types: vec![AccessTokenType::Tenant],
73 body: serde_json::to_vec(req)?,
74 ..Default::default()
75 };
76
77 let resp =
78 Transport::<BindDepartmentResponse>::request(api_req, &self.config, None).await?;
79 Ok(resp.data.unwrap_or_default())
80 }
81
82 pub async fn unbind_department(
84 &self,
85 unit_id: &str,
86 req: &UnbindDepartmentRequest,
87 ) -> crate::core::SDKResult<UnbindDepartmentResponse> {
88 let api_req = ApiRequest {
89 http_method: reqwest::Method::POST,
90 api_path: EndpointBuilder::replace_param(
91 crate::core::endpoints::contact::CONTACT_V3_UNIT_UNBIND_DEPARTMENT,
92 "unit_id",
93 unit_id,
94 ),
95 supported_access_token_types: vec![AccessTokenType::Tenant],
96 body: serde_json::to_vec(req)?,
97 ..Default::default()
98 };
99
100 let resp =
101 Transport::<UnbindDepartmentResponse>::request(api_req, &self.config, None).await?;
102 Ok(resp.data.unwrap_or_default())
103 }
104
105 pub async fn list_department(
107 &self,
108 unit_id: &str,
109 _req: &ListUnitDepartmentsRequest,
110 ) -> crate::core::SDKResult<ListUnitDepartmentsResponse> {
111 let api_req = ApiRequest {
112 http_method: reqwest::Method::GET,
113 api_path: EndpointBuilder::replace_param(
114 crate::core::endpoints::contact::CONTACT_V3_UNIT_LIST_DEPARTMENT,
115 "unit_id",
116 unit_id,
117 ),
118 supported_access_token_types: vec![AccessTokenType::Tenant],
119 body: Vec::new(),
120 query_params: std::collections::HashMap::new(),
121 ..Default::default()
122 };
123
124 let resp =
125 Transport::<ListUnitDepartmentsResponse>::request(api_req, &self.config, None).await?;
126 Ok(resp.data.unwrap_or_default())
127 }
128
129 pub async fn get(&self, unit_id: &str) -> crate::core::SDKResult<GetUnitResponse> {
131 let api_req = ApiRequest {
132 http_method: reqwest::Method::GET,
133 api_path: EndpointBuilder::replace_param(
134 crate::core::endpoints::contact::CONTACT_V3_UNIT_GET,
135 "unit_id",
136 unit_id,
137 ),
138 supported_access_token_types: vec![AccessTokenType::Tenant],
139 body: Vec::new(),
140 ..Default::default()
141 };
142
143 let resp = Transport::<GetUnitResponse>::request(api_req, &self.config, None).await?;
144 Ok(resp.data.unwrap_or_default())
145 }
146
147 pub async fn list(&self, _req: &ListUnitsRequest) -> crate::core::SDKResult<ListUnitsResponse> {
149 let api_req = ApiRequest {
150 http_method: reqwest::Method::GET,
151 api_path: crate::core::endpoints::contact::CONTACT_V3_UNITS.to_string(),
152 supported_access_token_types: vec![AccessTokenType::Tenant],
153 body: Vec::new(),
154 query_params: std::collections::HashMap::new(),
155 ..Default::default()
156 };
157
158 let resp = Transport::<ListUnitsResponse>::request(api_req, &self.config, None).await?;
159 Ok(resp.data.unwrap_or_default())
160 }
161
162 pub async fn delete(&self, unit_id: &str) -> crate::core::SDKResult<DeleteUnitResponse> {
164 let api_req = ApiRequest {
165 http_method: reqwest::Method::DELETE,
166 api_path: EndpointBuilder::replace_param(
167 crate::core::endpoints::contact::CONTACT_V3_UNIT_GET,
168 "unit_id",
169 unit_id,
170 ),
171 supported_access_token_types: vec![AccessTokenType::Tenant],
172 body: Vec::new(),
173 ..Default::default()
174 };
175
176 let resp = Transport::<DeleteUnitResponse>::request(api_req, &self.config, None).await?;
177 Ok(resp.data.unwrap_or_default())
178 }
179}
180
181#[derive(Debug, Clone, Serialize, Deserialize)]
182pub struct CreateUnitRequest {
183 pub unit: Unit,
184}
185
186#[derive(Debug, Clone, Serialize, Deserialize, Default)]
187pub struct CreateUnitResponse {
188 pub unit: Unit,
189}
190
191impl ApiResponseTrait for CreateUnitResponse {
192 fn data_format() -> crate::core::api_resp::ResponseFormat {
193 crate::core::api_resp::ResponseFormat::Data
194 }
195}
196
197#[derive(Debug, Clone, Serialize, Deserialize)]
198pub struct PatchUnitRequest {
199 pub unit: Unit,
200}
201
202#[derive(Debug, Clone, Serialize, Deserialize, Default)]
203pub struct PatchUnitResponse {
204 pub unit: Unit,
205}
206
207impl ApiResponseTrait for PatchUnitResponse {
208 fn data_format() -> crate::core::api_resp::ResponseFormat {
209 crate::core::api_resp::ResponseFormat::Data
210 }
211}
212
213#[derive(Debug, Clone, Serialize, Deserialize)]
214pub struct BindDepartmentRequest {
215 pub department_id: String,
216 #[serde(skip_serializing_if = "Option::is_none")]
217 pub department_id_type: Option<String>,
218}
219
220#[derive(Debug, Clone, Serialize, Deserialize, Default)]
221pub struct BindDepartmentResponse {}
222
223impl ApiResponseTrait for BindDepartmentResponse {
224 fn data_format() -> crate::core::api_resp::ResponseFormat {
225 crate::core::api_resp::ResponseFormat::Data
226 }
227}
228
229#[derive(Debug, Clone, Serialize, Deserialize)]
230pub struct UnbindDepartmentRequest {
231 pub department_id: String,
232 #[serde(skip_serializing_if = "Option::is_none")]
233 pub department_id_type: Option<String>,
234}
235
236#[derive(Debug, Clone, Serialize, Deserialize, Default)]
237pub struct UnbindDepartmentResponse {}
238
239impl ApiResponseTrait for UnbindDepartmentResponse {
240 fn data_format() -> crate::core::api_resp::ResponseFormat {
241 crate::core::api_resp::ResponseFormat::Data
242 }
243}
244
245#[derive(Debug, Clone, Default, Serialize, Deserialize)]
246pub struct ListUnitDepartmentsRequest {
247 #[serde(skip_serializing_if = "Option::is_none")]
248 pub department_id_type: Option<String>,
249 #[serde(skip_serializing_if = "Option::is_none")]
250 pub page_size: Option<i32>,
251 #[serde(skip_serializing_if = "Option::is_none")]
252 pub page_token: Option<String>,
253}
254
255#[derive(Debug, Clone, Serialize, Deserialize, Default)]
256pub struct ListUnitDepartmentsResponse {
257 pub department_list: Vec<Department>,
258 #[serde(skip_serializing_if = "Option::is_none")]
259 pub has_more: Option<bool>,
260 #[serde(skip_serializing_if = "Option::is_none")]
261 pub page_token: Option<String>,
262}
263
264impl ApiResponseTrait for ListUnitDepartmentsResponse {
265 fn data_format() -> crate::core::api_resp::ResponseFormat {
266 crate::core::api_resp::ResponseFormat::Data
267 }
268}
269
270#[derive(Debug, Clone, Serialize, Deserialize, Default)]
271pub struct GetUnitResponse {
272 pub unit: Unit,
273}
274
275impl ApiResponseTrait for GetUnitResponse {
276 fn data_format() -> crate::core::api_resp::ResponseFormat {
277 crate::core::api_resp::ResponseFormat::Data
278 }
279}
280
281#[derive(Debug, Clone, Default, Serialize, Deserialize)]
282pub struct ListUnitsRequest {
283 #[serde(skip_serializing_if = "Option::is_none")]
284 pub page_size: Option<i32>,
285 #[serde(skip_serializing_if = "Option::is_none")]
286 pub page_token: Option<String>,
287}
288
289#[derive(Debug, Clone, Serialize, Deserialize, Default)]
290pub struct ListUnitsResponse {
291 pub items: Vec<Unit>,
292 #[serde(skip_serializing_if = "Option::is_none")]
293 pub has_more: Option<bool>,
294 #[serde(skip_serializing_if = "Option::is_none")]
295 pub page_token: Option<String>,
296}
297
298impl ApiResponseTrait for ListUnitsResponse {
299 fn data_format() -> crate::core::api_resp::ResponseFormat {
300 crate::core::api_resp::ResponseFormat::Data
301 }
302}
303
304#[derive(Debug, Clone, Serialize, Deserialize, Default)]
305pub struct DeleteUnitResponse {}
306
307impl ApiResponseTrait for DeleteUnitResponse {
308 fn data_format() -> crate::core::api_resp::ResponseFormat {
309 crate::core::api_resp::ResponseFormat::Data
310 }
311}