files_sdk/automation/
syncs.rs

1use 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                endpoint: None,
54                code: status.as_u16(),
55                message: response.text().await.unwrap_or_default(),
56            });
57        }
58
59        let entities: Vec<SyncEntity> = response.json().await?;
60        Ok((entities, pagination))
61    }
62
63    pub async fn get(&self, id: i64) -> Result<SyncEntity> {
64        let endpoint = format!("/syncs/{}", id);
65        let response = self.client.get_raw(&endpoint).await?;
66        Ok(serde_json::from_value(response)?)
67    }
68
69    pub async fn create(&self, params: serde_json::Value) -> Result<SyncEntity> {
70        let response = self.client.post_raw("/syncs", params).await?;
71        Ok(serde_json::from_value(response)?)
72    }
73
74    pub async fn update(&self, id: i64, params: serde_json::Value) -> Result<SyncEntity> {
75        let endpoint = format!("/syncs/{}", id);
76        let response = self.client.patch_raw(&endpoint, params).await?;
77        Ok(serde_json::from_value(response)?)
78    }
79
80    pub async fn delete(&self, id: i64) -> Result<()> {
81        let endpoint = format!("/syncs/{}", id);
82        self.client.delete_raw(&endpoint).await?;
83        Ok(())
84    }
85
86    pub async fn manual_run(&self, id: i64, params: serde_json::Value) -> Result<()> {
87        let endpoint = format!("/syncs/{}/manual_run", id);
88        self.client.post_raw(&endpoint, params).await?;
89        Ok(())
90    }
91
92    pub async fn dry_run(&self, id: i64, params: serde_json::Value) -> Result<SyncEntity> {
93        let endpoint = format!("/syncs/{}/dry_run", id);
94        let response = self.client.post_raw(&endpoint, params).await?;
95        Ok(serde_json::from_value(response)?)
96    }
97}