changeset 105:2f22414d6cea

Amend drive_example to demonstrate downloads too.
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 25 Oct 2020 08:53:53 +0100
parents 981f7beb9db3
children 4dc7d2a7ff71
files drive_example/src/drive_v3_types.rs drive_example/src/main.rs
diffstat 2 files changed, 58 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/drive_example/src/drive_v3_types.rs	Sun Oct 25 08:09:46 2020 +0100
+++ b/drive_example/src/drive_v3_types.rs	Sun Oct 25 08:53:53 2020 +0100
@@ -4095,15 +4095,11 @@
 
 
 /// Exports a Google Doc to the requested MIME type and returns the exported content. Please note that the exported content is limited to 10MB.
-///
-/// This method downloads data.
 pub async fn export(
-    &mut self, params: &FilesExportParams,  dst: &mut dyn std::io::Write)
-    -> Result<()> {
+    &mut self, params: &FilesExportParams) -> Result<()> {
 
     let rel_path = format!("files/{fileId}/export", fileId=params.file_id);
     let path = "https://www.googleapis.com/drive/v3/".to_string() + &rel_path;
-
     let tok;
     if self.scopes.is_empty() {
         let scopes = &["https://www.googleapis.com/auth/drive.readonly".to_string(),
@@ -4118,10 +4114,11 @@
     }
 
     let full_uri = path + &url_params;
+
     let opt_request: Option<EmptyRequest> = None;
-
-    do_download(&self.client, &full_uri, &[(hyper::header::AUTHORIZATION, format!("Bearer {token}", token=tok.as_str()))],
-        "GET", opt_request, dst).await
+    do_request(&self.client, &full_uri,
+        &[(hyper::header::AUTHORIZATION, format!("Bearer {token}", token=tok.as_str()))],
+        "GET", opt_request).await
   }
 
 
@@ -4154,11 +4151,15 @@
 
 
 /// Gets a file's metadata or content by ID.
+///
+/// This method downloads data.
 pub async fn get(
-    &mut self, params: &FilesGetParams) -> Result<File> {
+    &mut self, params: &FilesGetParams,  dst: Option<&mut dyn std::io::Write>)
+    -> Result<DownloadResponse<File>> {
 
     let rel_path = format!("files/{fileId}", fileId=params.file_id);
     let path = "https://www.googleapis.com/drive/v3/".to_string() + &rel_path;
+
     let tok;
     if self.scopes.is_empty() {
         let scopes = &["https://www.googleapis.com/auth/drive.readonly".to_string(),
@@ -4173,11 +4174,10 @@
     }
 
     let full_uri = path + &url_params;
-
     let opt_request: Option<EmptyRequest> = None;
-    do_request(&self.client, &full_uri,
-        &[(hyper::header::AUTHORIZATION, format!("Bearer {token}", token=tok.as_str()))],
-        "GET", opt_request).await
+
+    do_download(&self.client, &full_uri, &[(hyper::header::AUTHORIZATION, format!("Bearer {token}", token=tok.as_str()))],
+        "GET", opt_request, dst).await
   }
 
 
@@ -4311,11 +4311,15 @@
 
 
 /// Subscribes to changes to a file
+///
+/// This method downloads data.
 pub async fn watch(
-    &mut self, params: &FilesWatchParams, req: &Channel) -> Result<Channel> {
+    &mut self, params: &FilesWatchParams, req: &Channel, dst: Option<&mut dyn std::io::Write>)
+    -> Result<DownloadResponse<Channel>> {
 
     let rel_path = format!("files/{fileId}/watch", fileId=params.file_id);
     let path = "https://www.googleapis.com/drive/v3/".to_string() + &rel_path;
+
     let tok;
     if self.scopes.is_empty() {
         let scopes = &["https://www.googleapis.com/auth/drive.readonly".to_string(),
@@ -4330,12 +4334,11 @@
     }
 
     let full_uri = path + &url_params;
-
     let opt_request: Option<EmptyRequest> = None;
     let opt_request = Some(req);
-    do_request(&self.client, &full_uri,
-        &[(hyper::header::AUTHORIZATION, format!("Bearer {token}", token=tok.as_str()))],
-        "POST", opt_request).await
+
+    do_download(&self.client, &full_uri, &[(hyper::header::AUTHORIZATION, format!("Bearer {token}", token=tok.as_str()))],
+        "POST", opt_request, dst).await
   }
 
 }
@@ -4727,11 +4730,15 @@
 
 
 /// Gets a revision's metadata or content by ID.
+///
+/// This method downloads data.
 pub async fn get(
-    &mut self, params: &RevisionsGetParams) -> Result<Revision> {
+    &mut self, params: &RevisionsGetParams,  dst: Option<&mut dyn std::io::Write>)
+    -> Result<DownloadResponse<Revision>> {
 
     let rel_path = format!("files/{fileId}/revisions/{revisionId}", fileId=params.file_id,revisionId=params.revision_id);
     let path = "https://www.googleapis.com/drive/v3/".to_string() + &rel_path;
+
     let tok;
     if self.scopes.is_empty() {
         let scopes = &["https://www.googleapis.com/auth/drive.readonly".to_string(),
@@ -4746,11 +4753,10 @@
     }
 
     let full_uri = path + &url_params;
-
     let opt_request: Option<EmptyRequest> = None;
-    do_request(&self.client, &full_uri,
-        &[(hyper::header::AUTHORIZATION, format!("Bearer {token}", token=tok.as_str()))],
-        "GET", opt_request).await
+
+    do_download(&self.client, &full_uri, &[(hyper::header::AUTHORIZATION, format!("Bearer {token}", token=tok.as_str()))],
+        "GET", opt_request, dst).await
   }
 
 
--- a/drive_example/src/main.rs	Sun Oct 25 08:09:46 2020 +0100
+++ b/drive_example/src/main.rs	Sun Oct 25 08:53:53 2020 +0100
@@ -59,11 +59,38 @@
     let mut params = drive::FilesGetParams::default();
     params.file_id = file_id.clone();
     params.drive_params = Some(general_params.clone());
+    params.drive_params.as_mut().unwrap().alt = Some("media".into());
+    let mut dest = vec![0 as u8; 128];
 
-    let get_file = cl.get(&params).await?;
+    // Get file contents.
+    let get_file = cl.get(&params, Some(&mut dest)).await?;
     println!("{:?}", get_file);
 
-    assert!(get_file.name == Some(fname.to_string()));
+    // get() returns a response type (Response(...)) if the server returned an application/json
+    // response, otherwise it will download to a Writer. If you don't supply a Writer and there is
+    // data to download, an error is returned (ApiError::DataAvailableError).
+    match get_file {
+        common::DownloadResponse::Response(_) => {
+            // We want download! No metadata.
+            assert!(false);
+        }
+        common::DownloadResponse::Downloaded => println!("Download success."),
+    }
+
+    // Get file metadata.
+    let mut params = drive::FilesGetParams::default();
+    params.file_id = file_id.clone();
+    let get_file = cl.get(&params, None).await?;
+    println!("{:?}", get_file);
+
+    match get_file {
+        common::DownloadResponse::Response(gf) => {
+            println!("Metadata success.");
+            assert!(gf.name == Some(fname.to_string()));
+        }
+        // We want metadata! No download.
+        common::DownloadResponse::Downloaded => assert!(false),
+    }
     Ok(())
 }