changeset 34:9e8c954489f0

Enable sharing of Authenticators.
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 18 Oct 2020 23:47:29 +0200
parents d1a1a533c6be
children 5138eadcd649
files drive_example/src/drive_v3_types.rs drive_example/src/main.rs generate/templates.py
diffstat 3 files changed, 79 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/drive_example/src/drive_v3_types.rs	Sun Oct 18 21:24:36 2020 +0200
+++ b/drive_example/src/drive_v3_types.rs	Sun Oct 18 23:47:29 2020 +0200
@@ -1,4 +1,12 @@
 
+//! This file was generated by async-google-apis. (https://github.com/dermesser/async-google-apis)
+//!
+//! (c) 2020 Lewin Bormann <lbo@spheniscida.de>
+//!
+//! I'd be happy if you let me know about your use case of this code.
+//!
+//! THIS FILE HAS BEEN GENERATED -- SAVE ANY MODIFICATIONS BEFORE REPLACING.
+
 use serde::{Deserialize, Serialize};
 use chrono::{DateTime, Utc};
 use anyhow::{Error, Result};
@@ -993,7 +1001,7 @@
     #[serde(rename = "teamDriveId")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub team_drive_id: Option<String>,
-    /// A short-lived link to the file's thumbnail, if available. Typically lasts on the order of hours. Only populated when the requesting app can access the file's content. If the file isn't shared publicly, the URL returned in Files.thumbnailLink must be fetched using a credentialed request.
+    /// A short-lived link to the file's thumbnail, if available. Typically lasts on the order of hours. Only populated when the requesting app can access the file's content.
     #[serde(rename = "thumbnailLink")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub thumbnail_link: Option<String>,
@@ -2286,14 +2294,16 @@
 
 pub struct AboutService {
   client: TlsClient,
-  authenticator: Authenticator,
+  authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
   scopes: Vec<String>,
 }
 
 impl AboutService {
-  /// Create a new AboutService object.
-  pub fn new(client: TlsClient, auth: Authenticator) -> AboutService {
-    AboutService { client: client, authenticator: auth, scopes: vec![] }
+  /// Create a new AboutService object. The easiest way to call this is wrapping the Authenticator
+  /// into an Rc: new(client.clone(), Rc::new(authenticator)).
+  /// This way, one authenticator can be shared among several services.
+  pub fn new<A: 'static + std::ops::Deref<Target=Authenticator>>(client: TlsClient, auth: A) -> AboutService {
+    AboutService { client: client, authenticator: Box::new(auth), scopes: vec![] }
   }
 
   /// Explicitly select which scopes should be requested for authorization. Otherwise,
@@ -2346,14 +2356,16 @@
 
 pub struct ChangesService {
   client: TlsClient,
-  authenticator: Authenticator,
+  authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
   scopes: Vec<String>,
 }
 
 impl ChangesService {
-  /// Create a new ChangesService object.
-  pub fn new(client: TlsClient, auth: Authenticator) -> ChangesService {
-    ChangesService { client: client, authenticator: auth, scopes: vec![] }
+  /// Create a new ChangesService object. The easiest way to call this is wrapping the Authenticator
+  /// into an Rc: new(client.clone(), Rc::new(authenticator)).
+  /// This way, one authenticator can be shared among several services.
+  pub fn new<A: 'static + std::ops::Deref<Target=Authenticator>>(client: TlsClient, auth: A) -> ChangesService {
+    ChangesService { client: client, authenticator: Box::new(auth), scopes: vec![] }
   }
 
   /// Explicitly select which scopes should be requested for authorization. Otherwise,
@@ -2605,14 +2617,16 @@
 
 pub struct ChannelsService {
   client: TlsClient,
-  authenticator: Authenticator,
+  authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
   scopes: Vec<String>,
 }
 
 impl ChannelsService {
-  /// Create a new ChannelsService object.
-  pub fn new(client: TlsClient, auth: Authenticator) -> ChannelsService {
-    ChannelsService { client: client, authenticator: auth, scopes: vec![] }
+  /// Create a new ChannelsService object. The easiest way to call this is wrapping the Authenticator
+  /// into an Rc: new(client.clone(), Rc::new(authenticator)).
+  /// This way, one authenticator can be shared among several services.
+  pub fn new<A: 'static + std::ops::Deref<Target=Authenticator>>(client: TlsClient, auth: A) -> ChannelsService {
+    ChannelsService { client: client, authenticator: Box::new(auth), scopes: vec![] }
   }
 
   /// Explicitly select which scopes should be requested for authorization. Otherwise,
@@ -2670,14 +2684,16 @@
 
 pub struct CommentsService {
   client: TlsClient,
-  authenticator: Authenticator,
+  authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
   scopes: Vec<String>,
 }
 
 impl CommentsService {
-  /// Create a new CommentsService object.
-  pub fn new(client: TlsClient, auth: Authenticator) -> CommentsService {
-    CommentsService { client: client, authenticator: auth, scopes: vec![] }
+  /// Create a new CommentsService object. The easiest way to call this is wrapping the Authenticator
+  /// into an Rc: new(client.clone(), Rc::new(authenticator)).
+  /// This way, one authenticator can be shared among several services.
+  pub fn new<A: 'static + std::ops::Deref<Target=Authenticator>>(client: TlsClient, auth: A) -> CommentsService {
+    CommentsService { client: client, authenticator: Box::new(auth), scopes: vec![] }
   }
 
   /// Explicitly select which scopes should be requested for authorization. Otherwise,
@@ -2893,14 +2909,16 @@
 
 pub struct DrivesService {
   client: TlsClient,
-  authenticator: Authenticator,
+  authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
   scopes: Vec<String>,
 }
 
 impl DrivesService {
-  /// Create a new DrivesService object.
-  pub fn new(client: TlsClient, auth: Authenticator) -> DrivesService {
-    DrivesService { client: client, authenticator: auth, scopes: vec![] }
+  /// Create a new DrivesService object. The easiest way to call this is wrapping the Authenticator
+  /// into an Rc: new(client.clone(), Rc::new(authenticator)).
+  /// This way, one authenticator can be shared among several services.
+  pub fn new<A: 'static + std::ops::Deref<Target=Authenticator>>(client: TlsClient, auth: A) -> DrivesService {
+    DrivesService { client: client, authenticator: Box::new(auth), scopes: vec![] }
   }
 
   /// Explicitly select which scopes should be requested for authorization. Otherwise,
@@ -3183,14 +3201,16 @@
 
 pub struct FilesService {
   client: TlsClient,
-  authenticator: Authenticator,
+  authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
   scopes: Vec<String>,
 }
 
 impl FilesService {
-  /// Create a new FilesService object.
-  pub fn new(client: TlsClient, auth: Authenticator) -> FilesService {
-    FilesService { client: client, authenticator: auth, scopes: vec![] }
+  /// Create a new FilesService object. The easiest way to call this is wrapping the Authenticator
+  /// into an Rc: new(client.clone(), Rc::new(authenticator)).
+  /// This way, one authenticator can be shared among several services.
+  pub fn new<A: 'static + std::ops::Deref<Target=Authenticator>>(client: TlsClient, auth: A) -> FilesService {
+    FilesService { client: client, authenticator: Box::new(auth), scopes: vec![] }
   }
 
   /// Explicitly select which scopes should be requested for authorization. Otherwise,
@@ -3921,14 +3941,16 @@
 
 pub struct PermissionsService {
   client: TlsClient,
-  authenticator: Authenticator,
+  authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
   scopes: Vec<String>,
 }
 
 impl PermissionsService {
-  /// Create a new PermissionsService object.
-  pub fn new(client: TlsClient, auth: Authenticator) -> PermissionsService {
-    PermissionsService { client: client, authenticator: auth, scopes: vec![] }
+  /// Create a new PermissionsService object. The easiest way to call this is wrapping the Authenticator
+  /// into an Rc: new(client.clone(), Rc::new(authenticator)).
+  /// This way, one authenticator can be shared among several services.
+  pub fn new<A: 'static + std::ops::Deref<Target=Authenticator>>(client: TlsClient, auth: A) -> PermissionsService {
+    PermissionsService { client: client, authenticator: Box::new(auth), scopes: vec![] }
   }
 
   /// Explicitly select which scopes should be requested for authorization. Otherwise,
@@ -4230,14 +4252,16 @@
 
 pub struct RepliesService {
   client: TlsClient,
-  authenticator: Authenticator,
+  authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
   scopes: Vec<String>,
 }
 
 impl RepliesService {
-  /// Create a new RepliesService object.
-  pub fn new(client: TlsClient, auth: Authenticator) -> RepliesService {
-    RepliesService { client: client, authenticator: auth, scopes: vec![] }
+  /// Create a new RepliesService object. The easiest way to call this is wrapping the Authenticator
+  /// into an Rc: new(client.clone(), Rc::new(authenticator)).
+  /// This way, one authenticator can be shared among several services.
+  pub fn new<A: 'static + std::ops::Deref<Target=Authenticator>>(client: TlsClient, auth: A) -> RepliesService {
+    RepliesService { client: client, authenticator: Box::new(auth), scopes: vec![] }
   }
 
   /// Explicitly select which scopes should be requested for authorization. Otherwise,
@@ -4449,14 +4473,16 @@
 
 pub struct RevisionsService {
   client: TlsClient,
-  authenticator: Authenticator,
+  authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
   scopes: Vec<String>,
 }
 
 impl RevisionsService {
-  /// Create a new RevisionsService object.
-  pub fn new(client: TlsClient, auth: Authenticator) -> RevisionsService {
-    RevisionsService { client: client, authenticator: auth, scopes: vec![] }
+  /// Create a new RevisionsService object. The easiest way to call this is wrapping the Authenticator
+  /// into an Rc: new(client.clone(), Rc::new(authenticator)).
+  /// This way, one authenticator can be shared among several services.
+  pub fn new<A: 'static + std::ops::Deref<Target=Authenticator>>(client: TlsClient, auth: A) -> RevisionsService {
+    RevisionsService { client: client, authenticator: Box::new(auth), scopes: vec![] }
   }
 
   /// Explicitly select which scopes should be requested for authorization. Otherwise,
@@ -4635,14 +4661,16 @@
 
 pub struct TeamdrivesService {
   client: TlsClient,
-  authenticator: Authenticator,
+  authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
   scopes: Vec<String>,
 }
 
 impl TeamdrivesService {
-  /// Create a new TeamdrivesService object.
-  pub fn new(client: TlsClient, auth: Authenticator) -> TeamdrivesService {
-    TeamdrivesService { client: client, authenticator: auth, scopes: vec![] }
+  /// Create a new TeamdrivesService object. The easiest way to call this is wrapping the Authenticator
+  /// into an Rc: new(client.clone(), Rc::new(authenticator)).
+  /// This way, one authenticator can be shared among several services.
+  pub fn new<A: 'static + std::ops::Deref<Target=Authenticator>>(client: TlsClient, auth: A) -> TeamdrivesService {
+    TeamdrivesService { client: client, authenticator: Box::new(auth), scopes: vec![] }
   }
 
   /// Explicitly select which scopes should be requested for authorization. Otherwise,
--- a/drive_example/src/main.rs	Sun Oct 18 21:24:36 2020 +0200
+++ b/drive_example/src/main.rs	Sun Oct 18 23:47:29 2020 +0200
@@ -9,6 +9,7 @@
 mod drive_v3_types;
 use drive_v3_types as drive;
 
+use std::rc::Rc;
 use std::fs;
 use std::path::Path;
 
@@ -61,6 +62,7 @@
 
 #[tokio::main]
 async fn main() {
+    let https = https_client();
     // Put your client secret in the working directory!
     let sec = yup_oauth2::read_application_secret("client_secret.json")
         .await
@@ -70,13 +72,13 @@
         yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect,
     )
     .persist_tokens_to_disk("tokencache.json")
+    .hyper_client(https.clone())
     .build()
     .await
     .expect("InstalledFlowAuthenticator failed to build");
 
     let scopes = &["https://www.googleapis.com/auth/drive"];
-    let https = https_client();
-    let mut cl = drive::FilesService::new(https, auth);
+    let mut cl = drive::FilesService::new(https, Rc::new(auth));
     cl.set_scopes(scopes);
 
     let arg = std::env::args().skip(1).next();
--- a/generate/templates.py	Sun Oct 18 21:24:36 2020 +0200
+++ b/generate/templates.py	Sun Oct 18 23:47:29 2020 +0200
@@ -55,14 +55,16 @@
 ServiceImplementationTmpl = '''
 pub struct {{{service}}}Service {
   client: TlsClient,
-  authenticator: Authenticator,
+  authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
   scopes: Vec<String>,
 }
 
 impl {{{service}}}Service {
-  /// Create a new {{service}}Service object.
-  pub fn new(client: TlsClient, auth: Authenticator) -> {{service}}Service {
-    {{{service}}}Service { client: client, authenticator: auth, scopes: vec![] }
+  /// Create a new {{service}}Service object. The easiest way to call this is wrapping the Authenticator
+  /// into an Rc: new(client.clone(), Rc::new(authenticator)).
+  /// This way, one authenticator can be shared among several services.
+  pub fn new<A: 'static + std::ops::Deref<Target=Authenticator>>(client: TlsClient, auth: A) -> {{service}}Service {
+    {{{service}}}Service { client: client, authenticator: Box::new(auth), scopes: vec![] }
   }
 
   /// Explicitly select which scopes should be requested for authorization. Otherwise,