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
use serde::{Deserialize, Serialize};
use crate::handlers::{upload, download, delete, search, find};
use crate::models::client::file_upload::{DriveFileUpload, DriveFolderUpload};
use crate::models::server::file_upload_response::FileUploadResponse;
use crate::models::client::error::{GoogleClientError};
use crate::models::client::file_search::DriveFileSearch;
use crate::models::server::file_list::DriveFileList;
use crate::models::client::file_download::FileDownloadResponse;
use crate::models::server::file::DriveFile;
use std::time::Duration;
use reqwest::blocking::{Client, ClientBuilder};
use crate::models::client::chunk_size::ChunkSize;
use reqwest::Proxy;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct GoogleDriveClient {
params: GoogleDriveClientParams
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct GoogleDriveClientParams {
pub request_timeout: Duration,
pub chunk_size: Option<ChunkSize>,
pub proxy: Option<String>
}
impl GoogleDriveClient {
pub fn upload_file(self, drive_file: DriveFileUpload) -> Result<FileUploadResponse, GoogleClientError> {
let chunk_size = self.params.chunk_size.clone();
let client = self.setup_client();
upload::handle_file_upload(client, &drive_file, chunk_size)
}
pub fn download_file(self, file_id: &String) -> Result<FileDownloadResponse, GoogleClientError> {
download::handle_file_download(self.setup_client(), file_id)
}
pub fn create_folder(self, drive_folder: DriveFolderUpload) -> Result<String, GoogleClientError> {
upload::handle_create_folder(self.setup_client(), &drive_folder)
}
pub fn delete_file_or_folder(self, file_or_folder_id: &String) -> Result<(), GoogleClientError> {
delete::delete_file_or_folder(self.setup_client(), file_or_folder_id)
}
pub fn search(self, search: DriveFileSearch) -> Result<DriveFileList, GoogleClientError> {
search::handle_search(self.setup_client(), search)
}
pub fn find(self, file_id: String) -> Result<Option<DriveFile>, GoogleClientError> {
find::handle_find(self.setup_client(), file_id)
}
fn setup_client(self) -> Client {
if self.params.proxy.is_some() {
let proxy: String = self.params.proxy.unwrap();
ClientBuilder::new()
.proxy(Proxy::all(proxy.as_str()).unwrap())
.timeout(self.params.request_timeout)
.build().unwrap()
} else {
ClientBuilder::new().timeout(self.params.request_timeout).build().unwrap()
}
}
pub fn new(params: GoogleDriveClientParams) -> GoogleDriveClient {
GoogleDriveClient { params }
}
}
impl Default for GoogleDriveClient {
fn default() -> Self {
GoogleDriveClient {
params: GoogleDriveClientParams {
request_timeout: Duration::from_secs(60),
chunk_size: Some(ChunkSize::default()),
proxy: None
}
}
}
}