changeset 145:0f6f369a8539

generate: Enable easy overriding of URLs for test purposes
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 27 Oct 2020 10:41:33 +0100
parents 0467dfe39f46
children de02414174dc
files generate/generate.py generate/templates.py
diffstat 2 files changed, 48 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/generate/generate.py	Tue Oct 27 10:40:59 2020 +0100
+++ b/generate/generate.py	Tue Oct 27 10:41:33 2020 +0100
@@ -319,8 +319,8 @@
                 'download_in_type': in_type if in_type else 'EmptyRequest',
                 'out_type':
                 out_type,
-                'base_path':
-                discdoc['baseUrl'],
+                'base_path': discdoc['baseUrl'],
+                'root_path': discdoc['rootUrl'],
                 'rel_path_expr':
                 formatted_path,
                 'params': [{
@@ -352,8 +352,8 @@
                 'in_type': in_type,
                 'out_type':
                 out_type,
-                'base_path':
-                discdoc['baseUrl'],
+                'base_path': discdoc['baseUrl'],
+                'root_path': discdoc['rootUrl'],
                 'rel_path_expr':
                 formatted_path,
                 'params': [{
@@ -383,7 +383,8 @@
             'param_type': params_type_name,
             'in_type': in_type,
             'out_type': out_type,
-            'base_path': discdoc['rootUrl'],
+            'base_path': discdoc['baseUrl'],
+            'root_path': discdoc['rootUrl'],
             'simple_rel_path_expr': formatted_simple_upload_path.lstrip('/'),
             'resumable_rel_path_expr': formatted_resumable_upload_path.lstrip('/'),
             'global_params_name':
@@ -412,6 +413,8 @@
         ServiceImplementationTmpl, {
             'service': service,
             'name': capitalize_first(discdoc.get('name', '')),
+            'base_path': discdoc['baseUrl'],
+            'root_path': discdoc['rootUrl'],
             'wants_auth': 'auth' in discdoc,
             'methods': [{
                 'text': t
--- a/generate/templates.py	Tue Oct 27 10:40:59 2020 +0100
+++ b/generate/templates.py	Tue Oct 27 10:41:33 2020 +0100
@@ -95,6 +95,9 @@
     authenticator: Box<dyn 'static + std::ops::Deref<Target=Authenticator>>,
     scopes: Vec<String>,
     {{/wants_auth}}
+
+    base_url: String,
+    root_url: String,
 }
 
 impl {{{service}}}Service {
@@ -105,7 +108,39 @@
     {{#wants_auth}}<A: 'static + std::ops::Deref<Target=Authenticator>>
     {{/wants_auth}}(client: TlsClient{{#wants_auth}}, auth: A{{/wants_auth}}) -> {{service}}Service {
         {{{service}}}Service { client: client
-            {{#wants_auth}}, authenticator: Box::new(auth), scopes: vec![]{{/wants_auth}} }
+            {{#wants_auth}}, authenticator: Box::new(auth), scopes: vec![]{{/wants_auth}},
+            base_url: "{{{base_path}}}".into(), root_url: "{{{root_path}}}".into() }
+    }
+
+    /// Provide the base URL of this API. The returned URL is guaranteed to end with a '/'.
+    fn base_url(&self) -> String {
+        if self.base_url.ends_with("/") {
+            return self.base_url.clone();
+        }
+        return self.base_url.clone() + "/";
+    }
+    /// Provide the root URL of this API. The returned URL is guaranteed to end with a '/'.
+    fn root_url(&self) -> String {
+        if self.root_url.ends_with("/") {
+            return self.root_url.clone();
+        }
+        return self.root_url.clone();
+    }
+    /// Returns appropriate URLs for relative and absolute paths.
+    fn format_path(&self, path: &str) -> String {
+        if path.starts_with("/") {
+            return self.root_url().trim_end_matches("/").to_string() + path;
+        } else {
+            return self.base_url() + path;
+        }
+    }
+
+    #[cfg(test)]
+    /// Override API URLs. `base` is the base path relative to which (relative) method paths are interpreted,
+    /// whereas `root` is the URL relative to which absolute paths are interpreted.
+    fn set_urls(&mut self, base: String, root: String) {
+        self.base_url = base;
+        self.root_url = root;
     }
 
     {{#wants_auth}}
@@ -135,7 +170,7 @@
     &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 path = self.format_path(rel_path.as_str());
 
     let mut headers = vec![];
     {{#wants_auth}}
@@ -181,7 +216,7 @@
 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 = {{{simple_rel_path_expr}}};
-    let path = "{{{base_path}}}".to_string() + &rel_path;
+    let path = self.format_path(rel_path.as_str());
 
     let mut headers = vec![];
     {{#wants_auth}}
@@ -232,7 +267,7 @@
     &'client mut self, params: &{{{param_type}}}, {{#in_type}}req: &{{{in_type}}}{{/in_type}}) -> Result<ResumableUpload<'client, {{{out_type}}}>> {
 
     let rel_path = {{{resumable_rel_path_expr}}};
-    let path = "{{{base_path}}}".to_string() + &rel_path;
+    let path = self.format_path(rel_path.as_str());
 
     let mut headers = vec![];
     {{#wants_auth}}
@@ -286,7 +321,7 @@
     -> Result<Download<'a, {{{download_in_type}}}, {{{out_type}}}>> {
 
     let rel_path = {{{rel_path_expr}}};
-    let path = "{{{base_path}}}".to_string() + &rel_path;
+    let path = self.format_path(rel_path.as_str());
 
     let mut headers = vec![];
     {{#wants_auth}}