Mercurial > lbo > hg > async-google-apis
view generate/templates.py @ 35:5138eadcd649
Generate a scopes enum
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Mon, 19 Oct 2020 10:21:11 +0200 |
parents | 9e8c954489f0 |
children | 77c979f1c242 |
line wrap: on
line source
# General imports and error type. RustHeader = ''' //! 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}; use std::collections::HashMap; use tokio::stream::{Stream, StreamExt}; use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; pub type TlsConnr = hyper_rustls::HttpsConnector<hyper::client::HttpConnector>; pub type TlsClient = hyper::Client<TlsConnr, hyper::Body>; pub type Authenticator = yup_oauth2::authenticator::Authenticator<TlsConnr>; #[derive(Debug, Clone)] pub enum ApiError { InputDataError(String), HTTPError(hyper::StatusCode), } impl std::error::Error for ApiError {} impl std::fmt::Display for ApiError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Debug::fmt(self, f) } } ''' # 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}}} {{{name}}}, {{/scopes}} } impl std::convert::AsRef<str> for {{{name}}}Scopes { fn as_ref(&self) -> &'static str { match self { {{#scopes}} {{{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 mut scopes = &self.scopes; if scopes.is_empty() { scopes = &vec![{{#scopes}}"{{{scope}}}".to_string(), {{/scopes}}]; } let 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 reqb = hyper::Request::builder() .uri(full_uri) .method("{{{http_method}}}") .header("Content-Type", "application/json"); let body = hyper::Body::from(""); {{#in_type}} let mut body_str = serde_json::to_string(req)?; if body_str == "null" { body_str.clear(); } let body = hyper::Body::from(body_str); {{/in_type}} let request = reqb.body(body)?; let resp = self.client.request(request).await?; if !resp.status().is_success() { return Err(anyhow::Error::new(ApiError::HTTPError(resp.status()))); } let resp_body = hyper::body::to_bytes(resp.into_body()).await?; let bodystr = String::from_utf8(resp_body.to_vec())?; let decoded = serde_json::from_str(&bodystr)?; Ok(decoded) } ''' # 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 = 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; let reqb = hyper::Request::builder() .uri(full_uri) .method("{{{http_method}}}") .header("Content-Length", data.len()); let body = hyper::Body::from(data); let request = reqb.body(body)?; let resp = self.client.request(request).await?; if !resp.status().is_success() { return Err(anyhow::Error::new(ApiError::HTTPError(resp.status()))); } let resp_body = hyper::body::to_bytes(resp.into_body()).await?; let bodystr = String::from_utf8(resp_body.to_vec())?; let decoded = serde_json::from_str(&bodystr)?; Ok(decoded) } ''' # 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}}{{{in_type}}},{{/in_type}} dst: &mut std::io::Write) -> Result<{{out_type}}> { let rel_path = {{{rel_path_expr}}}; let path = "{{{base_path}}}".to_string() + &rel_path; let mut scopes = &self.scopes; if scopes.is_empty() { scopes = &vec![{{#scopes}}"{{{scope}}}".to_string(), {{/scopes}}]; } let 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 reqb = hyper::Request::builder() .uri(full_uri) .method("{{{http_method}}}") .header("Content-Type", "application/json"); let body = hyper::Body::from(""); {{#in_type}} let mut body_str = serde_json::to_string(req)?; if body_str == "null" { body_str.clear(); } let body = hyper::Body::from(body_str); {{/in_type}} let request = reqb.body(body)?; let resp = self.client.request(request).await?; if !resp.status().is_success() { return Err(anyhow::Error::new(ApiError::HTTPError(resp.status()))); } let resp_body = resp.into_body(); let write_result = resp_body.map(move |chunk| { dst.write(chunk?.as_ref()) .map(|_| ()) .map_err(Error::from) }) .collect::<Vec<Result<()>>>().await; if let Some(e) = write_result.into_iter().find(|r| r.is_err()) { return e; } Ok(()) } '''