files_sdk/security/
sftp_host_keys.rs

1//! SFTP host key management
2
3use crate::{FilesClient, PaginationInfo, Result};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Serialize, Deserialize, Clone)]
7pub struct SftpHostKeyEntity {
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 SftpHostKeyHandler {
15    client: FilesClient,
16}
17
18impl SftpHostKeyHandler {
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<SftpHostKeyEntity>, PaginationInfo)> {
28        let mut endpoint = "/sftp_host_keys".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<SftpHostKeyEntity> = response.json().await?;
59        Ok((items, pagination))
60    }
61
62    pub async fn get(&self, id: i64) -> Result<SftpHostKeyEntity> {
63        let endpoint = format!("/sftp_host_keys/{}", 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<SftpHostKeyEntity> {
69        let response = self.client.post_raw("/sftp_host_keys", params).await?;
70        Ok(serde_json::from_value(response)?)
71    }
72
73    pub async fn update(&self, id: i64, params: serde_json::Value) -> Result<SftpHostKeyEntity> {
74        let endpoint = format!("/sftp_host_keys/{}", 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!("/sftp_host_keys/{}", 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 = SftpHostKeyHandler::new(client);
93    }
94}