drive_v3/resources/
drives.rs

1use reqwest::Method;
2use drive_v3_macros::{DriveRequestBuilder, request};
3
4use super::DriveRequestBuilder;
5use crate::{objects, Credentials};
6
7#[request(
8    method=Method::POST,
9    url="https://www.googleapis.com/drive/v3/drives",
10    returns=objects::DriveInfo,
11)]
12#[derive(DriveRequestBuilder)]
13/// A request builder to create a shared drive.
14pub struct CreateRequest {
15    /// An ID, such as a random UUID, which uniquely identifies this user's
16    /// request for idempotent creation of a shared drive.
17    ///
18    /// A repeated request by the same user and with the same request ID
19    /// will avoid creating duplicates by attempting to create the same
20    /// shared drive. If the shared drive already exists a `409` error will
21    /// be returned.
22    #[drive_v3(parameter)]
23    request_id: Option<String>,
24
25    /// The Drive to be created
26    #[drive_v3(body)]
27    drive_info: Option<objects::DriveInfo>
28}
29
30#[request(
31    method=Method::DELETE,
32    url="https://www.googleapis.com/drive/v3/drives/{drive_id}",
33    returns=(),
34)]
35#[derive(DriveRequestBuilder)]
36/// A request builder to delete a shared drive.
37pub struct DeleteRequest {
38    /// Issue the request as a domain administrator.
39    ///
40    /// If set to `true`, then the requester will be granted access if they
41    /// are an administrator of the domain to which the shared drive
42    /// belongs.
43    #[drive_v3(parameter)]
44    use_domain_admin_access: Option<bool>,
45
46    /// Whether any items inside the shared drive should also be deleted.
47    ///
48    /// This option is only supported when
49    /// [`use_domain_admin_access`](DeleteRequest::use_domain_admin_access)
50    /// is also set to `true`.
51    #[drive_v3(parameter)]
52    allow_item_deletion: Option<bool>,
53}
54
55#[request(
56    method=Method::GET,
57    url="https://www.googleapis.com/drive/v3/drives/{drive_id}",
58    returns=objects::DriveInfo,
59)]
60#[derive(DriveRequestBuilder)]
61/// A request builder to get a shared drive.
62pub struct GetRequest {
63    /// Issue the request as a domain administrator.
64    ///
65    /// If set to `true`, then the requester will be granted access if they
66    /// are an administrator of the domain to which the shared drive
67    /// belongs.
68    #[drive_v3(parameter)]
69    use_domain_admin_access: Option<bool>,
70}
71
72#[request(
73    method=Method::POST,
74    url="https://www.googleapis.com/drive/v3/drives/{drive_id}/hide",
75    returns=objects::DriveInfo,
76)]
77#[derive(DriveRequestBuilder)]
78/// A request builder to hide a shared drive from the default view.
79pub struct HideRequest {}
80
81#[request(
82    method=Method::GET,
83    url="https://www.googleapis.com/drive/v3/drives",
84    returns=objects::DriveInfoList,
85)]
86#[derive(DriveRequestBuilder)]
87/// A request builder to list the user's shared drives.
88pub struct ListRequest {
89    /// Maximum number of shared drives to return per page.
90    #[drive_v3(parameter)]
91    page_size: Option<i64>,
92
93    /// Page token for shared drives.
94    #[drive_v3(parameter)]
95    page_token: Option<String>,
96
97    /// Query string for searching shared drives.
98    #[drive_v3(parameter)]
99    q: Option<String>,
100
101    /// Issue the request as a domain administrator.
102    ///
103    /// If set to `true`, then the requester will be granted access if they
104    /// are an administrator of the domain to which the shared drive
105    /// belongs.
106    #[drive_v3(parameter)]
107    use_domain_admin_access: Option<bool>,
108}
109
110#[request(
111    method=Method::POST,
112    url="https://www.googleapis.com/drive/v3/drives/{drive_id}/unhide",
113    returns=objects::DriveInfo,
114)]
115#[derive(DriveRequestBuilder)]
116/// A request builder to restore a shared drive to the default view.
117pub struct UnhideRequest {}
118
119#[request(
120    method=Method::PATCH,
121    url="https://www.googleapis.com/drive/v3/drives/{drive_id}",
122    returns=objects::DriveInfo,
123)]
124#[derive(DriveRequestBuilder)]
125/// A request builder to updates the metadata for a shared drive.
126pub struct UpdateRequest {
127    /// Issue the request as a domain administrator.
128    ///
129    /// if set to `true`, then the requester will be granted access if they
130    /// are an administrator of the domain to which the shared drive
131    /// belongs.
132    #[drive_v3(parameter)]
133    use_domain_admin_access: Option<bool>,
134
135    /// The Drive to be created
136    #[drive_v3(body)]
137    drive_info: Option<objects::DriveInfo>,
138}
139
140/// Information of a shared drive.
141///
142/// Some resource methods (such as [`drives.update`](Drives::update)) require a
143/// `drive_id`. Use the [`drives.list`](Drives::list) method to retrieve the ID
144/// for a shared drive.
145///
146/// # Examples
147///
148/// List the shared Drives of a user
149///
150/// ```no_run
151/// # use drive_v3::{Error, Credentials, Drive};
152/// #
153/// # let drive = Drive::new( &Credentials::from_file(
154/// #     "../.secure-files/google_drive_credentials.json",
155/// #     &["https://www.googleapis.com/auth/drive.file"],
156/// # )? );
157/// #
158/// let drive_list = drive.drives.list()
159///     .page_size(10)
160///     .q("name = 'drive_im_looking_for'") // search for specific drives
161///     .execute()?;
162///
163/// if let Some(drives) = drive_list.drives {
164///     for drive in drives {
165///         println!("{}", drive);
166///     }
167/// }
168/// # Ok::<(), Error>(())
169/// ```
170#[derive(Debug, Clone, PartialEq, Eq)]
171pub struct Drives {
172    /// Credentials used to authenticate a user's access to this resource.
173    credentials: Credentials,
174}
175
176impl Drives {
177    /// Creates a new [`Drives`] resource with the given [`Credentials`].
178    pub fn new( credentials: &Credentials ) -> Self {
179        Self { credentials: credentials.clone() }
180    }
181
182    /// Creates a shared drive.
183    ///
184    /// See Google's
185    /// [documentation](https://developers.google.com/drive/api/reference/rest/v3/drives/create)
186    /// for more information.
187    ///
188    /// # Requires the following OAuth scope:
189    ///
190    /// - `https://www.googleapis.com/auth/drive`
191    ///
192    /// # Examples:
193    ///
194    /// ```no_run
195    /// use drive_v3::objects::DriveInfo;
196    /// # use drive_v3::{Error, Credentials, Drive};
197    /// #
198    /// # let drive = Drive::new( &Credentials::from_file(
199    /// #     "../.secure-files/google_drive_credentials.json",
200    /// #     &["https://www.googleapis.com/auth/drive.file"],
201    /// # )? );
202    ///
203    /// let drive_info = DriveInfo {
204    ///     name: Some( "drive-name".to_string() ),
205    ///     ..Default::default()
206    /// };
207    ///
208    /// let created_drive = drive.drives.create()
209    ///     .drive_info(&drive_info)
210    ///     .execute()?;
211    ///
212    /// assert_eq!(created_drive.name, drive_info.name);
213    /// # Ok::<(), Error>(())
214    /// ```
215    pub fn create( &self ) -> CreateRequest {
216        CreateRequest::new(&self.credentials)
217    }
218
219    /// Permanently deletes a shared drive for which the user is an organizer.
220    ///
221    /// The shared drive cannot contain any untrashed items.
222    ///
223    /// See Google's
224    /// [documentation](https://developers.google.com/drive/api/reference/rest/v3/drives/delete)
225    /// for more information.
226    ///
227    /// # Requires the following OAuth scope:
228    ///
229    /// - `https://www.googleapis.com/auth/drive`
230    ///
231    /// # Examples:
232    ///
233    /// ```no_run
234    /// # use drive_v3::{Error, Credentials, Drive};
235    /// #
236    /// # let drive = Drive::new( &Credentials::from_file(
237    /// #     "../.secure-files/google_drive_credentials.json",
238    /// #     &["https://www.googleapis.com/auth/drive.file"],
239    /// # )? );
240    /// #
241    /// let drive_id = "some-drive-id";
242    /// let response = drive.drives.delete(&drive_id).execute();
243    ///
244    /// assert!( response.is_ok() );
245    /// # Ok::<(), Error>(())
246    /// ```
247    pub fn delete<T: AsRef<str>> ( &self, drive_id: T ) -> DeleteRequest {
248        DeleteRequest::new(&self.credentials, drive_id)
249    }
250
251    /// Gets a shared drive's metadata by ID.
252    ///
253    /// See Google's
254    /// [documentation](https://developers.google.com/drive/api/reference/rest/v3/drives/get)
255    /// for more information.
256    ///
257    /// # Requires one of the following OAuth scopes:
258    ///
259    /// - `https://www.googleapis.com/auth/drive`
260    /// - `https://www.googleapis.com/auth/drive.readonly`
261    ///
262    /// # Examples:
263    ///
264    /// ```no_run
265    /// # use drive_v3::{Error, Credentials, Drive};
266    /// #
267    /// # let drive = Drive::new( &Credentials::from_file(
268    /// #     "../.secure-files/google_drive_credentials.json",
269    /// #     &["https://www.googleapis.com/auth/drive.file"],
270    /// # )? );
271    /// #
272    /// let drive_id = "some-drive-id";
273    /// let drive_info = drive.drives.get(&drive_id).execute()?;
274    ///
275    /// println!("This is the Drive's information:\n{}", drive_info);
276    /// # Ok::<(), Error>(())
277    /// ```
278    pub fn get<T: AsRef<str>> ( &self, drive_id: T ) -> GetRequest {
279        GetRequest::new(&self.credentials, drive_id)
280    }
281
282    /// Hides a shared drive from the default view.
283    ///
284    /// See Google's
285    /// [documentation](https://developers.google.com/drive/api/reference/rest/v3/drives/hide)
286    /// for more information.
287    ///
288    /// # Requires the following OAuth scope:
289    ///
290    /// - `https://www.googleapis.com/auth/drive`
291    ///
292    /// # Examples:
293    ///
294    /// ```no_run
295    /// # use drive_v3::{Error, Credentials, Drive};
296    /// #
297    /// # let drive = Drive::new( &Credentials::from_file(
298    /// #     "../.secure-files/google_drive_credentials.json",
299    /// #     &["https://www.googleapis.com/auth/drive.file"],
300    /// # )? );
301    /// #
302    /// let drive_id = "some-drive-id";
303    /// let drive_info = drive.drives.hide(&drive_id).execute()?;
304    ///
305    /// assert!( drive_info.hidden.unwrap() );
306    /// # Ok::<(), Error>(())
307    /// ```
308    pub fn hide<T: AsRef<str>> ( &self, drive_id: T ) -> HideRequest {
309        HideRequest::new(&self.credentials, drive_id)
310    }
311
312    /// Lists the user's shared drives.
313    ///
314    /// See Google's
315    /// [documentation](https://developers.google.com/drive/api/reference/rest/v3/drives/list)
316    /// for more information.
317    ///
318    /// # Note:
319    ///
320    /// This method accepts the [`q`](ListRequest::q) parameter, which is a
321    /// search query combining one or more search terms. For more information,
322    /// see Google's
323    /// [Search for shared drives](https://developers.google.com/drive/api/guides/search-shareddrives)
324    /// guide.
325    ///
326    /// # Requires one of the following OAuth scopes:
327    ///
328    /// - `https://www.googleapis.com/auth/drive`
329    /// - `https://www.googleapis.com/auth/drive.readonly`
330    ///
331    /// # Examples:
332    ///
333    /// ```no_run
334    /// # use drive_v3::{Error, Credentials, Drive};
335    /// #
336    /// # let drive = Drive::new( &Credentials::from_file(
337    /// #     "../.secure-files/google_drive_credentials.json",
338    /// #     &["https://www.googleapis.com/auth/drive.file"],
339    /// # )? );
340    /// #
341    /// let drive_list = drive.drives.list()
342    ///     .page_size(10)
343    ///     .q("name = 'drive_im_looking_for'") // search for specific drives
344    ///     .execute()?;
345    ///
346    /// if let Some(drives) = drive_list.drives {
347    ///     for drive in drives {
348    ///         println!("{}", drive);
349    ///     }
350    /// }
351    /// # Ok::<(), Error>(())
352    /// ```
353    pub fn list( &self ) -> ListRequest {
354        ListRequest::new(&self.credentials)
355    }
356
357    /// Restores a shared drive to the default view.
358    ///
359    /// See Google's
360    /// [documentation](https://developers.google.com/drive/api/reference/rest/v3/drives/unhide)
361    /// for more information.
362    ///
363    /// # Requires the following OAuth scope:
364    ///
365    /// - `https://www.googleapis.com/auth/drive`
366    ///
367    /// # Examples:
368    ///
369    /// ```no_run
370    /// # use drive_v3::{Error, Credentials, Drive};
371    /// #
372    /// # let drive = Drive::new( &Credentials::from_file(
373    /// #     "../.secure-files/google_drive_credentials.json",
374    /// #     &["https://www.googleapis.com/auth/drive.file"],
375    /// # )? );
376    /// #
377    /// let drive_id = "some-drive-id";
378    /// let drive_info = drive.drives.unhide(&drive_id).execute()?;
379    ///
380    /// assert!( !drive_info.hidden.unwrap() );
381    /// # Ok::<(), Error>(())
382    /// ```
383    pub fn unhide<T: AsRef<str>> ( &self, drive_id: T ) -> UnhideRequest {
384        UnhideRequest::new(&self.credentials, drive_id)
385    }
386
387    /// Updates the metadata for a shared drive.
388    ///
389    /// See Google's
390    /// [documentation](https://developers.google.com/drive/api/reference/rest/v3/drives/update)
391    /// for more information.
392    ///
393    /// # Requires the following OAuth scope:
394    ///
395    /// - `https://www.googleapis.com/auth/drive`
396    ///
397    /// # Examples:
398    ///
399    /// ```no_run
400    /// use drive_v3::objects::DriveInfo;
401    /// # use drive_v3::{Error, Credentials, Drive};
402    /// #
403    /// # let drive = Drive::new( &Credentials::from_file(
404    /// #     "../.secure-files/google_drive_credentials.json",
405    /// #     &["https://www.googleapis.com/auth/drive.file"],
406    /// # )? );
407    ///
408    /// let updated_drive_info = DriveInfo {
409    ///     name: Some( "updated-drive-name".to_string() ),
410    ///     ..Default::default()
411    /// };
412    ///
413    /// let drive_id = "some-drive-id";
414    ///
415    /// let drive_info = drive.drives.update(&drive_id)
416    ///     .drive_info(&updated_drive_info)
417    ///     .execute()?;
418    ///
419    /// assert_eq!(drive_info.name, updated_drive_info.name);
420    /// # Ok::<(), Error>(())
421    /// ```
422    pub fn update<T: AsRef<str>> ( &self, drive_id: T ) -> UpdateRequest {
423        UpdateRequest::new(&self.credentials, drive_id)
424    }
425}
426
427#[cfg(test)]
428mod tests {
429    use super::Drives;
430    use crate::ErrorKind;
431    use crate::utils::test::{INVALID_CREDENTIALS, VALID_CREDENTIALS};
432
433    fn get_resource() -> Drives {
434        Drives::new(&VALID_CREDENTIALS)
435    }
436
437    fn get_invalid_resource() -> Drives {
438        Drives::new(&INVALID_CREDENTIALS)
439    }
440
441    #[test]
442    fn new_test() {
443        let valid_resource = get_resource();
444        let invalid_resource = get_invalid_resource();
445
446        assert_eq!( valid_resource.credentials, VALID_CREDENTIALS.clone() );
447        assert_eq!( invalid_resource.credentials, INVALID_CREDENTIALS.clone() );
448    }
449
450    #[test]
451    fn create_invalid_test() {
452        let response = get_invalid_resource().create()
453            .execute();
454
455        assert!( response.is_err() );
456        assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
457    }
458
459    #[test]
460    fn delete_invalid_test() {
461        let response = get_invalid_resource().delete("invalid-id")
462            .execute();
463
464        assert!( response.is_err() );
465        assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
466    }
467
468    #[test]
469    fn get_invalid_test() {
470        let response = get_invalid_resource().get("invalid-id")
471            .execute();
472
473        assert!( response.is_err() );
474        assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
475    }
476
477    #[test]
478    fn hide_invalid_test() {
479        let response = get_invalid_resource().hide("invalid-id")
480            .execute();
481
482        assert!( response.is_err() );
483        assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
484    }
485
486    #[test]
487    fn list_test() {
488        let response = get_resource().list()
489            .execute();
490
491        assert!( response.is_ok() );
492    }
493
494    #[test]
495    fn list_invalid_test() {
496        let response = get_invalid_resource().list()
497            .execute();
498
499        assert!( response.is_err() );
500        assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
501    }
502
503    #[test]
504    fn unhide_invalid_test() {
505        let response = get_invalid_resource().unhide("invalid-id")
506            .execute();
507
508        assert!( response.is_err() );
509        assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
510    }
511
512    #[test]
513    fn update_invalid_test() {
514        let response = get_invalid_resource().update("invalid-id")
515            .execute();
516
517        assert!( response.is_err() );
518        assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
519    }
520}