dco3 0.3.0

Async API wrapper for DRACOON in Rust.
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
#![allow(dead_code, unused_imports)]

pub mod filters;
pub mod sorts;

pub use filters::*;
pub use sorts::*;

use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::sync::Arc;
use std::sync::Mutex;

use crate::{
    auth::{errors::DracoonClientError, models::DracoonErrorResponse},
    models::{ObjectExpiration, Range, RangedItems},
    utils::parse_body,
    utils::FromResponse,
};

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use dco3_crypto::FileKey;
use dco3_crypto::PublicKeyContainer;
use reqwest::{Response, StatusCode};
use serde::{Deserialize, Serialize};

use super::rooms::models::NodePermissionsBuilder;

/// A callback function that is called after each chunk is processed (download)
pub type DownloadProgressCallback = Box<dyn FnMut(u64, u64) + Send + Sync>;

/// A callback function that is called after each chunk is processed (upload)
pub type UploadProgressCallback = Box<dyn FnMut(u64, u64) + Send + Sync>;

/// A callback function (thread-safe) that can be cloned and called from multiple threads (upload)
pub struct CloneableUploadProgressCallback(Arc<Mutex<UploadProgressCallback>>);

impl Clone for CloneableUploadProgressCallback {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl CloneableUploadProgressCallback {
    pub fn new<F>(callback: F) -> Self
    where
        F: 'static + FnMut(u64, u64) + Send + Sync,
    {
        Self(Arc::new(Mutex::new(Box::new(callback))))
    }

    pub fn call(&self, bytes_read: u64, total_size: u64) {
        (self.0.lock().unwrap())(bytes_read, total_size);
    }
}

/// file meta information (name, size, timestamp creation, timestamp modification)
#[derive(Debug, Clone)]
pub struct FileMeta(
    pub String,
    pub u64,
    pub Option<DateTime<Utc>>,
    pub Option<DateTime<Utc>>,
);

#[derive(Default)]
pub struct FileMetaBuilder {
    name: Option<String>,
    size: Option<u64>,
    timestamp_creation: Option<DateTime<Utc>>,
    timestamp_modification: Option<DateTime<Utc>>,
}

impl FileMeta {
    pub fn builder() -> FileMetaBuilder {
        FileMetaBuilder::new()
    }
}

impl FileMetaBuilder {
    pub fn new() -> Self {
        Self {
            name: None,
            size: None,
            timestamp_creation: None,
            timestamp_modification: None,
        }
    }

    pub fn with_name(mut self, name: String) -> Self {
        self.name = Some(name);
        self
    }

    pub fn with_size(mut self, size: u64) -> Self {
        self.size = Some(size);
        self
    }

    pub fn with_timestamp_creation(mut self, timestamp_creation: DateTime<Utc>) -> Self {
        self.timestamp_creation = Some(timestamp_creation);
        self
    }

    pub fn with_timestamp_modification(mut self, timestamp_modification: DateTime<Utc>) -> Self {
        self.timestamp_modification = Some(timestamp_modification);
        self
    }

    pub fn build(self) -> FileMeta {
        FileMeta(
            self.name.unwrap(),
            self.size.unwrap(),
            self.timestamp_creation,
            self.timestamp_modification,
        )
    }
}

/// upload options (expiration, classification, keep share links, resolution strategy)
#[derive(Debug, Clone, Default)]
pub struct UploadOptions(
    pub Option<ObjectExpiration>,
    pub Option<u8>,
    pub Option<bool>,
    pub Option<ResolutionStrategy>,
);

impl UploadOptions {
    pub fn builder() -> UploadOptionsBuilder {
        UploadOptionsBuilder::new()
    }
}

#[derive(Default)]
pub struct UploadOptionsBuilder {
    expiration: Option<ObjectExpiration>,
    classification: Option<u8>,
    keep_share_links: Option<bool>,
    resolution_strategy: Option<ResolutionStrategy>,
}

impl UploadOptionsBuilder {
    pub fn new() -> Self {
        Self {
            expiration: None,
            classification: None,
            keep_share_links: None,
            resolution_strategy: None,
        }
    }

    pub fn with_expiration(mut self, expiration: ObjectExpiration) -> Self {
        self.expiration = Some(expiration);
        self
    }

    pub fn with_classification(mut self, classification: u8) -> Self {
        self.classification = Some(classification);
        self
    }

    pub fn with_keep_share_links(mut self, keep_share_links: bool) -> Self {
        self.keep_share_links = Some(keep_share_links);
        self
    }

    pub fn with_resolution_strategy(mut self, resolution_strategy: ResolutionStrategy) -> Self {
        self.resolution_strategy = Some(resolution_strategy);
        self
    }

    pub fn build(self) -> UploadOptions {
        UploadOptions(
            self.expiration,
            self.classification,
            self.keep_share_links,
            self.resolution_strategy,
        )
    }
}

/// A list of nodes in DRACOON - GET /nodes
pub type NodeList = RangedItems<Node>;

impl NodeList {
    pub fn get_files(&self) -> Vec<Node> {
        self.items
            .iter()
            .filter(|node| node.node_type == NodeType::File)
            .cloned()
            .collect()
    }

    pub fn get_folders(&self) -> Vec<Node> {
        self.items
            .iter()
            .filter(|node| node.node_type == NodeType::Folder)
            .cloned()
            .collect()
    }

    pub fn get_rooms(&self) -> Vec<Node> {
        self.items
            .iter()
            .filter(|node| node.node_type == NodeType::Room)
            .cloned()
            .collect()
    }
}

/// A node in DRACOON - GET /nodes/{nodeId}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Node {
    pub id: u64,
    pub reference_id: Option<u64>,
    #[serde(rename = "type")]
    pub node_type: NodeType,
    pub name: String,
    pub timestamp_creation: Option<String>,
    pub timestamp_modification: Option<String>,
    pub parent_id: Option<u64>,
    pub parent_path: Option<String>,
    pub created_at: Option<String>,
    pub created_by: Option<UserInfo>,
    pub updated_at: Option<String>,
    pub updated_by: Option<UserInfo>,
    pub expire_at: Option<String>,
    pub hash: Option<String>,
    pub file_type: Option<String>,
    pub media_type: Option<String>,
    pub size: Option<u64>,
    pub classification: Option<u64>,
    pub notes: Option<String>,
    pub permissions: Option<NodePermissions>,
    pub inherit_permissions: Option<bool>,
    pub is_encrypted: Option<bool>,
    pub encryption_info: Option<EncryptionInfo>,
    pub cnt_deleted_versions: Option<u64>,
    pub cnt_comments: Option<u64>,
    pub cnt_upload_shares: Option<u64>,
    pub cnt_download_shares: Option<u64>,
    pub recycle_bin_retention_period: Option<u64>,
    pub has_activities_log: Option<bool>,
    pub quota: Option<u64>,
    pub is_favorite: Option<bool>,
    pub branch_version: Option<u64>,
    pub media_token: Option<String>,
    pub is_browsable: Option<bool>,
    pub cnt_rooms: Option<u64>,
    pub cnt_folders: Option<u64>,
    pub cnt_files: Option<u64>,
    pub auth_parent_id: Option<u64>,
}

#[async_trait]
impl FromResponse for Node {
    async fn from_response(response: Response) -> Result<Self, DracoonClientError> {
        parse_body::<Self, DracoonErrorResponse>(response).await
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum NodeType {
    #[serde(rename = "room")]
    Room,
    #[serde(rename = "folder")]
    Folder,
    #[serde(rename = "file")]
    File,
}

impl From<NodeType> for String {
    fn from(node_type: NodeType) -> Self {
        match node_type {
            NodeType::Room => "room".to_string(),
            NodeType::Folder => "folder".to_string(),
            NodeType::File => "file".to_string(),
        }
    }
}

impl From<&NodeType> for String {
    fn from(node_type: &NodeType) -> Self {
        match node_type {
            NodeType::Room => "room".to_string(),
            NodeType::Folder => "folder".to_string(),
            NodeType::File => "file".to_string(),
        }
    }
}

/// DRACOOON node permissions
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[allow(clippy::struct_excessive_bools)]
pub struct NodePermissions {
    pub manage: bool,
    pub read: bool,
    pub create: bool,
    pub change: bool,
    pub delete: bool,
    pub manage_download_share: bool,
    pub manage_upload_share: bool,
    pub read_recycle_bin: bool,
    pub restore_recycle_bin: bool,
    pub delete_recycle_bin: bool,
}

impl NodePermissions {
    pub fn builder() -> NodePermissionsBuilder {
        NodePermissionsBuilder::new()
    }

    pub fn new_with_edit_permissions() -> Self {
        Self {
            manage: false,
            read: true,
            create: true,
            change: true,
            delete: true,
            manage_download_share: true,
            manage_upload_share: true,
            read_recycle_bin: true,
            restore_recycle_bin: true,
            delete_recycle_bin: false,
        }
    }

    pub fn new_with_read_permissions() -> Self {
        Self {
            manage: false,
            read: true,
            create: false,
            change: false,
            delete: false,
            manage_download_share: false,
            manage_upload_share: false,
            read_recycle_bin: false,
            restore_recycle_bin: false,
            delete_recycle_bin: false,
        }
    }

    pub fn new_with_manage_permissions() -> Self {
        Self {
            manage: true,
            read: true,
            create: true,
            change: true,
            delete: true,
            manage_download_share: true,
            manage_upload_share: true,
            read_recycle_bin: true,
            restore_recycle_bin: true,
            delete_recycle_bin: true,
        }
    }
}

impl ToString for NodePermissions {
    fn to_string(&self) -> String {
        let mapping = [
            (self.manage, 'm'),
            (self.read, 'r'),
            (self.create, 'w'),
            (self.change, 'c'),
            (self.delete, 'd'),
            (self.manage_download_share, 'm'),
            (self.manage_upload_share, 'm'),
            (self.read_recycle_bin, 'r'),
            (self.restore_recycle_bin, 'r'),
            (self.delete_recycle_bin, 'd'),
        ];

        let mut perms = String::with_capacity(mapping.len());

        for (i, &(flag, ch)) in mapping.iter().enumerate() {
            perms.push(if flag { ch } else { '-' });

            // Add a dash after the "delete" permission
            if i == 4 {
                perms.push('-');
            }
        }

        perms
    }
}

/// DRACOOON encryption info (rescue keys)
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct EncryptionInfo {
    user_key_state: String,
    room_key_state: String,
    data_space_key_state: String,
}

/// DRACOON user info on nodes (`created_by`, `updated_by`)
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UserInfo {
    pub id: u64,
    pub user_type: String,
    pub avatar_uuid: String,
    pub first_name: Option<String>,
    pub last_name: Option<String>,
    pub email: Option<String>,
}

#[async_trait]
impl FromResponse for NodeList {
    /// transforms a response into a NodeList
    async fn from_response(res: Response) -> Result<Self, DracoonClientError> {
        parse_body::<Self, DracoonErrorResponse>(res).await
    }
}

/// Response for download url of a node - POST /nodes/files/{nodeId}/download
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct DownloadUrlResponse {
    pub download_url: String,
}

#[async_trait]
impl FromResponse for DownloadUrlResponse {
    /// transforms a response into a DownloadUrlResponse
    async fn from_response(res: Response) -> Result<Self, DracoonClientError> {
        parse_body::<Self, DracoonErrorResponse>(res).await
    }
}

/// Error response for S3 requests (XML)
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct S3XmlError {
    code: Option<String>,
    request_id: Option<String>,
    host_id: Option<String>,
    message: Option<String>,
    argument_name: Option<String>,
}

/// Error response for S3 requests
#[derive(Debug, PartialEq)]
pub struct S3ErrorResponse {
    pub status: StatusCode,
    pub error: S3XmlError,
}

impl Display for S3ErrorResponse {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Error: {} ({})",
            self.error
                .message
                .as_ref()
                .unwrap_or(&String::from("Unknown S3 error")),
            self.status,
        )
    }
}

impl S3ErrorResponse {
    /// transforms a `S3XmlError` into a `S3ErrorResponse`
    pub fn from_xml_error(status: StatusCode, error: S3XmlError) -> Self {
        Self { status, error }
    }
}

#[async_trait]
impl FromResponse for FileKey {
    /// transforms a response into a `FileKey`
    async fn from_response(res: Response) -> Result<Self, DracoonClientError> {
        parse_body::<Self, DracoonErrorResponse>(res).await
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateFileUploadResponse {
    pub upload_url: Option<String>,
    pub upload_id: String,
    pub token: Option<String>,
}

#[async_trait]
impl FromResponse for CreateFileUploadResponse {
    /// transforms a response into a `CreateFileUploadResponse`
    async fn from_response(res: Response) -> Result<Self, DracoonClientError> {
        parse_body::<Self, DracoonErrorResponse>(res).await
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PresignedUrl {
    pub url: String,
    pub part_number: u32,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PresignedUrlList {
    pub urls: Vec<PresignedUrl>,
}

#[async_trait]
impl FromResponse for PresignedUrlList {
    /// transforms a response into a `PresignedUrlList`
    async fn from_response(res: Response) -> Result<Self, DracoonClientError> {
        parse_body::<Self, DracoonErrorResponse>(res).await
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct S3FileUploadStatus {
    pub status: S3UploadStatus,
    pub node: Option<Node>,
    pub error_details: Option<DracoonErrorResponse>,
}

#[derive(Debug, Deserialize)]
pub enum S3UploadStatus {
    #[serde(rename = "transfer")]
    Transfer,
    #[serde(rename = "finishing")]
    Finishing,
    #[serde(rename = "done")]
    Done,
    #[serde(rename = "error")]
    Error,
}

#[async_trait]
impl FromResponse for S3FileUploadStatus {
    /// transforms a response into a `S3FileUploadStatus`
    async fn from_response(res: Response) -> Result<Self, DracoonClientError> {
        parse_body::<Self, DracoonErrorResponse>(res).await
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[allow(non_snake_case)]
pub struct CreateFileUploadRequest {
    parent_id: u64,
    name: String,
    size: Option<u64>,
    classification: Option<u8>,
    expiration: Option<ObjectExpiration>,
    direct_S3_upload: Option<bool>,
    timestamp_creation: Option<String>,
    timestamp_modification: Option<String>,
}

impl CreateFileUploadRequest {
    pub fn builder(parent_id: u64, name: String) -> CreateFileUploadRequestBuilder {
        CreateFileUploadRequestBuilder {
            parent_id,
            name,
            size: None,
            classification: None,
            expiration: None,
            direct_s3_upload: Some(true),
            timestamp_creation: None,
            timestamp_modification: None,
        }
    }
}

pub struct CreateFileUploadRequestBuilder {
    parent_id: u64,
    name: String,
    size: Option<u64>,
    classification: Option<u8>,
    expiration: Option<ObjectExpiration>,
    direct_s3_upload: Option<bool>,
    timestamp_creation: Option<String>,
    timestamp_modification: Option<String>,
}

impl CreateFileUploadRequestBuilder {
    pub fn with_size(mut self, size: u64) -> Self {
        self.size = Some(size);
        self
    }

    pub fn with_classification(mut self, classification: u8) -> Self {
        self.classification = Some(classification);
        self
    }

    pub fn with_expiration(mut self, expiration: ObjectExpiration) -> Self {
        self.expiration = Some(expiration);
        self
    }
    pub fn with_timestamp_creation(mut self, timestamp_creation: DateTime<Utc>) -> Self {
        self.timestamp_creation = Some(timestamp_creation.to_rfc3339());
        self
    }
    pub fn with_timestamp_modification(mut self, timestamp_modification: DateTime<Utc>) -> Self {
        self.timestamp_modification = Some(timestamp_modification.to_rfc3339());
        self
    }
    pub fn build(self) -> CreateFileUploadRequest {
        CreateFileUploadRequest {
            parent_id: self.parent_id,
            name: self.name,
            size: self.size,
            classification: self.classification,
            expiration: self.expiration,
            direct_S3_upload: self.direct_s3_upload,
            timestamp_creation: self.timestamp_creation,
            timestamp_modification: self.timestamp_modification,
        }
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GeneratePresignedUrlsRequest {
    size: u64,
    first_part_number: u32,
    last_part_number: u32,
}

impl GeneratePresignedUrlsRequest {
    pub fn new(size: u64, first_part_number: u32, last_part_number: u32) -> Self {
        Self {
            size,
            first_part_number,
            last_part_number,
        }
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompleteS3FileUploadRequest {
    parts: Vec<S3FileUploadPart>,
    resolution_strategy: Option<ResolutionStrategy>,
    file_name: Option<String>,
    keep_share_links: Option<bool>,
    file_key: Option<FileKey>,
}

pub struct CompleteS3FileUploadRequestBuilder {
    parts: Vec<S3FileUploadPart>,
    resolution_strategy: Option<ResolutionStrategy>,
    file_name: Option<String>,
    keep_share_links: Option<bool>,
    file_key: Option<FileKey>,
}

impl CompleteS3FileUploadRequest {
    pub fn builder(parts: Vec<S3FileUploadPart>) -> CompleteS3FileUploadRequestBuilder {
        CompleteS3FileUploadRequestBuilder {
            parts,
            resolution_strategy: None,
            file_name: None,
            keep_share_links: None,
            file_key: None,
        }
    }
}

impl CompleteS3FileUploadRequestBuilder {
    pub fn with_resolution_strategy(mut self, resolution_strategy: ResolutionStrategy) -> Self {
        self.resolution_strategy = Some(resolution_strategy);
        self
    }

    pub fn with_file_name(mut self, file_name: String) -> Self {
        self.file_name = Some(file_name);
        self
    }

    pub fn with_keep_share_links(mut self, keep_share_links: bool) -> Self {
        self.keep_share_links = Some(keep_share_links);
        self
    }

    pub fn with_file_key(mut self, file_key: FileKey) -> Self {
        self.file_key = Some(file_key);
        self
    }

    pub fn build(self) -> CompleteS3FileUploadRequest {
        CompleteS3FileUploadRequest {
            parts: self.parts,
            resolution_strategy: self.resolution_strategy,
            file_name: self.file_name,
            keep_share_links: self.keep_share_links,
            file_key: self.file_key,
        }
    }
}

#[derive(Debug, Serialize, Clone)]
pub enum ResolutionStrategy {
    #[serde(rename = "autorename")]
    AutoRename,
    #[serde(rename = "overwrite")]
    Overwrite,
    #[serde(rename = "fail")]
    Fail,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct S3FileUploadPart {
    part_number: u32,
    part_etag: String,
}

impl S3FileUploadPart {
    pub fn new(part_number: u32, part_etag: String) -> Self {
        Self {
            part_number,
            part_etag,
        }
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteNodesRequest {
    node_ids: Vec<u64>,
}

impl From<Vec<u64>> for DeleteNodesRequest {
    fn from(node_ids: Vec<u64>) -> Self {
        Self { node_ids }
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TransferNodesRequest {
    items: Vec<TransferNode>,
    resolution_strategy: Option<ResolutionStrategy>,
    keep_share_links: Option<bool>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TransferNode {
    id: u64,
    name: Option<String>,
    timestamp_creation: Option<String>,
    timestamp_modification: Option<String>,
}

impl From<u64> for TransferNode {
    fn from(node_id: u64) -> Self {
        Self {
            id: node_id,
            name: None,
            timestamp_creation: None,
            timestamp_modification: None,
        }
    }
}

impl From<Vec<u64>> for TransferNodesRequest {
    fn from(node_ids: Vec<u64>) -> Self {
        Self {
            items: node_ids.into_iter().map(std::convert::Into::into).collect(),
            resolution_strategy: None,
            keep_share_links: None,
        }
    }
}

pub struct TransferNodesRequestBuilder {
    items: Vec<TransferNode>,
    resolution_strategy: Option<ResolutionStrategy>,
    keep_share_links: Option<bool>,
}

impl TransferNodesRequest {
    pub fn builder(items: Vec<TransferNode>) -> TransferNodesRequestBuilder {
        TransferNodesRequestBuilder {
            items,
            resolution_strategy: None,
            keep_share_links: None,
        }
    }

    pub fn new_from_ids(node_ids: Vec<u64>) -> TransferNodesRequestBuilder {
        TransferNodesRequestBuilder {
            items: node_ids.into_iter().map(std::convert::Into::into).collect(),
            resolution_strategy: None,
            keep_share_links: None,
        }
    }

    pub fn with_resolution_strategy(mut self, resolution_strategy: ResolutionStrategy) -> Self {
        self.resolution_strategy = Some(resolution_strategy);
        self
    }

    pub fn with_keep_share_links(mut self, keep_share_links: bool) -> Self {
        self.keep_share_links = Some(keep_share_links);
        self
    }

    pub fn build(self) -> TransferNodesRequest {
        TransferNodesRequest {
            items: self.items,
            resolution_strategy: self.resolution_strategy,
            keep_share_links: self.keep_share_links,
        }
    }
}

pub struct TransferNodeBuilder {
    id: u64,
    name: Option<String>,
    timestamp_creation: Option<String>,
    timestamp_modification: Option<String>,
}

impl TransferNode {
    pub fn builder(id: u64) -> TransferNodeBuilder {
        TransferNodeBuilder {
            id,
            name: None,
            timestamp_creation: None,
            timestamp_modification: None,
        }
    }

    pub fn with_name(mut self, name: String) -> Self {
        self.name = Some(name);
        self
    }

    pub fn with_timestamp_creation(mut self, timestamp_creation: String) -> Self {
        self.timestamp_creation = Some(timestamp_creation);
        self
    }

    pub fn with_timestamp_modification(mut self, timestamp_modification: String) -> Self {
        self.timestamp_modification = Some(timestamp_modification);
        self
    }

    pub fn build(self) -> TransferNode {
        TransferNode {
            id: self.id,
            name: self.name,
            timestamp_creation: self.timestamp_creation,
            timestamp_modification: self.timestamp_modification,
        }
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateFolderRequest {
    name: String,
    parent_id: u64,
    notes: Option<String>,
    timestamp_creation: Option<String>,
    timestamp_modification: Option<String>,
    classification: Option<u8>,
}

pub struct CreateFolderRequestBuilder {
    name: String,
    parent_id: u64,
    notes: Option<String>,
    timestamp_creation: Option<String>,
    timestamp_modification: Option<String>,
    classification: Option<u8>,
}

impl CreateFolderRequest {
    pub fn builder(name: impl Into<String>, parent_id: u64) -> CreateFolderRequestBuilder {
        CreateFolderRequestBuilder {
            name: name.into(),
            parent_id,
            notes: None,
            timestamp_creation: None,
            timestamp_modification: None,
            classification: None,
        }
    }
}

impl CreateFolderRequestBuilder {
    pub fn with_notes(mut self, notes: impl Into<String>) -> Self {
        self.notes = Some(notes.into());
        self
    }

    pub fn with_timestamp_creation(mut self, timestamp_creation: impl Into<String>) -> Self {
        self.timestamp_creation = Some(timestamp_creation.into());
        self
    }

    pub fn with_timestamp_modification(mut self, timestamp_modification: impl Into<String>) -> Self {
        self.timestamp_modification = Some(timestamp_modification.into());
        self
    }

    pub fn with_classification(mut self, classification: u8) -> Self {
        self.classification = Some(classification);
        self
    }

    pub fn build(self) -> CreateFolderRequest {
        CreateFolderRequest {
            name: self.name,
            parent_id: self.parent_id,
            notes: self.notes,
            timestamp_creation: self.timestamp_creation,
            timestamp_modification: self.timestamp_modification,
            classification: self.classification,
        }
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateFolderRequest {
    name: Option<String>,
    notes: Option<String>,
    timestamp_creation: Option<String>,
    timestamp_modification: Option<String>,
    classification: Option<u8>,
}

pub struct UpdateFolderRequestBuilder {
    name: Option<String>,
    notes: Option<String>,
    timestamp_creation: Option<String>,
    timestamp_modification: Option<String>,
    classification: Option<u8>,
}

impl UpdateFolderRequest {
    pub fn builder() -> UpdateFolderRequestBuilder {
        UpdateFolderRequestBuilder {
            name: None,
            notes: None,
            timestamp_creation: None,
            timestamp_modification: None,
            classification: None,
        }
    }
}

impl UpdateFolderRequestBuilder {
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    pub fn with_notes(mut self, notes: impl Into<String>) -> Self {
        self.notes = Some(notes.into());
        self
    }

    pub fn with_timestamp_creation(mut self, timestamp_creation: impl Into<String>) -> Self {
        self.timestamp_creation = Some(timestamp_creation.into());
        self
    }

    pub fn with_timestamp_modification(mut self, timestamp_modification: impl Into<String>) -> Self {
        self.timestamp_modification = Some(timestamp_modification.into());
        self
    }

    pub fn with_classification(mut self, classification: u8) -> Self {
        self.classification = Some(classification);
        self
    }

    pub fn build(self) -> UpdateFolderRequest {
        UpdateFolderRequest {
            name: self.name,
            notes: self.notes,
            timestamp_creation: self.timestamp_creation,
            timestamp_modification: self.timestamp_modification,
            classification: self.classification,
        }
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserIdFileItem {
    pub user_id: u64,
    pub file_id: u64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserUserPublicKey {
    pub id: u64,
    pub public_key_container: PublicKeyContainer,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FileFileKeys {
    pub id: u64,
    pub file_key_container: FileKey,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MissingKeysResponse {
    pub range: Range,
    pub items: Vec<UserIdFileItem>,
    pub users: Vec<UserUserPublicKey>,
    pub files: Vec<FileFileKeys>,
}

#[async_trait]
impl FromResponse for MissingKeysResponse {
    async fn from_response(res: Response) -> Result<Self, DracoonClientError> {
        parse_body::<Self, DracoonErrorResponse>(res).await
    }
}

#[derive(Debug, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct UserFileKeySetBatchRequest {
    items: Vec<UserFileKeySetRequest>,
}

impl UserFileKeySetBatchRequest {
    pub fn new() -> Self {
        UserFileKeySetBatchRequest { items: Vec::new() }
    }

    pub fn add(&mut self, user_id: u64, file_id: u64, file_key: FileKey) {
        self.items
            .push(UserFileKeySetRequest::new(user_id, file_id, file_key));
    }
}

impl From<Vec<UserFileKeySetRequest>> for UserFileKeySetBatchRequest {
    fn from(items: Vec<UserFileKeySetRequest>) -> Self {
        UserFileKeySetBatchRequest { items }
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UserFileKeySetRequest {
    user_id: u64,
    file_id: u64,
    file_key: FileKey,
}

impl UserFileKeySetRequest {
    pub fn new(user_id: u64, file_id: u64, file_key: FileKey) -> Self {
        UserFileKeySetRequest {
            user_id,
            file_id,
            file_key,
        }
    }
}