drive_v3/resources/apps.rs
1use reqwest::Method;
2use drive_v3_macros::{DriveRequestBuilder, request};
3
4use super::DriveRequestBuilder;
5use crate::{objects, Credentials};
6
7#[request(
8 method=Method::GET,
9 url="https://www.googleapis.com/drive/v3/apps/{app_id}",
10 returns=objects::App
11)]
12#[derive(DriveRequestBuilder)]
13/// A request builder to get a specific app.
14pub struct GetRequest {}
15
16#[request(
17 method=Method::GET,
18 url="https://www.googleapis.com/drive/v3/apps",
19 returns=objects::AppList
20)]
21#[derive(DriveRequestBuilder)]
22/// A request builder to list a user's installed apps.
23pub struct ListRequest {
24 /// A comma-separated list of file extensions to limit returned results.
25 ///
26 /// All results within the given app query scope which can open any of
27 /// the given file extensions are included in the response. If
28 /// [`app_filter_mime_types`](ListRequest::app_filter_mime_types) are
29 /// provided as well, the result is a union of the two resulting app
30 /// lists.
31 #[drive_v3(parameter)]
32 app_filter_extensions: Option<String>,
33
34 /// A comma-separated list of file extensions to limit returned results.
35 ///
36 /// All results within the given app query scope which can open any of
37 /// the given MIME types will be included in the response. If
38 /// [`app_filter_extensions`](ListRequest::app_filter_extensions) are
39 /// provided as well, the result is a union of the two resulting app
40 /// lists.
41 #[drive_v3(parameter)]
42 app_filter_mime_types: Option<String>,
43
44 /// A language or locale code, as defined by BCP 47, with some
45 /// extensions from Unicode's LDML format.
46 #[drive_v3(parameter)]
47 language_code: Option<String>,
48}
49
50/// Provides a list of apps that a user has installed.
51///
52/// Contains information about each app's supported MIME types, file,
53/// extensions, and other details.
54///
55/// Some resource methods (such as [`apps.get`](Apps::get)) require an `app_id`.
56/// Use the [`apps.list`](Apps::list) method to retrieve the ID for an installed
57/// application.
58///
59/// # Examples:
60///
61/// List the apps for a user
62///
63/// ```no_run
64/// # use drive_v3::{Error, Credentials, Drive};
65/// #
66/// # let drive = Drive::new( &Credentials::from_file(
67/// # "../.secure-files/google_drive_credentials.json",
68/// # &["https://www.googleapis.com/auth/drive.apps.readonly"],
69/// # )? );
70/// #
71/// let app_list = drive.apps.list().execute()?;
72///
73/// if let Some(apps) = app_list.items {
74/// for app in apps {
75/// println!("{}", app);
76/// }
77/// }
78/// # Ok::<(), Error>(())
79/// ```
80#[derive(Debug, Clone, PartialEq, Eq)]
81pub struct Apps {
82 /// Credentials used to authenticate a user's access to this resource.
83 credentials: Credentials,
84}
85
86impl Apps {
87 /// Creates a new [`Apps`] resource with the given [`Credentials`].
88 pub fn new( credentials: &Credentials ) -> Self {
89 Self { credentials: credentials.clone() }
90 }
91
92 /// Gets a specific app.
93 ///
94 /// See Google's
95 /// [documentation](https://developers.google.com/drive/api/reference/rest/v3/apps/get)
96 /// for more information.
97 ///
98 /// # Requires one of the following OAuth scopes:
99 ///
100 /// - `https://www.googleapis.com/auth/docs`
101 /// - `https://www.googleapis.com/auth/drive`
102 /// - `https://www.googleapis.com/auth/drive.appdata`
103 /// - `https://www.googleapis.com/auth/drive.apps.readonly`
104 /// - `https://www.googleapis.com/auth/drive.file`
105 /// - `https://www.googleapis.com/auth/drive.metadata`
106 /// - `https://www.googleapis.com/auth/drive.metadata.readonly`
107 /// - `https://www.googleapis.com/auth/drive.readonly`
108 ///
109 /// # Examples:
110 ///
111 /// ```no_run
112 /// # use drive_v3::{Error, Credentials, Drive};
113 /// #
114 /// # let drive = Drive::new( &Credentials::from_file(
115 /// # "../.secure-files/google_drive_credentials.json",
116 /// # &["https://www.googleapis.com/auth/drive.file"],
117 /// # )? );
118 /// #
119 /// let app_id = "some-app-id";
120 ///
121 /// let app = drive.apps.get(&app_id).execute()?;
122 ///
123 /// println!("This is the info of the app:\n{}", app);
124 /// # Ok::<(), Error>(())
125 /// ```
126 pub fn get<T: AsRef<str>> ( &self, app_id: T ) -> GetRequest {
127 GetRequest::new(&self.credentials, app_id)
128 }
129
130 /// Lists a user's installed apps.
131 ///
132 /// See Google's
133 /// [documentation](https://developers.google.com/drive/api/reference/rest/v3/apps/list)
134 /// for more information.
135 ///
136 /// # Requires the following OAuth scope:
137 ///
138 /// - `https://www.googleapis.com/auth/drive.apps.readonly`
139 ///
140 /// # Examples:
141 ///
142 /// ```no_run
143 /// # use drive_v3::{Error, Credentials, Drive};
144 /// #
145 /// # let drive = Drive::new( &Credentials::from_file(
146 /// # "../.secure-files/google_drive_credentials.json",
147 /// # &["https://www.googleapis.com/auth/drive.apps.readonly"],
148 /// # )? );
149 /// #
150 /// let app_list = drive.apps.list().execute()?;
151 ///
152 /// if let Some(apps) = app_list.items {
153 /// for app in apps {
154 /// println!("{}", app);
155 /// }
156 /// }
157 /// # Ok::<(), Error>(())
158 /// ```
159 pub fn list( &self ) -> ListRequest {
160 ListRequest::new(&self.credentials)
161 }
162}
163
164#[cfg(test)]
165mod tests {
166 use super::Apps;
167 use crate::ErrorKind;
168 use crate::utils::test::{INVALID_CREDENTIALS, VALID_CREDENTIALS};
169
170 fn get_resource() -> Apps {
171 Apps::new(&VALID_CREDENTIALS)
172 }
173
174 fn get_invalid_resource() -> Apps {
175 Apps::new(&INVALID_CREDENTIALS)
176 }
177
178 #[test]
179 fn new_test() {
180 let valid_resource = get_resource();
181 let invalid_resource = get_invalid_resource();
182
183 assert_eq!( valid_resource.credentials, VALID_CREDENTIALS.clone() );
184 assert_eq!( invalid_resource.credentials, INVALID_CREDENTIALS.clone() );
185 }
186
187 #[test]
188 fn get_test() {
189 let google_docs_id = "619683526622";
190 let response = get_resource().get(&google_docs_id)
191 .execute();
192
193 assert!( response.is_ok() );
194
195 let app = response.unwrap();
196
197 assert_eq!( &app.id.unwrap(), google_docs_id );
198 assert_eq!( &app.name.unwrap(), "Google Docs" );
199 assert_eq!( &app.kind.unwrap(), "drive#app" );
200 }
201
202 #[test]
203 fn get_invalid_test() {
204 let google_docs_id = "619683526622";
205 let response = get_invalid_resource().get(&google_docs_id)
206 .execute();
207
208 assert!( response.is_err() );
209 assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
210 }
211
212 #[test]
213 fn list_test() {
214 let response = get_resource().list().execute();
215
216 assert!( response.is_ok() );
217 assert!( response.unwrap().items.is_some() );
218 }
219
220 #[test]
221 fn list_invalid_test() {
222 let response = get_invalid_resource().list()
223 .execute();
224
225 assert!( response.is_err() );
226 assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
227 }
228}