onspring/endpoints/
records.rs1use reqwest::Method;
2
3use crate::client::OnspringClient;
4use crate::error::Result;
5use crate::models::{
6 BatchDeleteRecordsRequest, BatchGetRecordsRequest, CollectionResponse, DataFormat, PagedResponse,
7 PagingRequest, QueryRecordsRequest, Record, SaveRecordRequest, SaveRecordResponse,
8};
9
10impl OnspringClient {
11 pub async fn list_records(
13 &self,
14 app_id: i32,
15 paging: Option<PagingRequest>,
16 field_ids: Option<&[i32]>,
17 data_format: Option<DataFormat>,
18 ) -> Result<PagedResponse<Record>> {
19 let path = format!("/Records/appId/{}", app_id);
20 let mut query = Vec::new();
21 if let Some(p) = paging {
22 query.push(("PageNumber", p.page_number.to_string()));
23 query.push(("PageSize", p.page_size.to_string()));
24 }
25 if let Some(ids) = field_ids {
26 let ids_str = ids
27 .iter()
28 .map(|id| id.to_string())
29 .collect::<Vec<_>>()
30 .join(",");
31 query.push(("fieldIds", ids_str));
32 }
33 if let Some(fmt) = data_format {
34 query.push(("dataFormat", format!("{:?}", fmt)));
35 }
36 let query_refs: Vec<(&str, String)> = query.iter().map(|(k, v)| (*k, v.clone())).collect();
37 self
38 .request(Method::GET, &path, &query_refs, Option::<&()>::None)
39 .await
40 }
41
42 pub async fn get_record(
44 &self,
45 app_id: i32,
46 record_id: i32,
47 field_ids: Option<&[i32]>,
48 data_format: Option<DataFormat>,
49 ) -> Result<Record> {
50 let path = format!("/Records/appId/{}/recordId/{}", app_id, record_id);
51 let mut query = Vec::new();
52 if let Some(ids) = field_ids {
53 let ids_str = ids
54 .iter()
55 .map(|id| id.to_string())
56 .collect::<Vec<_>>()
57 .join(",");
58 query.push(("fieldIds", ids_str));
59 }
60 if let Some(fmt) = data_format {
61 query.push(("dataFormat", format!("{:?}", fmt)));
62 }
63 let query_refs: Vec<(&str, String)> = query.iter().map(|(k, v)| (*k, v.clone())).collect();
64 self
65 .request(Method::GET, &path, &query_refs, Option::<&()>::None)
66 .await
67 }
68
69 pub async fn save_record(&self, request: SaveRecordRequest) -> Result<SaveRecordResponse> {
71 self
72 .request(Method::PUT, "/Records", &[], Some(&request))
73 .await
74 }
75
76 pub async fn delete_record(&self, app_id: i32, record_id: i32) -> Result<()> {
78 let path = format!("/Records/appId/{}/recordId/{}", app_id, record_id);
79 self
80 .request_no_content(Method::DELETE, &path, &[], Option::<&()>::None)
81 .await
82 }
83
84 pub async fn batch_get_records(
86 &self,
87 request: BatchGetRecordsRequest,
88 ) -> Result<CollectionResponse<Record>> {
89 self
90 .request(Method::POST, "/Records/batch-get", &[], Some(&request))
91 .await
92 }
93
94 pub async fn query_records(
96 &self,
97 request: QueryRecordsRequest,
98 paging: Option<PagingRequest>,
99 ) -> Result<PagedResponse<Record>> {
100 let mut query = Vec::new();
101 if let Some(p) = paging {
102 query.push(("PageNumber", p.page_number.to_string()));
103 query.push(("PageSize", p.page_size.to_string()));
104 }
105 let query_refs: Vec<(&str, String)> = query.iter().map(|(k, v)| (*k, v.clone())).collect();
106 self
107 .request(Method::POST, "/Records/Query", &query_refs, Some(&request))
108 .await
109 }
110
111 pub async fn batch_delete_records(&self, request: BatchDeleteRecordsRequest) -> Result<()> {
113 self
114 .request_no_content(Method::POST, "/Records/batch-delete", &[], Some(&request))
115 .await
116 }
117}