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
use reqwest::Method;
use std::collections::HashMap;
use crate::{objects, request_builder, Credentials, Error, ErrorKind};
request_builder!(
/// A request builder to get the starting `pageToken` for listing future
/// changes.
pub GetStartPageTokenRequest {
/// The ID of the shared drive for which the starting pageToken for
/// listing future changes from that shared drive will be returned.
drive_id: Option<String>,
/// Whether the requesting application supports both My Drives and
/// shared drives.
supports_all_drives: Option<bool>,
},
// HTTP Method
Method::GET,
// API endpoint
("https://www.googleapis.com/drive/v3/changes/startPageToken"),
);
impl GetStartPageTokenRequest {
/// Executes this request.
///
/// # Errors:
///
/// - a [`UrlParsing`](crate::ErrorKind::UrlParsing) error, if the creation
/// of the request's URL failed.
/// - a [`Request`](crate::ErrorKind::Request) error, if unable to send the
/// request or get a body from the response.
/// - a [`Response`](crate::ErrorKind::Response) error, if the request
/// returned an error response.
/// - a [`Json`](crate::ErrorKind::Json) error, if unable to parse the
/// response's body.
pub fn execute( &self ) -> Result<String, Error> {
let response = self.send()?;
let response_text = response.text()?;
let parsed_json = serde_json::from_str::<HashMap<&str, String>> (&response_text)?;
match parsed_json.get("startPageToken") {
Some(token) => Ok( token.clone() ),
#[cfg(not(tarpaulin_include))]
None => Err( Error {
kind: ErrorKind::Json,
message: "the response did not contain the 'startPageToken' field".into(),
} )
}
}
}
request_builder!(
/// A request builder to list the changes for a user or shared drive.
pub ListRequest {
/// The shared drive from which changes will be returned.
///
/// If specified the change IDs will be reflective of the shared drive;
/// use the combined drive ID and change ID as an identifier.
drive_id: Option<String>,
/// Whether changes should include the file resource if the file is
/// still accessible by the user at the time of the request, even when
/// a file was removed from the list of changes and there will be no
/// further change entries for this file.
include_corpus_removals: Option<bool>,
/// Whether both My Drive and shared drive items should be included in
/// results.
include_items_from_all_drives: Option<bool>,
/// Whether to include changes indicating that items have been removed
/// from the list of changes, for example by deletion or loss of access.
include_removed: Option<bool>,
/// The maximum number of changes to return per page.
page_size: Option<i64>,
/// The token for continuing a previous list request on the next page.
///
/// This should be set to the value of
/// [`next_page_token`](objects::ChangeList::next_page_token) from the
/// previous response or to the response from the
/// [`get_start_page_token`](Changes::get_start_page_token) method.
page_token: Option<String>,
/// Whether to restrict the results to changes inside the My Drive
/// hierarchy.
///
/// This omits changes to files such as those in the Application Data
/// folder or shared files which have not been added to My Drive.
restrict_to_my_drive: Option<bool>,
/// A comma-separated list of spaces to query within the corpora.
///
/// Supported values are `drive` and `appDataFolder`.
spaces: Option<String>,
/// Whether the requesting application supports both My Drives and
/// shared drives.
supports_all_drives: Option<bool>,
/// Specifies which additional view's permissions to include in the
/// response.
///
/// Only `published` is supported.
include_permissions_for_view: Option<String>,
/// A comma-separated list of IDs of labels to include in the
/// [`label_info`](objects::File::label_info) part of the response.
include_labels: Option<String>,
},
// HTTP Method
Method::GET,
// API endpoint
("https://www.googleapis.com/drive/v3/changes"),
);
impl ListRequest {
/// Executes this request.
///
/// # Errors:
///
/// - a [`UrlParsing`](crate::ErrorKind::UrlParsing) error, if the creation
/// of the request's URL failed.
/// - a [`Request`](crate::ErrorKind::Request) error, if unable to send the
/// request or get a body from the response.
/// - a [`Response`](crate::ErrorKind::Response) error, if the request
/// returned an error response.
/// - a [`Json`](crate::ErrorKind::Json) error, if unable to parse the
/// response's body.
pub fn execute( &self ) -> Result<objects::ChangeList, Error> {
let response = self.send()?;
Ok( serde_json::from_str( &response.text()? )? )
}
}
request_builder!(
/// A request builder for subscribing to changes for a user.
pub WatchRequest {
/// The shared drive from which changes will be returned.
///
/// If specified the change IDs will be reflective of the shared drive;
/// use the combined drive ID and change ID as an identifier.
drive_id: Option<String>,
/// Whether changes should include the file resource if the file is
/// still accessible by the user at the time of the request, even when
/// a file was removed from the list of changes and there will be no
/// further change entries for this file.
include_corpus_removals: Option<bool>,
/// Whether both My Drive and shared drive items should be included in
/// results.
include_items_from_all_drives: Option<bool>,
/// Whether to include changes indicating that items have been removed
/// from the list of changes, for example by deletion or loss of access.
include_removed: Option<bool>,
/// The maximum number of changes to return per page.
page_size: Option<i64>,
/// The token for continuing a previous list request on the next page.
///
/// This should be set to the value of
/// [`next_page_token`](objects::ChangeList::next_page_token) from the
/// previous response or to the response from the
/// [`get_start_page_token`](Changes::get_start_page_token) method.
page_token: Option<String>,
/// Whether to restrict the results to changes inside the My Drive
/// hierarchy.
///
/// This omits changes to files such as those in the Application Data
/// folder or shared files which have not been added to My Drive.
restrict_to_my_drive: Option<bool>,
/// A comma-separated list of spaces to query within the corpora.
///
/// Supported values are `drive` and `appDataFolder`.
spaces: Option<String>,
/// Whether the requesting application supports both My Drives and
/// shared drives.
supports_all_drives: Option<bool>,
/// Specifies which additional view's permissions to include in the
/// response.
///
/// Only `published` is supported.
include_permissions_for_view: Option<String>,
/// A comma-separated list of IDs of labels to include in the
/// [`label_info`](objects::File::label_info) part of the response.
include_labels: Option<String>,
},
// HTTP Method
Method::POST,
// API endpoint
("https://www.googleapis.com/drive/v3/changes/watch"),
// Other fields
/// Sets the metadata that the channel will have.
channel: Option<objects::Channel>
);
impl WatchRequest {
/// Executes this request.
///
/// # Errors
///
/// - a [`UrlParsing`](ErrorKind::UrlParsing) error, if the creation of the
/// request URL failed.
/// - a [`Json`](ErrorKind::Json) error, if unable to parse the destination
/// file to JSON.
/// - a [`Request`](ErrorKind::Request) error, if unable to send the request
/// or get a body from the response.
/// - a [`Response`](ErrorKind::Response) error, if the request returned an
/// error response.
pub fn execute( &self ) -> Result<objects::Channel, Error> {
let channel_metadata = self.channel.clone().unwrap_or_default();
let request = self.build()?
.body( serde_json::to_string(&channel_metadata)? );
let response = request.send()?;
if !response.status().is_success() {
return Err( response.into() )
}
Ok( serde_json::from_str( &response.text()? )? )
}
}
/// A change to a file or shared drive.
///
/// # Examples:
///
/// List changes in a drive
///
/// ```no_run
/// # use drive_v3::{Error, Credentials, Drive};
/// #
/// # let drive = Drive::new( &Credentials::from_file(
/// # ".secure-files/google_drive_credentials.json",
/// # &["https://www.googleapis.com/auth/drive.file"],
/// # )? );
/// #
/// let drive_id = "some-drive-id";
/// let page_token = "some-page-token";
///
/// let change_list = drive.changes.list()
/// .page_size(10)
/// .drive_id(drive_id)
/// .page_token(page_token)
/// .execute()?;
///
/// if let Some(changes) = change_list.changes {
/// for change in changes {
/// println!("{}", change);
/// }
/// }
/// # Ok::<(), Error>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Changes {
/// Credentials used to authenticate a user's access to this resource.
credentials: Credentials,
}
impl Changes {
/// Creates a new [`Changes`] resource with the given [`Credentials`].
pub fn new( credentials: &Credentials ) -> Self {
Self { credentials: credentials.clone() }
}
/// Gets the starting `pageToken` for listing future changes.
///
/// The starting page token is used for listing future changes. The page
/// token doesn't expire.
///
/// See Google's
/// [documentation](https://developers.google.com/drive/api/reference/rest/v3/changes/getStartPageToken)
/// for more information.
///
/// # Note:
///
/// This request requires you to set the
/// [`drive_id`](GetStartPageTokenRequest::drive_id) and
/// [`supports_all_drives`](GetStartPageTokenRequest::supports_all_drives)
/// parameters.
///
/// # Requires one of the following OAuth scopes:
///
/// - `https://www.googleapis.com/auth/drive`
/// - `https://www.googleapis.com/auth/drive.appdata`
/// - `https://www.googleapis.com/auth/drive.file`
/// - `https://www.googleapis.com/auth/drive.metadata`
/// - `https://www.googleapis.com/auth/drive.metadata.readonly`
/// - `https://www.googleapis.com/auth/drive.photos.readonly`
/// - `https://www.googleapis.com/auth/drive.readonly`
///
/// # Examples:
///
/// ```no_run
/// # use drive_v3::{Error, Credentials, Drive};
/// #
/// # let drive = Drive::new( &Credentials::from_file(
/// # ".secure-files/google_drive_credentials.json",
/// # &["https://www.googleapis.com/auth/drive.file"],
/// # )? );
/// #
/// let drive_id = "some-drive-id";
///
/// let start_page_token = drive.changes.get_start_page_token()
/// .drive_id(drive_id)
/// .supports_all_drives(true)
/// .execute()?;
///
/// println!("Your start page token is: {}", start_page_token);
/// # Ok::<(), Error>(())
/// ```
pub fn get_start_page_token( &self ) -> GetStartPageTokenRequest {
GetStartPageTokenRequest::new(&self.credentials)
}
/// Lists the changes for a user or shared drive.
///
/// See Google's
/// [documentation](https://developers.google.com/drive/api/reference/rest/v3/changes/list)
/// for more information.
///
/// # Note:
///
/// This request requires you to set the
/// [`drive_id`](ListRequest::drive_id) and
/// [`page_token`](ListRequest::page_token)
/// parameters.
///
/// # Requires one of the following OAuth scopes:
///
/// - `https://www.googleapis.com/auth/drive`
/// - `https://www.googleapis.com/auth/drive.appdata`
/// - `https://www.googleapis.com/auth/drive.file`
/// - `https://www.googleapis.com/auth/drive.metadata`
/// - `https://www.googleapis.com/auth/drive.metadata.readonly`
/// - `https://www.googleapis.com/auth/drive.photos.readonly`
/// - `https://www.googleapis.com/auth/drive.readonly`
///
/// # Examples:
///
/// ```no_run
/// # use drive_v3::{Error, Credentials, Drive};
/// #
/// # let drive = Drive::new( &Credentials::from_file(
/// # ".secure-files/google_drive_credentials.json",
/// # &["https://www.googleapis.com/auth/drive.file"],
/// # )? );
/// #
/// let drive_id = "some-drive-id";
/// let page_token = "some-page-token";
///
/// let change_list = drive.changes.list()
/// .page_size(10)
/// .drive_id(drive_id)
/// .page_token(page_token)
/// .execute()?;
///
/// if let Some(changes) = change_list.changes {
/// for change in changes {
/// println!("{}", change);
/// }
/// }
/// # Ok::<(), Error>(())
/// ```
pub fn list( &self ) -> ListRequest {
ListRequest::new(&self.credentials)
}
/// Subscribes to changes for a user.
///
/// See Google's
/// [documentation](https://developers.google.com/drive/api/reference/rest/v3/changes/watch)
/// for more information.
///
/// # Note:
///
/// This request requires you to set the
/// [`drive_id`](ListRequest::drive_id) and
/// [`page_token`](ListRequest::page_token)
/// parameters.
///
/// # Note
///
/// In order to subscribe to changes, you must provide a
/// [`Channel`](objects::Channel) with an `id` and an `address` which is the
/// one that will receive the notifications. This can be done by creating a
/// channel using [`from`](objects::Channel::from).
///
/// For more informChangesation on channels, see Google's
/// [documentation](https://developers.google.com/drive/api/guides/push).
///
/// # Requires one of the following OAuth scopes:
///
/// - `https://www.googleapis.com/auth/drive`
/// - `https://www.googleapis.com/auth/drive.appdata`
/// - `https://www.googleapis.com/auth/drive.file`
/// - `https://www.googleapis.com/auth/drive.metadata`
/// - `https://www.googleapis.com/auth/drive.metadata.readonly`
/// - `https://www.googleapis.com/auth/drive.photos.readonly`
/// - `https://www.googleapis.com/auth/drive.readonly`
///
/// # Examples:
///
/// ```no_run
/// use drive_v3::objects::Channel;
/// # use drive_v3::{Error, Credentials, Drive};
/// #
/// # let drive = Drive::new( &Credentials::from_file(
/// # ".secure-files/google_drive_credentials.json",
/// # &["https://www.googleapis.com/auth/drive.file"],
/// # )? );
///
/// let channel_id = "my-channel-id";
/// let channel_address = "https://mydomain.com/channel-notifications";
/// let channel = Channel::from(&channel_id, &channel_address);
///
/// let drive_id = "some-drive-id";
/// let page_token = "some-page-token";
///
/// let created_channel = drive.changes.watch()
/// .drive_id(drive_id)
/// .page_token(page_token)
/// .channel(&channel)
/// .execute()?;
///
/// println!("this is the created channel:\n{}", created_channel);
/// # Ok::<(), Error>(())
/// ```
pub fn watch( &self ) -> WatchRequest {
WatchRequest::new(&self.credentials)
}
}
#[cfg(test)]
mod tests {
use super::Changes;
use crate::{objects, ErrorKind};
use crate::utils::test::{INVALID_CREDENTIALS, VALID_CREDENTIALS};
fn get_resource() -> Changes {
Changes::new(&VALID_CREDENTIALS)
}
fn get_invalid_resource() -> Changes {
Changes::new(&INVALID_CREDENTIALS)
}
#[test]
fn new_test() {
let valid_resource = get_resource();
let invalid_resource = get_invalid_resource();
assert_eq!( valid_resource.credentials, VALID_CREDENTIALS.clone() );
assert_eq!( invalid_resource.credentials, INVALID_CREDENTIALS.clone() );
}
#[test]
fn get_start_page_token_invalid_response_test() {
let response = get_invalid_resource().get_start_page_token()
.drive_id("test-drive-id")
.supports_all_drives(true)
.execute();
assert!( response.is_err() );
assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
}
#[test]
fn list_invalid_response_test() {
let response = get_invalid_resource().list()
.drive_id("test-drive-id")
.page_token("test-page-token")
.execute();
assert!( response.is_err() );
assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
}
#[test]
fn watch_invalid_response_test() {
let channel = objects::Channel::from("test-channel-id", "test-address");
let response = get_invalid_resource().watch()
.drive_id("test-drive-id")
.page_token("test-page-token")
.channel(&channel)
.execute();
assert!( response.is_err() );
assert_eq!( response.unwrap_err().kind, ErrorKind::Response );
}
}