files_sdk/automation/
syncs.rs1use crate::{Result, client::FilesClient, types::PaginationInfo};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Serialize, Deserialize, Clone)]
5pub struct SyncEntity {
6 #[serde(flatten)]
7 pub data: serde_json::Map<String, serde_json::Value>,
8}
9
10pub struct SyncHandler {
11 client: FilesClient,
12}
13
14impl SyncHandler {
15 pub fn new(client: FilesClient) -> Self {
16 Self { client }
17 }
18
19 pub async fn list(
20 &self,
21 cursor: Option<String>,
22 per_page: Option<i64>,
23 ) -> Result<(Vec<SyncEntity>, PaginationInfo)> {
24 let mut endpoint = "/syncs".to_string();
25 let mut query_params = Vec::new();
26
27 if let Some(cursor) = cursor {
28 query_params.push(format!("cursor={}", cursor));
29 }
30
31 if let Some(per_page) = per_page {
32 query_params.push(format!("per_page={}", per_page));
33 }
34
35 if !query_params.is_empty() {
36 endpoint.push('?');
37 endpoint.push_str(&query_params.join("&"));
38 }
39
40 let url = format!("{}{}", self.client.inner.base_url, endpoint);
41 let response = reqwest::Client::new()
42 .get(&url)
43 .header("X-FilesAPI-Key", &self.client.inner.api_key)
44 .send()
45 .await?;
46
47 let headers = response.headers().clone();
48 let pagination = PaginationInfo::from_headers(&headers);
49
50 let status = response.status();
51 if !status.is_success() {
52 return Err(crate::FilesError::ApiError {
53 code: status.as_u16(),
54 message: response.text().await.unwrap_or_default(),
55 });
56 }
57
58 let entities: Vec<SyncEntity> = response.json().await?;
59 Ok((entities, pagination))
60 }
61
62 pub async fn get(&self, id: i64) -> Result<SyncEntity> {
63 let endpoint = format!("/syncs/{}", id);
64 let response = self.client.get_raw(&endpoint).await?;
65 Ok(serde_json::from_value(response)?)
66 }
67
68 pub async fn create(&self, params: serde_json::Value) -> Result<SyncEntity> {
69 let response = self.client.post_raw("/syncs", params).await?;
70 Ok(serde_json::from_value(response)?)
71 }
72
73 pub async fn update(&self, id: i64, params: serde_json::Value) -> Result<SyncEntity> {
74 let endpoint = format!("/syncs/{}", id);
75 let response = self.client.patch_raw(&endpoint, params).await?;
76 Ok(serde_json::from_value(response)?)
77 }
78
79 pub async fn delete(&self, id: i64) -> Result<()> {
80 let endpoint = format!("/syncs/{}", id);
81 self.client.delete_raw(&endpoint).await?;
82 Ok(())
83 }
84
85 pub async fn manual_run(&self, id: i64, params: serde_json::Value) -> Result<()> {
86 let endpoint = format!("/syncs/{}/manual_run", id);
87 self.client.post_raw(&endpoint, params).await?;
88 Ok(())
89 }
90
91 pub async fn dry_run(&self, id: i64, params: serde_json::Value) -> Result<SyncEntity> {
92 let endpoint = format!("/syncs/{}/dry_run", id);
93 let response = self.client.post_raw(&endpoint, params).await?;
94 Ok(serde_json::from_value(response)?)
95 }
96}