Mercurial > lbo > hg > async-google-apis
changeset 90:e031af86de5b
Generate stubs for resumable uploads
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 24 Oct 2020 21:46:19 +0200 |
parents | c260d92db27f |
children | 6d501f826123 |
files | generate/generate.py generate/templates.py |
diffstat | 2 files changed, 52 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/generate/generate.py Sat Oct 24 21:45:48 2020 +0200 +++ b/generate/generate.py Sat Oct 24 21:46:19 2020 +0200 @@ -477,7 +477,7 @@ def from_cache(apiId): try: with open(path.join('cache', apiId+'.json'), 'r') as f: - print('Found API description in cache!') + print('Found API description in cache for', apiId) return json.load(f) except Exception as e: print('Fetching description from cache failed:', e) @@ -499,7 +499,10 @@ Returns: List of API JSON documents. """ - doc = json.loads(requests.get(url).text) + doc = from_cache('_global_discovery') + if not doc: + doc = json.loads(requests.get(url).text) + to_cache('_global_discovery', doc) return [it for it in doc['items'] if (not apis or it['id'] in apis)]
--- a/generate/templates.py Sat Oct 24 21:45:48 2020 +0200 +++ b/generate/templates.py Sat Oct 24 21:46:19 2020 +0200 @@ -170,9 +170,8 @@ } else { tok = self.authenticator.token(&self.scopes).await?; } - let mut url_params = format!("?uploadType=multipart&oauth_token={token}", token=tok.as_str()); + let mut url_params = format!("?uploadType=multipart&oauth_token={token}{params}", token=tok.as_str(), params=params); - url_params.push_str(&format!("{}", params)); {{#global_params_name}} if let Some(ref api_params) = ¶ms.{{{global_params_name}}} { url_params.push_str(&format!("{}", api_params)); @@ -194,6 +193,52 @@ # base_path, rel_path_expr # params: [{param, snake_param}] # http_method +ResumableUploadMethodTmpl = ''' +/// {{{description}}} +/// +/// This method is a variant of `{{{name}}}()`, taking data for upload. +pub async fn {{{name}}}_resumable_upload<'client>( + &'client mut self, params: &{{{param_type}}}, {{#in_type}}req: &{{{in_type}}}{{/in_type}}) -> Result<ResumableUpload<'client, {{{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=resumable&oauth_token={token}{params}", token=tok.as_str(), params=params); + {{#global_params_name}} + if let Some(ref api_params) = ¶ms.{{{global_params_name}}} { + url_params.push_str(&format!("{}", api_params)); + } + {{/global_params_name}} + + let full_uri = path + &url_params; + + let opt_request: Option<EmptyRequest> = None; + {{#in_type}} + let opt_request = Some(req); + {{/in_type}} + let (_resp, headers): (EmptyResponse, hyper::HeaderMap) = do_request_with_headers(&self.client, &full_uri, &[], "{{{http_method}}}", opt_request).await?; + if let Some(dest) = headers.get(hyper::header::LOCATION) { + use std::convert::TryFrom; + Ok(ResumableUpload::new(hyper::Uri::try_from(dest.to_str()?)?, &self.client, 256*1024)) + } else { + Err(Error::from(ApiError::RedirectError(format!("Resumable upload response didn't contain Location: {:?}", headers))) + .context(format!("{:?}", headers)))? + } + } +''' + +# Takes: +# name, param_type, in_type, out_type +# base_path, rel_path_expr +# params: [{param, snake_param}] +# http_method DownloadMethodTmpl = ''' /// {{{description}}} ///