From d756a7cfeafb9b8fc3de2f0c9b036a00f45758f5 Mon Sep 17 00:00:00 2001 From: HugoMario Date: Fri, 20 Dec 2019 11:48:41 -0500 Subject: [PATCH] added support for api key refresh in configuration module --- .../handlebars/python/configuration.mustache | 17 ++++++++++++----- .../mustache/python/configuration.mustache | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main/resources/handlebars/python/configuration.mustache b/src/main/resources/handlebars/python/configuration.mustache index 84e6ad4d20..803438be0f 100644 --- a/src/main/resources/handlebars/python/configuration.mustache +++ b/src/main/resources/handlebars/python/configuration.mustache @@ -47,6 +47,8 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): self.api_key = {} # dict to store API prefix (e.g. Bearer) self.api_key_prefix = {} + # function to refresh API key if expired + self.refresh_api_key_hook = None # Username for HTTP basic authentication self.username = "" # Password for HTTP basic authentication @@ -197,11 +199,16 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): :param identifier: The identifier of apiKey. :return: The token for api key authentication. """ - if (self.api_key.get(identifier) and - self.api_key_prefix.get(identifier)): - return self.api_key_prefix[identifier] + ' ' + self.api_key[identifier] # noqa: E501 - elif self.api_key.get(identifier): - return self.api_key[identifier] + if self.refresh_api_key_hook: + self.refresh_api_key_hook(self) + + key = self.api_key.get(identifier) + if key: + prefix = self.api_key_prefix.get(identifier) + if prefix: + return "%s %s" % (prefix, key) + else: + return key def get_basic_auth_token(self): """Gets HTTP basic authentication header (string). diff --git a/src/main/resources/mustache/python/configuration.mustache b/src/main/resources/mustache/python/configuration.mustache index 84e6ad4d20..803438be0f 100644 --- a/src/main/resources/mustache/python/configuration.mustache +++ b/src/main/resources/mustache/python/configuration.mustache @@ -47,6 +47,8 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): self.api_key = {} # dict to store API prefix (e.g. Bearer) self.api_key_prefix = {} + # function to refresh API key if expired + self.refresh_api_key_hook = None # Username for HTTP basic authentication self.username = "" # Password for HTTP basic authentication @@ -197,11 +199,16 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): :param identifier: The identifier of apiKey. :return: The token for api key authentication. """ - if (self.api_key.get(identifier) and - self.api_key_prefix.get(identifier)): - return self.api_key_prefix[identifier] + ' ' + self.api_key[identifier] # noqa: E501 - elif self.api_key.get(identifier): - return self.api_key[identifier] + if self.refresh_api_key_hook: + self.refresh_api_key_hook(self) + + key = self.api_key.get(identifier) + if key: + prefix = self.api_key_prefix.get(identifier) + if prefix: + return "%s %s" % (prefix, key) + else: + return key def get_basic_auth_token(self): """Gets HTTP basic authentication header (string).