files_sdk/users/
public_keys.rs1use crate::{FilesClient, PaginationInfo, Result};
10use serde::{Deserialize, Serialize};
11use serde_json::json;
12
13#[derive(Debug, Serialize, Deserialize, Clone)]
15pub struct PublicKeyEntity {
16 pub id: Option<i64>,
18
19 pub title: Option<String>,
21
22 pub user_id: Option<i64>,
24
25 pub username: Option<String>,
27
28 pub fingerprint: Option<String>,
30
31 pub fingerprint_sha256: Option<String>,
33
34 pub created_at: Option<String>,
36
37 pub last_login_at: Option<String>,
39
40 pub status: Option<String>,
42
43 pub generated_private_key: Option<String>,
45
46 pub generated_public_key: Option<String>,
48}
49
50#[derive(Debug, Clone)]
52pub struct PublicKeyHandler {
53 client: FilesClient,
54}
55
56impl PublicKeyHandler {
57 pub fn new(client: FilesClient) -> Self {
59 Self { client }
60 }
61
62 pub async fn list(
88 &self,
89 user_id: Option<i64>,
90 cursor: Option<String>,
91 per_page: Option<i64>,
92 ) -> Result<(Vec<PublicKeyEntity>, PaginationInfo)> {
93 let mut endpoint = "/public_keys".to_string();
94 let mut query_params = Vec::new();
95
96 if let Some(user_id) = user_id {
97 query_params.push(format!("user_id={}", user_id));
98 }
99
100 if let Some(cursor) = cursor {
101 query_params.push(format!("cursor={}", cursor));
102 }
103
104 if let Some(per_page) = per_page {
105 query_params.push(format!("per_page={}", per_page));
106 }
107
108 if !query_params.is_empty() {
109 endpoint.push('?');
110 endpoint.push_str(&query_params.join("&"));
111 }
112
113 let url = format!("{}{}", self.client.inner.base_url, endpoint);
114 let response = reqwest::Client::new()
115 .get(&url)
116 .header("X-FilesAPI-Key", &self.client.inner.api_key)
117 .send()
118 .await?;
119
120 let headers = response.headers().clone();
121 let pagination = PaginationInfo::from_headers(&headers);
122
123 let status = response.status();
124 if !status.is_success() {
125 return Err(crate::FilesError::ApiError {
126 code: status.as_u16(),
127 message: response.text().await.unwrap_or_default(),
128 });
129 }
130
131 let keys: Vec<PublicKeyEntity> = response.json().await?;
132 Ok((keys, pagination))
133 }
134
135 pub async fn get(&self, id: i64) -> Result<PublicKeyEntity> {
141 let endpoint = format!("/public_keys/{}", id);
142 let response = self.client.get_raw(&endpoint).await?;
143 Ok(serde_json::from_value(response)?)
144 }
145
146 pub async fn create(
154 &self,
155 user_id: i64,
156 title: &str,
157 public_key: &str,
158 ) -> Result<PublicKeyEntity> {
159 let body = json!({
160 "user_id": user_id,
161 "title": title,
162 "public_key": public_key,
163 });
164
165 let response = self.client.post_raw("/public_keys", body).await?;
166 Ok(serde_json::from_value(response)?)
167 }
168
169 pub async fn generate(
179 &self,
180 user_id: i64,
181 title: &str,
182 algorithm: &str,
183 length: Option<i64>,
184 password: Option<&str>,
185 ) -> Result<PublicKeyEntity> {
186 let mut body = json!({
187 "user_id": user_id,
188 "title": title,
189 "generate_keypair": true,
190 "generate_algorithm": algorithm,
191 });
192
193 if let Some(length) = length {
194 body["generate_length"] = json!(length);
195 }
196
197 if let Some(password) = password {
198 body["generate_private_key_password"] = json!(password);
199 }
200
201 let response = self.client.post_raw("/public_keys", body).await?;
202 Ok(serde_json::from_value(response)?)
203 }
204
205 pub async fn update(&self, id: i64, title: &str) -> Result<PublicKeyEntity> {
212 let body = json!({
213 "title": title,
214 });
215
216 let endpoint = format!("/public_keys/{}", id);
217 let response = self.client.patch_raw(&endpoint, body).await?;
218 Ok(serde_json::from_value(response)?)
219 }
220
221 pub async fn delete(&self, id: i64) -> Result<()> {
227 let endpoint = format!("/public_keys/{}", id);
228 self.client.delete_raw(&endpoint).await?;
229 Ok(())
230 }
231}
232
233#[cfg(test)]
234mod tests {
235 use super::*;
236
237 #[test]
238 fn test_handler_creation() {
239 let client = FilesClient::builder().api_key("test-key").build().unwrap();
240 let _handler = PublicKeyHandler::new(client);
241 }
242}