drive_v3/resources/
about.rs

1use reqwest::Method;
2use drive_v3_macros::{DriveRequestBuilder, request};
3
4use super::DriveRequestBuilder;
5use crate::{Credentials, objects};
6
7/// A request builder to get information about the user, the user's Drive,
8/// and system capabilities.
9#[request(
10    method=Method::GET,
11    url="https://www.googleapis.com/drive/v3/about",
12    returns=objects::About
13)]
14#[derive(DriveRequestBuilder)]
15pub struct GetRequest {}
16
17/// Information about the user, the user's Drive, and system capabilities.
18///
19/// # Examples:
20///
21/// ```no_run
22/// use drive_v3::{Credentials, Drive};
23/// # use drive_v3::Error;
24///
25/// let credentials_path = "my_credentials.json";
26/// let scopes = ["https://www.googleapis.com/auth/drive.metadata.readonly"];
27///
28/// let credentials = Credentials::from_file(&credentials_path, &scopes)?;
29/// let drive = Drive::new(&credentials);
30///
31/// let my_information = drive.about.get()
32///     .fields("storageQuota, canCreateDrives, appInstalled")
33///     .execute()?;
34///
35/// println!("look at all this information:\n{}", my_information);
36/// # Ok::<(), Error>(())
37/// ```
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct About {
40    /// Credentials used to authenticate a user's access to this resource.
41    credentials: Credentials,
42}
43
44impl About {
45    /// Creates a new [`About`] resource with the given [`Credentials`].
46    pub fn new( credentials: &Credentials ) -> Self {
47        Self { credentials: credentials.clone() }
48    }
49
50    /// Gets information about the user, the user's Drive, and system capabilities.
51    ///
52    /// See Google's [documentation](https://developers.google.com/drive/api/reference/rest/v3/about/get) for more information.
53    ///
54    /// # Note:
55    ///
56    /// This request requires you to set the [`fields`](GetRequest::fields) parameter.
57    ///
58    /// # Requires one of the following OAuth scopes:
59    ///
60    /// - `https://www.googleapis.com/auth/drive`
61    /// - `https://www.googleapis.com/auth/drive.appdata`
62    /// - `https://www.googleapis.com/auth/drive.file`
63    /// - `https://www.googleapis.com/auth/drive.metadata`
64    /// - `https://www.googleapis.com/auth/drive.metadata.readonly`
65    /// - `https://www.googleapis.com/auth/drive.photos.readonly`
66    /// - `https://www.googleapis.com/auth/drive.readonly`
67    ///
68    /// # Examples:
69    ///
70    /// ```no_run
71    /// use drive_v3::{Credentials, Drive};
72    /// # use drive_v3::Error;
73    ///
74    /// let credentials_path = "my_credentials.json";
75    /// let scopes = ["https://www.googleapis.com/auth/drive.metadata.readonly"];
76    ///
77    /// let credentials = Credentials::from_file(&credentials_path, &scopes)?;
78    /// let drive = Drive::new(&credentials);
79    ///
80    /// let my_information = drive.about.get()
81    ///     .fields("storageQuota, canCreateDrives, appInstalled")
82    ///     .execute()?;
83    ///
84    /// println!("look at all this information:\n{}", my_information);
85    /// # Ok::<(), Error>(())
86    /// ```
87    pub fn get( &self ) -> GetRequest {
88        GetRequest::new(&self.credentials)
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use reqwest::Method;
95
96    use super::About;
97    use crate::ErrorKind;
98    use crate::utils::test::{VALID_CREDENTIALS, INVALID_CREDENTIALS};
99
100    fn get_resource() -> About {
101        About::new(&VALID_CREDENTIALS)
102    }
103
104    fn get_invalid_resource() -> About {
105        About::new(&INVALID_CREDENTIALS)
106    }
107
108    #[test]
109    fn new_test() {
110        let credentials = VALID_CREDENTIALS.clone();
111        let resource = About::new(&credentials);
112
113        assert_eq!(resource.credentials, credentials);
114    }
115
116    #[test]
117    fn get_test() {
118        let about_request = get_resource().get();
119
120        assert_eq!(about_request.method, Method::GET);
121        assert_eq!(&about_request.url, "https://www.googleapis.com/drive/v3/about");
122    }
123
124    #[test]
125    fn fields_test() {
126        let about_request = get_resource().get()
127            .fields("test-fields");
128
129        assert_eq!( about_request.fields, Some("test-fields".into()) );
130    }
131
132    #[test]
133    fn execute_test() {
134        let response = get_resource().get()
135            .fields("kind")
136            .execute();
137
138        assert!( response.is_ok() );
139    }
140
141    #[test]
142    fn execute_invalid_credentials_test() {
143        let response = get_invalid_resource().get()
144            .fields("kind")
145            .execute();
146
147        assert!( response.is_err() );
148        assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
149    }
150}