files_sdk/security/
gpg_keys.rs1use crate::{FilesClient, PaginationInfo, Result};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Serialize, Deserialize, Clone)]
7pub struct GpgKeyEntity {
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 GpgKeyHandler {
15 client: FilesClient,
16}
17
18impl GpgKeyHandler {
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<GpgKeyEntity>, PaginationInfo)> {
28 let mut endpoint = "/gpg_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(¶ms.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 code: status.as_u16(),
54 message: response.text().await.unwrap_or_default(),
55 });
56 }
57 let items: Vec<GpgKeyEntity> = response.json().await?;
58 Ok((items, pagination))
59 }
60
61 pub async fn get(&self, id: i64) -> Result<GpgKeyEntity> {
62 let endpoint = format!("/gpg_keys/{}", id);
63 let response = self.client.get_raw(&endpoint).await?;
64 Ok(serde_json::from_value(response)?)
65 }
66
67 pub async fn create(&self, params: serde_json::Value) -> Result<GpgKeyEntity> {
68 let response = self.client.post_raw("/gpg_keys", params).await?;
69 Ok(serde_json::from_value(response)?)
70 }
71
72 pub async fn update(&self, id: i64, params: serde_json::Value) -> Result<GpgKeyEntity> {
73 let endpoint = format!("/gpg_keys/{}", id);
74 let response = self.client.patch_raw(&endpoint, params).await?;
75 Ok(serde_json::from_value(response)?)
76 }
77
78 pub async fn delete(&self, id: i64) -> Result<()> {
79 let endpoint = format!("/gpg_keys/{}", id);
80 self.client.delete_raw(&endpoint).await?;
81 Ok(())
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88 #[test]
89 fn test_handler_creation() {
90 let client = FilesClient::builder().api_key("test-key").build().unwrap();
91 let _handler = GpgKeyHandler::new(client);
92 }
93}