files-sdk 0.4.1

Rust SDK for the Files.com API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Remote Server operations
//!
//! Remote Servers represent connections to external storage providers like S3, Azure,
//! FTP/SFTP servers, and more for syncing or mounting.

use crate::{FilesClient, PaginationInfo, Result};
use serde::{Deserialize, Serialize};
use serde_json::json;

/// A Remote Server entity
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoteServerEntity {
    /// Remote server ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i64>,

    /// Server type (e.g., s3, azure, ftp, sftp)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub server_type: Option<String>,

    /// Server name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// Authentication method
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authentication_method: Option<String>,

    /// Hostname (for FTP/SFTP)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hostname: Option<String>,

    /// Port (for FTP/SFTP)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub port: Option<i64>,

    /// Username
    #[serde(skip_serializing_if = "Option::is_none")]
    pub username: Option<String>,

    /// Remote home path
    #[serde(skip_serializing_if = "Option::is_none")]
    pub remote_home_path: Option<String>,

    /// Use SSL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ssl: Option<String>,

    /// Max connections
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_connections: Option<i64>,

    /// Pin to site region
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pin_to_site_region: Option<bool>,

    /// Pinned region
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pinned_region: Option<String>,

    /// S3 bucket name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub s3_bucket: Option<String>,

    /// S3 region
    #[serde(skip_serializing_if = "Option::is_none")]
    pub s3_region: Option<String>,

    /// AWS access key
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aws_access_key: Option<String>,

    /// Server host key (SSH)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub server_host_key: Option<String>,

    /// Server certificate
    #[serde(skip_serializing_if = "Option::is_none")]
    pub server_certificate: Option<String>,

    /// Azure Blob storage account
    #[serde(skip_serializing_if = "Option::is_none")]
    pub azure_blob_storage_account: Option<String>,

    /// Azure Blob storage container
    #[serde(skip_serializing_if = "Option::is_none")]
    pub azure_blob_storage_container: Option<String>,

    /// Azure Blob storage DNS suffix
    #[serde(skip_serializing_if = "Option::is_none")]
    pub azure_blob_storage_dns_suffix: Option<String>,

    /// Azure Blob hierarchical namespace
    #[serde(skip_serializing_if = "Option::is_none")]
    pub azure_blob_storage_hierarchical_namespace: Option<bool>,

    /// Azure Files storage account
    #[serde(skip_serializing_if = "Option::is_none")]
    pub azure_files_storage_account: Option<String>,

    /// Azure Files storage share name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub azure_files_storage_share_name: Option<String>,

    /// Azure Files storage DNS suffix
    #[serde(skip_serializing_if = "Option::is_none")]
    pub azure_files_storage_dns_suffix: Option<String>,

    /// Backblaze B2 bucket
    #[serde(skip_serializing_if = "Option::is_none")]
    pub backblaze_b2_bucket: Option<String>,

    /// Backblaze B2 S3 endpoint
    #[serde(skip_serializing_if = "Option::is_none")]
    pub backblaze_b2_s3_endpoint: Option<String>,

    /// Wasabi bucket
    #[serde(skip_serializing_if = "Option::is_none")]
    pub wasabi_bucket: Option<String>,

    /// Wasabi region
    #[serde(skip_serializing_if = "Option::is_none")]
    pub wasabi_region: Option<String>,

    /// Wasabi access key
    #[serde(skip_serializing_if = "Option::is_none")]
    pub wasabi_access_key: Option<String>,

    /// Google Cloud Storage bucket
    #[serde(skip_serializing_if = "Option::is_none")]
    pub google_cloud_storage_bucket: Option<String>,

    /// Google Cloud Storage project ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub google_cloud_storage_project_id: Option<String>,

    /// S3-compatible access key
    #[serde(skip_serializing_if = "Option::is_none")]
    pub s3_compatible_access_key: Option<String>,

    /// S3-compatible bucket
    #[serde(skip_serializing_if = "Option::is_none")]
    pub s3_compatible_bucket: Option<String>,

    /// S3-compatible endpoint
    #[serde(skip_serializing_if = "Option::is_none")]
    pub s3_compatible_endpoint: Option<String>,

    /// S3-compatible region
    #[serde(skip_serializing_if = "Option::is_none")]
    pub s3_compatible_region: Option<String>,

    /// Files Agent API token
    #[serde(skip_serializing_if = "Option::is_none")]
    pub files_agent_api_token: Option<String>,

    /// Files Agent root
    #[serde(skip_serializing_if = "Option::is_none")]
    pub files_agent_root: Option<String>,

    /// Files Agent permission set
    #[serde(skip_serializing_if = "Option::is_none")]
    pub files_agent_permission_set: Option<String>,

    /// Files Agent version
    #[serde(skip_serializing_if = "Option::is_none")]
    pub files_agent_version: Option<String>,

    /// Filebase bucket
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filebase_bucket: Option<String>,

    /// Filebase access key
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filebase_access_key: Option<String>,

    /// Cloudflare bucket
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cloudflare_bucket: Option<String>,

    /// Cloudflare access key
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cloudflare_access_key: Option<String>,

    /// Cloudflare endpoint
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cloudflare_endpoint: Option<String>,

    /// Dropbox teams
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dropbox_teams: Option<bool>,

    /// Linode bucket
    #[serde(skip_serializing_if = "Option::is_none")]
    pub linode_bucket: Option<String>,

    /// Linode access key
    #[serde(skip_serializing_if = "Option::is_none")]
    pub linode_access_key: Option<String>,

    /// Linode region
    #[serde(skip_serializing_if = "Option::is_none")]
    pub linode_region: Option<String>,

    /// OneDrive account type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub one_drive_account_type: Option<String>,

    /// Server disabled
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disabled: Option<bool>,

    /// Supports versioning
    #[serde(skip_serializing_if = "Option::is_none")]
    pub supports_versioning: Option<bool>,

    /// Enable dedicated IPs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_dedicated_ips: Option<bool>,

    /// Auth account name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_account_name: Option<String>,

    /// Auth status
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_status: Option<String>,

    /// Google Cloud Storage S3-compatible access key
    #[serde(skip_serializing_if = "Option::is_none")]
    pub google_cloud_storage_s3_compatible_access_key: Option<String>,
}

/// Handler for remote server operations
pub struct RemoteServerHandler {
    client: FilesClient,
}

impl RemoteServerHandler {
    /// Create a new remote server handler
    pub fn new(client: FilesClient) -> Self {
        Self { client }
    }

    /// List remote servers
    ///
    /// # Arguments
    /// * `cursor` - Pagination cursor
    /// * `per_page` - Results per page
    ///
    /// # Returns
    /// Tuple of (remote_servers, pagination_info)
    ///
    /// # Example
    /// ```no_run
    /// use files_sdk::{FilesClient, RemoteServerHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = RemoteServerHandler::new(client);
    /// let (servers, _) = handler.list(None, None).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn list(
        &self,
        cursor: Option<&str>,
        per_page: Option<i64>,
    ) -> Result<(Vec<RemoteServerEntity>, PaginationInfo)> {
        let mut params = vec![];
        if let Some(c) = cursor {
            params.push(("cursor", c.to_string()));
        }
        if let Some(pp) = per_page {
            params.push(("per_page", pp.to_string()));
        }

        let query = if params.is_empty() {
            String::new()
        } else {
            format!(
                "?{}",
                params
                    .iter()
                    .map(|(k, v)| format!("{}={}", k, v))
                    .collect::<Vec<_>>()
                    .join("&")
            )
        };

        let response = self
            .client
            .get_raw(&format!("/remote_servers{}", query))
            .await?;
        let servers: Vec<RemoteServerEntity> = serde_json::from_value(response)?;

        let pagination = PaginationInfo {
            cursor_next: None,
            cursor_prev: None,
        };

        Ok((servers, pagination))
    }

    /// Get a specific remote server
    ///
    /// # Arguments
    /// * `id` - Remote server ID
    pub async fn get(&self, id: i64) -> Result<RemoteServerEntity> {
        let response = self
            .client
            .get_raw(&format!("/remote_servers/{}", id))
            .await?;
        Ok(serde_json::from_value(response)?)
    }

    /// Get remote server configuration file
    ///
    /// # Arguments
    /// * `id` - Remote server ID
    pub async fn get_configuration_file(&self, id: i64) -> Result<serde_json::Value> {
        let response = self
            .client
            .get_raw(&format!("/remote_servers/{}/configuration_file", id))
            .await?;
        Ok(response)
    }

    /// Create a new remote server
    ///
    /// # Arguments
    /// * `name` - Server name (required)
    /// * `server_type` - Server type (required)
    /// * `hostname` - Hostname (for FTP/SFTP)
    /// * `username` - Username
    /// * `port` - Port
    /// * `s3_bucket` - S3 bucket name
    /// * `s3_region` - S3 region
    ///
    /// # Returns
    /// The created remote server
    #[allow(clippy::too_many_arguments)]
    pub async fn create(
        &self,
        name: &str,
        server_type: &str,
        hostname: Option<&str>,
        username: Option<&str>,
        port: Option<i64>,
        s3_bucket: Option<&str>,
        s3_region: Option<&str>,
    ) -> Result<RemoteServerEntity> {
        let mut request_body = json!({
            "name": name,
            "server_type": server_type,
        });

        if let Some(h) = hostname {
            request_body["hostname"] = json!(h);
        }
        if let Some(u) = username {
            request_body["username"] = json!(u);
        }
        if let Some(p) = port {
            request_body["port"] = json!(p);
        }
        if let Some(b) = s3_bucket {
            request_body["s3_bucket"] = json!(b);
        }
        if let Some(r) = s3_region {
            request_body["s3_region"] = json!(r);
        }

        let response = self
            .client
            .post_raw("/remote_servers", request_body)
            .await?;
        Ok(serde_json::from_value(response)?)
    }

    /// Update a remote server
    ///
    /// # Arguments
    /// * `id` - Remote server ID
    /// * `name` - New server name
    /// * `hostname` - New hostname
    /// * `port` - New port
    /// * `disabled` - Disable the server
    ///
    /// # Returns
    /// The updated remote server
    #[allow(clippy::too_many_arguments)]
    pub async fn update(
        &self,
        id: i64,
        name: Option<&str>,
        hostname: Option<&str>,
        port: Option<i64>,
        disabled: Option<bool>,
    ) -> Result<RemoteServerEntity> {
        let mut request_body = json!({});

        if let Some(n) = name {
            request_body["name"] = json!(n);
        }
        if let Some(h) = hostname {
            request_body["hostname"] = json!(h);
        }
        if let Some(p) = port {
            request_body["port"] = json!(p);
        }
        if let Some(d) = disabled {
            request_body["disabled"] = json!(d);
        }

        let response = self
            .client
            .patch_raw(&format!("/remote_servers/{}", id), request_body)
            .await?;
        Ok(serde_json::from_value(response)?)
    }

    /// Delete a remote server
    ///
    /// # Arguments
    /// * `id` - Remote server ID
    pub async fn delete(&self, id: i64) -> Result<()> {
        self.client
            .delete_raw(&format!("/remote_servers/{}", id))
            .await?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_handler_creation() {
        let client = FilesClient::builder().api_key("test-key").build().unwrap();
        let _handler = RemoteServerHandler::new(client);
    }
}