files_sdk/as2/
as2_stations.rs

1//! AS2 station configuration
2
3use crate::{FilesClient, PaginationInfo, Result};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Serialize, Deserialize, Clone)]
7pub struct As2StationEntity {
8    pub id: Option<i64>,
9    #[serde(flatten)]
10    pub data: serde_json::Map<String, serde_json::Value>,
11}
12
13#[derive(Debug, Clone)]
14pub struct As2StationHandler {
15    client: FilesClient,
16}
17
18impl As2StationHandler {
19    pub fn new(client: FilesClient) -> Self {
20        Self { client }
21    }
22
23    pub async fn list(
24        &self,
25        cursor: Option<String>,
26        per_page: Option<i64>,
27    ) -> Result<(Vec<As2StationEntity>, PaginationInfo)> {
28        let mut endpoint = "/as2_stations".to_string();
29        let mut params = Vec::new();
30        if let Some(c) = cursor {
31            params.push(format!("cursor={}", c));
32        }
33        if let Some(pp) = per_page {
34            params.push(format!("per_page={}", pp));
35        }
36        if !params.is_empty() {
37            endpoint.push('?');
38            endpoint.push_str(&params.join("&"));
39        }
40
41        let url = format!("{}{}", self.client.inner.base_url, endpoint);
42        let response = reqwest::Client::new()
43            .get(&url)
44            .header("X-FilesAPI-Key", &self.client.inner.api_key)
45            .send()
46            .await?;
47
48        let headers = response.headers().clone();
49        let pagination = PaginationInfo::from_headers(&headers);
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        let items: Vec<As2StationEntity> = response.json().await?;
59        Ok((items, pagination))
60    }
61
62    pub async fn get(&self, id: i64) -> Result<As2StationEntity> {
63        let endpoint = format!("/as2_stations/{}", 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<As2StationEntity> {
69        let response = self.client.post_raw("/as2_stations", params).await?;
70        Ok(serde_json::from_value(response)?)
71    }
72
73    pub async fn update(&self, id: i64, params: serde_json::Value) -> Result<As2StationEntity> {
74        let endpoint = format!("/as2_stations/{}", 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!("/as2_stations/{}", id);
81        self.client.delete_raw(&endpoint).await?;
82        Ok(())
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    #[test]
90    fn test_handler_creation() {
91        let client = FilesClient::builder().api_key("test-key").build().unwrap();
92        let _handler = As2StationHandler::new(client);
93    }
94}