files_sdk/automation/
sync_runs.rs1use crate::{Result, client::FilesClient, types::PaginationInfo};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Serialize, Deserialize, Clone)]
5pub struct SyncRunEntity {
6 #[serde(flatten)]
7 pub data: serde_json::Map<String, serde_json::Value>,
8}
9
10pub struct SyncRunHandler {
11 client: FilesClient,
12}
13
14impl SyncRunHandler {
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<SyncRunEntity>, PaginationInfo)> {
24 let mut endpoint = "/sync_runs".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<SyncRunEntity> = response.json().await?;
60 Ok((entities, pagination))
61 }
62
63 pub async fn get(&self, id: i64) -> Result<SyncRunEntity> {
64 let endpoint = format!("/sync_runs/{}", id);
65 let response = self.client.get_raw(&endpoint).await?;
66 Ok(serde_json::from_value(response)?)
67 }
68}