Mercurial > lbo > hg > async-google-apis
view generate/templates.py @ 50:b76f414038e2 common_v0.1.0
Removed tag common
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Mon, 19 Oct 2020 14:37:41 +0200 |
parents | 2b6d7966e827 |
children | e6204806a74e |
line wrap: on
line source
# General imports and error type. RustHeader = ''' #![allow(unused_variables, unused_mut, dead_code)] //! 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 async_google_apis_common::*; ''' # Dict contents -- # name (of API, Capitalized) # scopes: [{name, url, desc}] OauthScopesType = ''' /// Scopes of this API. Convertible to their string representation with `AsRef`. #[derive(Debug, Clone, Copy)] pub enum {{{name}}}Scopes { {{#scopes}} /// {{{desc}}} /// /// URL: {{{url}}} {{{scope_name}}}, {{/scopes}} } impl std::convert::AsRef<str> for {{{name}}}Scopes { fn as_ref(&self) -> &'static str { match self { {{#scopes}} {{{name}}}Scopes::{{{scope_name}}} => "{{{url}}}", {{/scopes}} } } } ''' # A struct for parameters or input/output API types. # Dict contents -- # name # fields: [{name, comment, attr, typ}] ResourceStructTmpl = ''' #[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct {{{name}}} { {{#fields}} {{#comment}} /// {{{comment}}} {{/comment}} {{#attr}} {{{attr}}} {{/attr}} pub {{{name}}}: {{{typ}}}, {{/fields}} } ''' # Dict contents -- # service (e.g. Files) # methods: [{text}] # name (API name) ServiceImplementationTmpl = ''' pub struct {{{service}}}Service { client: TlsClient, authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>, scopes: Vec<String>, } impl {{{service}}}Service { /// 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, /// a possibly too large scope will be requested. /// /// It is most convenient to supply a vec or slice of {{{name}}}Scopes enum values. pub fn set_scopes<S: AsRef<str>, T: AsRef<[S]>>(&mut self, scopes: T) { self.scopes = scopes.as_ref().into_iter().map(|s| s.as_ref().to_string()).collect(); } {{#methods}} {{{text}}} {{/methods}} } ''' # Takes dict contents: # name, description, param_type, in_type, out_type # base_path, rel_path_expr, scopes (string repr. of rust string array), # params: [{param, snake_param}] # http_method NormalMethodTmpl = ''' /// {{{description}}} pub async fn {{{name}}}( &mut self, params: &{{{param_type}}}{{#in_type}}, req: &{{{in_type}}}{{/in_type}}) -> Result<{{{out_type}}}> { let rel_path = {{{rel_path_expr}}}; let path = "{{{base_path}}}".to_string() + &rel_path; let tok; if self.scopes.is_empty() { let scopes = &[{{#scopes}}"{{{scope}}}".to_string(), {{/scopes}}]; tok = self.authenticator.token(scopes).await?; } else { tok = self.authenticator.token(&self.scopes).await?; } let mut url_params = format!("?oauth_token={token}&fields=*", token=tok.as_str()); {{#params}} if let Some(ref val) = ¶ms.{{{snake_param}}} { url_params.push_str(&format!("&{{{param}}}={}", percent_encode(format!("{}", val).as_bytes(), NON_ALPHANUMERIC).to_string())); } {{/params}} {{#required_params}} url_params.push_str(&format!("&{{{param}}}={}", percent_encode(format!("{}", params.{{{snake_param}}}).as_bytes(), NON_ALPHANUMERIC).to_string())); {{/required_params}} let full_uri = path + &url_params; let opt_request: Option<EmptyRequest> = None; {{#in_type}} let opt_request = Some(req); {{/in_type}} do_request(&self.client, &full_uri, &[], "{{{http_method}}}", opt_request).await } ''' # Takes: # name, param_type, in_type, out_type # base_path, rel_path_expr # params: [{param, snake_param}] # http_method UploadMethodTmpl = ''' /// {{{description}}} pub async fn {{{name}}}_upload( &mut self, params: &{{{param_type}}}, data: hyper::body::Bytes) -> Result<{{out_type}}> { let rel_path = {{{rel_path_expr}}}; let path = "{{{base_path}}}".to_string() + &rel_path; let tok; if self.scopes.is_empty() { let scopes = &[{{#scopes}}"{{{scope}}}".to_string(), {{/scopes}}]; tok = self.authenticator.token(scopes).await?; } else { tok = self.authenticator.token(&self.scopes).await?; } let mut url_params = format!("?uploadType=media&oauth_token={token}&fields=*", token=tok.as_str()); {{#params}} if let Some(ref val) = ¶ms.{{{snake_param}}} { url_params.push_str(&format!("&{{{param}}}={}", percent_encode(format!("{}", val).as_bytes(), NON_ALPHANUMERIC).to_string())); } {{/params}} {{#required_params}} url_params.push_str(&format!("&{{{param}}}={}", percent_encode(format!("{}", params.{{{snake_param}}}).as_bytes(), NON_ALPHANUMERIC).to_string())); {{/required_params}} let full_uri = path + &url_params; do_upload(&self.client, &full_uri, &[], "{{{http_method}}}", data).await } ''' # Takes: # name, param_type, in_type, out_type # base_path, rel_path_expr # params: [{param, snake_param}] # http_method DownloadMethodTmpl = ''' /// {{{description}}} pub async fn {{{name}}}( &mut self, params: &{{{param_type}}}, {{#in_type}}req: {{{in_type}}},{{/in_type}} dst: &mut dyn std::io::Write) -> Result<{{out_type}}> { let rel_path = {{{rel_path_expr}}}; let path = "{{{base_path}}}".to_string() + &rel_path; let tok; if self.scopes.is_empty() { let scopes = &[{{#scopes}}"{{{scope}}}".to_string(), {{/scopes}}]; tok = self.authenticator.token(scopes).await?; } else { tok = self.authenticator.token(&self.scopes).await?; } let mut url_params = format!("?oauth_token={token}&fields=*", token=tok.as_str()); {{#params}} if let Some(ref val) = ¶ms.{{{snake_param}}} { url_params.push_str(&format!("&{{{param}}}={}", percent_encode(format!("{}", val).as_bytes(), NON_ALPHANUMERIC).to_string())); } {{/params}} {{#required_params}} url_params.push_str(&format!("&{{{param}}}={}", percent_encode(format!("{}", params.{{{snake_param}}}).as_bytes(), NON_ALPHANUMERIC).to_string())); {{/required_params}} let full_uri = path + &url_params; let opt_request: Option<EmptyRequest> = None; {{#in_type}} let opt_request = Some(req); {{/in_type}} do_download(&self.client, &full_uri, &[], "{{{http_method}}}", opt_request, dst).await } '''