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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Bundle (Share Link) operations
//!
//! Bundles are the Files.com API term for Share Links. They allow you to share files
//! and folders with external users via a public URL with granular access controls.
//!
//! # Features
//!
//! - Create shareable links to files and folders
//! - Password protection and expiration dates
//! - Access controls (read, write, preview-only)
//! - Registration requirements and user tracking
//! - Email sharing with notifications
//! - Custom branding and legal clickwrap
//!
//! # Example
//!
//! ```no_run
//! use files_sdk::{FilesClient, BundleHandler, BundlePermission};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = FilesClient::builder()
//!     .api_key("your-api-key")
//!     .build()?;
//!
//! let handler = BundleHandler::new(client);
//!
//! // Create a password-protected share link that expires in 7 days
//! let bundle = handler.create(
//!     vec!["/reports/quarterly-2024.pdf".to_string()],
//!     Some("secure-password"),
//!     Some("2024-12-31T23:59:59Z"),
//!     None,
//!     Some("Q4 2024 Financial Report"),
//!     Some("Internal sharing only"),
//!     None,
//!     Some(true),
//!     Some(BundlePermission::Read)
//! ).await?;
//!
//! println!("Share link: {}", bundle.url.unwrap_or_default());
//!
//! // Share via email
//! handler.share(
//!     bundle.id.unwrap(),
//!     vec!["colleague@company.com".to_string()],
//!     Some("Please review the Q4 report")
//! ).await?;
//! # Ok(())
//! # }
//! ```

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

/// Bundle permissions enum
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BundlePermission {
    /// Read-only access
    Read,
    /// Write-only access (upload)
    Write,
    /// Read and write access
    ReadWrite,
    /// Full access
    Full,
    /// No access
    None,
    /// Preview only (no download)
    PreviewOnly,
}

/// A Bundle entity (Share Link)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BundleEntity {
    /// Bundle ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i64>,

    /// Bundle code - forms the end part of the public URL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,

    /// Public URL of share link
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,

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

    /// Bundle internal note
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,

    /// Is password protected?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub password_protected: Option<bool>,

    /// Permissions that apply to folders in this share link
    #[serde(skip_serializing_if = "Option::is_none")]
    pub permissions: Option<BundlePermission>,

    /// Preview only mode
    #[serde(skip_serializing_if = "Option::is_none")]
    pub preview_only: Option<bool>,

    /// Require registration to access?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_registration: Option<bool>,

    /// Require explicit share recipient?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_share_recipient: Option<bool>,

    /// Require logout after each access?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_logout: Option<bool>,

    /// Legal clickwrap text
    #[serde(skip_serializing_if = "Option::is_none")]
    pub clickwrap_body: Option<String>,

    /// ID of clickwrap to use
    #[serde(skip_serializing_if = "Option::is_none")]
    pub clickwrap_id: Option<i64>,

    /// Skip name in registration?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub skip_name: Option<bool>,

    /// Skip email in registration?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub skip_email: Option<bool>,

    /// Skip company in registration?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub skip_company: Option<bool>,

    /// Bundle expiration date/time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<String>,

    /// Date when share becomes accessible
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_access_on_date: Option<String>,

    /// Bundle created at
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<String>,

    /// Don't create subfolders for submissions?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dont_separate_submissions_by_folder: Option<bool>,

    /// Maximum number of uses
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_uses: Option<i64>,

    /// Template for submission subfolder paths
    #[serde(skip_serializing_if = "Option::is_none")]
    pub path_template: Option<String>,

    /// Timezone for path template timestamps
    #[serde(skip_serializing_if = "Option::is_none")]
    pub path_template_time_zone: Option<String>,

    /// Send receipt to uploader?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub send_email_receipt_to_uploader: Option<bool>,

    /// Snapshot ID containing bundle contents
    #[serde(skip_serializing_if = "Option::is_none")]
    pub snapshot_id: Option<i64>,

    /// Bundle creator user ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id: Option<i64>,

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

    /// Associated inbox ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inbox_id: Option<i64>,

    /// Has associated inbox?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_inbox: Option<bool>,

    /// Prevent folder uploads?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dont_allow_folders_in_uploads: Option<bool>,

    /// Paths included in bundle (not provided when listing)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub paths: Option<Vec<String>>,

    /// Page link and button color
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color_left: Option<String>,

    /// Top bar link color
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color_link: Option<String>,

    /// Page link and button color
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color_text: Option<String>,

    /// Top bar background color
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color_top: Option<String>,

    /// Top bar text color
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color_top_text: Option<String>,
}

/// Handler for bundle operations
pub struct BundleHandler {
    client: FilesClient,
}

impl BundleHandler {
    /// Create a new bundle handler
    pub fn new(client: FilesClient) -> Self {
        Self { client }
    }

    /// List all bundles accessible to the current user
    ///
    /// Returns a paginated list of bundles (share links) with optional filtering.
    ///
    /// # Arguments
    ///
    /// * `user_id` - Filter bundles by user ID (None for all accessible bundles)
    /// * `cursor` - Pagination cursor from previous response
    /// * `per_page` - Number of results per page (max 10,000)
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// - Vector of `BundleEntity` objects
    /// - `PaginationInfo` with cursors for next/previous pages
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, BundleHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = BundleHandler::new(client);
    ///
    /// // List first page of bundles
    /// let (bundles, pagination) = handler.list(None, None, Some(50)).await?;
    ///
    /// for bundle in bundles {
    ///     println!("Bundle: {} - {}",
    ///         bundle.code.unwrap_or_default(),
    ///         bundle.url.unwrap_or_default());
    /// }
    ///
    /// // Get next page if available
    /// if let Some(next_cursor) = pagination.cursor_next {
    ///     let (more_bundles, _) = handler.list(None, Some(&next_cursor), Some(50)).await?;
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn list(
        &self,
        user_id: Option<i64>,
        cursor: Option<&str>,
        per_page: Option<i64>,
    ) -> Result<(Vec<BundleEntity>, PaginationInfo)> {
        let mut params = vec![];
        if let Some(uid) = user_id {
            params.push(("user_id", uid.to_string()));
        }
        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!("/bundles{}", query)).await?;
        let bundles: Vec<BundleEntity> = serde_json::from_value(response)?;

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

        Ok((bundles, pagination))
    }

    /// Get details of a specific bundle by ID
    ///
    /// # Arguments
    ///
    /// * `id` - The unique bundle ID
    ///
    /// # Returns
    ///
    /// A `BundleEntity` with complete bundle information
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, BundleHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = BundleHandler::new(client);
    ///
    /// let bundle = handler.get(12345).await?;
    /// println!("Bundle URL: {}", bundle.url.unwrap_or_default());
    /// println!("Expires: {}", bundle.expires_at.unwrap_or_default());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get(&self, id: i64) -> Result<BundleEntity> {
        let response = self.client.get_raw(&format!("/bundles/{}", id)).await?;
        Ok(serde_json::from_value(response)?)
    }

    /// Create a new bundle (share link)
    ///
    /// Creates a shareable link to one or more files or folders with configurable
    /// access controls and restrictions.
    ///
    /// # Arguments
    ///
    /// * `paths` - Vector of file/folder paths to share (required, must not be empty)
    /// * `password` - Password required to access the bundle
    /// * `expires_at` - ISO 8601 timestamp when bundle expires (e.g., "2024-12-31T23:59:59Z")
    /// * `max_uses` - Maximum number of times bundle can be accessed
    /// * `description` - Public description shown to recipients
    /// * `note` - Private internal note (not shown to recipients)
    /// * `code` - Custom URL code (auto-generated if not provided)
    /// * `require_registration` - Require recipients to register before access
    /// * `permissions` - Access level (see [`BundlePermission`])
    ///
    /// # Returns
    ///
    /// The newly created `BundleEntity` with URL and access details
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, BundleHandler, BundlePermission};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = BundleHandler::new(client);
    ///
    /// // Create a simple share link
    /// let bundle = handler.create(
    ///     vec!["/documents/report.pdf".to_string()],
    ///     None,
    ///     None,
    ///     None,
    ///     Some("Monthly Report"),
    ///     None,
    ///     None,
    ///     Some(false),
    ///     Some(BundlePermission::Read)
    /// ).await?;
    ///
    /// println!("Share this link: {}", bundle.url.unwrap());
    /// # Ok(())
    /// # }
    /// ```
    #[allow(clippy::too_many_arguments)]
    pub async fn create(
        &self,
        paths: Vec<String>,
        password: Option<&str>,
        expires_at: Option<&str>,
        max_uses: Option<i64>,
        description: Option<&str>,
        note: Option<&str>,
        code: Option<&str>,
        require_registration: Option<bool>,
        permissions: Option<BundlePermission>,
    ) -> Result<BundleEntity> {
        let mut body = json!({
            "paths": paths,
        });

        if let Some(p) = password {
            body["password"] = json!(p);
        }
        if let Some(e) = expires_at {
            body["expires_at"] = json!(e);
        }
        if let Some(m) = max_uses {
            body["max_uses"] = json!(m);
        }
        if let Some(d) = description {
            body["description"] = json!(d);
        }
        if let Some(n) = note {
            body["note"] = json!(n);
        }
        if let Some(c) = code {
            body["code"] = json!(c);
        }
        if let Some(r) = require_registration {
            body["require_registration"] = json!(r);
        }
        if let Some(perm) = permissions {
            body["permissions"] = json!(perm);
        }

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

    /// Update an existing bundle's settings
    ///
    /// Modifies bundle properties such as password, expiration, and description.
    /// Only provided fields will be updated; omitted fields remain unchanged.
    ///
    /// # Arguments
    ///
    /// * `id` - Bundle ID to update
    /// * `password` - New password (pass empty string to remove password)
    /// * `expires_at` - New expiration timestamp
    /// * `max_uses` - New maximum access count
    /// * `description` - New public description
    /// * `note` - New internal note
    ///
    /// # Returns
    ///
    /// The updated `BundleEntity`
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, BundleHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = BundleHandler::new(client);
    ///
    /// // Extend expiration and update description
    /// let bundle = handler.update(
    ///     12345,
    ///     None,
    ///     Some("2025-06-30T23:59:59Z"),
    ///     None,
    ///     Some("Updated report - extended access"),
    ///     None
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[allow(clippy::too_many_arguments)]
    pub async fn update(
        &self,
        id: i64,
        password: Option<&str>,
        expires_at: Option<&str>,
        max_uses: Option<i64>,
        description: Option<&str>,
        note: Option<&str>,
    ) -> Result<BundleEntity> {
        let mut body = json!({});

        if let Some(p) = password {
            body["password"] = json!(p);
        }
        if let Some(e) = expires_at {
            body["expires_at"] = json!(e);
        }
        if let Some(m) = max_uses {
            body["max_uses"] = json!(m);
        }
        if let Some(d) = description {
            body["description"] = json!(d);
        }
        if let Some(n) = note {
            body["note"] = json!(n);
        }

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

    /// Delete a bundle permanently
    ///
    /// Removes the bundle and revokes access via its share link. This operation
    /// cannot be undone.
    ///
    /// # Arguments
    ///
    /// * `id` - Bundle ID to delete
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, BundleHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = BundleHandler::new(client);
    ///
    /// handler.delete(12345).await?;
    /// println!("Bundle deleted successfully");
    /// # Ok(())
    /// # }
    /// ```
    pub async fn delete(&self, id: i64) -> Result<()> {
        self.client.delete_raw(&format!("/bundles/{}", id)).await?;
        Ok(())
    }

    /// Share a bundle via email
    ///
    /// Sends email notifications with the bundle link to specified recipients.
    /// Recipients receive an email with the share link and optional message.
    ///
    /// # Arguments
    ///
    /// * `id` - Bundle ID to share
    /// * `to` - Vector of recipient email addresses
    /// * `note` - Optional message to include in the email
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, BundleHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = BundleHandler::new(client);
    ///
    /// // Share with multiple recipients
    /// handler.share(
    ///     12345,
    ///     vec![
    ///         "user1@example.com".to_string(),
    ///         "user2@example.com".to_string()
    ///     ],
    ///     Some("Please review these files by Friday")
    /// ).await?;
    ///
    /// println!("Bundle shared successfully");
    /// # Ok(())
    /// # }
    /// ```
    pub async fn share(&self, id: i64, to: Vec<String>, note: Option<&str>) -> Result<()> {
        let mut body = json!({
            "to": to,
        });

        if let Some(n) = note {
            body["note"] = json!(n);
        }

        self.client
            .post_raw(&format!("/bundles/{}/share", id), body)
            .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 = BundleHandler::new(client);
    }
}