changeset 103:7aa7ac3ae82d

Only generate supported upload methods
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 25 Oct 2020 08:03:07 +0100
parents b89456fe2f2f
children 981f7beb9db3
files generate/generate.py generate/templates.py
diffstat 2 files changed, 57 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/generate/generate.py	Sat Oct 24 23:01:51 2020 +0200
+++ b/generate/generate.py	Sun Oct 25 08:03:07 2020 +0100
@@ -277,13 +277,20 @@
         out_type = method['response']['$ref'] if 'response' in method else '()'
 
         is_download = method.get('supportsMediaDownload', False) and not method.get('useMediaDownloadService', False)
-        is_upload = 'mediaUpload' in method
 
-        media_upload = method.get('mediaUpload', None)
-        if media_upload and 'simple' in media_upload['protocols']:
-            upload_path = media_upload['protocols']['simple']['path']
+        media_upload = method.get('mediaUpload', {})
+        supported_uploads = []
+        if 'simple' in media_upload.get('protocols', {}):
+            simple_upload_path = media_upload['protocols']['simple']['path']
+            supported_uploads.append('simple')
         else:
-            upload_path = ''
+            simple_upload_path = ''
+        if 'resumable' in media_upload.get('protocols', {}):
+            resumable_upload_path = media_upload['protocols']['resumable']['path']
+            supported_uploads.append('resumable')
+        else:
+            resumable_upload_path = ''
+
         http_method = method['httpMethod']
         has_global_params = 'parameters' in discdoc
         formatted_path, required_params = resolve_parameters(method['path'])
@@ -360,41 +367,44 @@
                 data_normal.pop('in_type')
             method_fragments.append(chevron.render(NormalMethodTmpl, data_normal))
 
-            # We generate an additional implementation with the option of uploading data.
-            if is_upload:
-                data_upload = {
-                    'name':
-                    rust_identifier(methodname),
-                    'param_type':
-                    params_type_name,
-                    'in_type':
-                    in_type,
-                    'out_type':
-                    out_type,
-                    'base_path':
-                    discdoc['rootUrl'],
-                    'rel_path_expr':
-                    '"' + upload_path.lstrip('/') + '"',
-                    'global_params_name':
-                    rust_identifier(global_params_name(discdoc.get('name', ''))) if has_global_params else None,
-                    'params': [{
-                        'param': p,
-                        'snake_param': sp
-                    } for (p, sp) in parameters.items()],
-                    'required_params': [{
-                        'param': p,
-                        'snake_param': sp
-                    } for (p, sp) in required_parameters.items()],
-                    'scopes': [{
-                        'scope': method.get('scopes', [''])[-1]
-                    }],
-                    'description':
-                    method.get('description', ''),
-                    'http_method':
-                    http_method,
-                }
-                method_fragments.append(chevron.render(UploadMethodTmpl, data_upload))
-                method_fragments.append(chevron.render(ResumableUploadMethodTmpl, data_upload))
+        # We generate an additional implementation with the option of uploading data.
+        data_upload = {
+            'name':
+            rust_identifier(methodname),
+            'param_type':
+            params_type_name,
+            'in_type':
+            in_type,
+            'out_type':
+            out_type,
+            'base_path':
+            discdoc['rootUrl'],
+            'simple_rel_path_expr':
+            '"' + simple_upload_path.lstrip('/') + '"',
+            'resumable_rel_path_expr':
+            '"' + resumable_upload_path.lstrip('/') + '"',
+            'global_params_name':
+            rust_identifier(global_params_name(discdoc.get('name', ''))) if has_global_params else None,
+            'params': [{
+                'param': p,
+                'snake_param': sp
+            } for (p, sp) in parameters.items()],
+            'required_params': [{
+                'param': p,
+                'snake_param': sp
+            } for (p, sp) in required_parameters.items()],
+            'scopes': [{
+                'scope': method.get('scopes', [''])[-1]
+            }],
+            'description':
+            method.get('description', ''),
+            'http_method':
+            http_method,
+        }
+        if 'simple' in supported_uploads:
+            method_fragments.append(chevron.render(UploadMethodTmpl, data_upload))
+        if 'resumable' in supported_uploads:
+            method_fragments.append(chevron.render(ResumableUploadMethodTmpl, data_upload))
 
     return chevron.render(
         ServiceImplementationTmpl, {
@@ -515,7 +525,11 @@
     cached = from_cache(cachekey)
     if cached:
         return cached
-    js = json.loads(requests.get(url).text)
+    if url.startswith('http'):
+        js = json.loads(requests.get(url).text)
+    else:
+        with open(url, 'r') as f:
+            js = json.load(f)
     to_cache(cachekey, js)
     return js
 
--- a/generate/templates.py	Sat Oct 24 23:01:51 2020 +0200
+++ b/generate/templates.py	Sun Oct 25 08:03:07 2020 +0100
@@ -161,7 +161,7 @@
 /// This method is a variant of `{{{name}}}()`, taking data for upload. It performs a multipart upload.
 pub async fn {{{name}}}_upload(
     &mut self, params: &{{{param_type}}}, {{#in_type}}req: &{{{in_type}}},{{/in_type}} data: hyper::body::Bytes) -> Result<{{out_type}}> {
-    let rel_path = {{{rel_path_expr}}};
+    let rel_path = {{{simple_rel_path_expr}}};
     let path = "{{{base_path}}}".to_string() + &rel_path;
 
     let tok;
@@ -207,7 +207,7 @@
 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 rel_path = {{{resumable_rel_path_expr}}};
     let path = "{{{base_path}}}".to_string() + &rel_path;
     let tok;
     if self.scopes.is_empty() {