From 8cad04860ec59b6a57d50827fb288871c2dbf384 Mon Sep 17 00:00:00 2001 From: Ilya Taratukhin Date: Mon, 29 Jul 2024 20:39:36 +0200 Subject: [PATCH] feat: simplify `Configuration` class, use inline types --- .../configuration.py | 85 ++++----------- template/configuration.mustache | 101 ++++-------------- 2 files changed, 40 insertions(+), 146 deletions(-) diff --git a/fingerprint_pro_server_api_sdk/configuration.py b/fingerprint_pro_server_api_sdk/configuration.py index c266cd54..8d3a9355 100644 --- a/fingerprint_pro_server_api_sdk/configuration.py +++ b/fingerprint_pro_server_api_sdk/configuration.py @@ -14,11 +14,12 @@ import logging import multiprocessing import sys -import urllib3 import http.client as httplib +from typing import Dict -class Configuration(object): + +class Configuration: """NOTE: This class is auto generated by the swagger code generator program. Ref: https://github.com/swagger-api/swagger-codegen @@ -27,7 +28,7 @@ class Configuration(object): _default = None - def __init__(self, api_key, region="us"): + def __init__(self, api_key: str, region: str = "us"): """Constructor""" if self._default: for key in self._default.__dict__.keys(): @@ -41,20 +42,10 @@ def __init__(self, api_key, region="us"): # Authentication Settings # dict to store API key(s) - self.api_key = {} - self.api_key['Auth-API-Key'] = 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 - self.password = "" + self.api_key = api_key # Logging Settings - self.logger = {} - self.logger["package_logger"] = logging.getLogger("fingerprint_pro_server_api_sdk") - self.logger["urllib3_logger"] = logging.getLogger("urllib3") + self.logger = {"package_logger": logging.getLogger("fingerprint_pro_server_api_sdk"), + "urllib3_logger": logging.getLogger("urllib3")} # Log format self.logger_format = '%(asctime)s %(levelname)s %(message)s' # Log stream handler @@ -99,26 +90,24 @@ def set_default(cls, default): cls._default = default @property - def logger_file(self): + def logger_file(self) -> str: """The logger file. If the logger_file is None, then add stream handler and remove file handler. Otherwise, add file handler and remove stream handler. - :param value: The logger_file path. - :type: str + :return: The logger_file path. """ return self.__logger_file @logger_file.setter - def logger_file(self, value): + def logger_file(self, value: str): """The logger file. If the logger_file is None, then add stream handler and remove file handler. Otherwise, add file handler and remove stream handler. :param value: The logger_file path. - :type: str """ self.__logger_file = value if self.__logger_file: @@ -141,20 +130,18 @@ def logger_file(self, value): logger.removeHandler(self.logger_file_handler) @property - def debug(self): + def debug(self) -> bool: """Debug status :param value: The debug status, True or False. - :type: bool """ return self.__debug @debug.setter - def debug(self, value): + def debug(self, value: bool): """Debug status :param value: The debug status, True or False. - :type: bool """ self.__debug = value if self.__debug: @@ -172,56 +159,27 @@ def debug(self, value): httplib.HTTPConnection.debuglevel = 0 @property - def logger_format(self): + def logger_format(self) -> str: """The logger format. The logger_formatter will be updated when sets logger_format. - :param value: The format string. - :type: str + :return: The format string. """ return self.__logger_format @logger_format.setter - def logger_format(self, value): + def logger_format(self, value: str): """The logger format. The logger_formatter will be updated when sets logger_format. :param value: The format string. - :type: str """ self.__logger_format = value self.logger_formatter = logging.Formatter(self.__logger_format) - def get_api_key_with_prefix(self, identifier): - """Gets API key (with prefix if set). - - :param identifier: The identifier of apiKey. - :return: The token for api key authentication. - """ - - 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). - - :return: The token for basic HTTP authentication. - """ - return urllib3.util.make_headers( - basic_auth=self.username + ':' + self.password - ).get('authorization') - - def auth_settings(self): + def auth_settings(self) -> Dict[str, Dict[str, str]]: """Gets Auth Settings dict for api client. :return: The Auth Settings information dict. @@ -232,18 +190,11 @@ def auth_settings(self): 'type': 'api_key', 'in': 'header', 'key': 'Auth-API-Key', - 'value': self.get_api_key_with_prefix('Auth-API-Key') - }, - 'ApiKeyQuery': - { - 'type': 'api_key', - 'in': 'query', - 'key': 'api_key', - 'value': self.get_api_key_with_prefix('api_key') + 'value': self.api_key }, } - def get_host(self, region): + def get_host(self, region: str) -> str: return { "us": "https://api.fpjs.io", "eu": "https://eu.api.fpjs.io", diff --git a/template/configuration.mustache b/template/configuration.mustache index e9b5ca78..cba5e82a 100644 --- a/template/configuration.mustache +++ b/template/configuration.mustache @@ -6,11 +6,12 @@ import copy import logging import multiprocessing import sys -import urllib3 import http.client as httplib +from typing import Dict -class Configuration(object): + +class Configuration: """NOTE: This class is auto generated by the swagger code generator program. Ref: https://github.com/swagger-api/swagger-codegen @@ -19,7 +20,7 @@ class Configuration(object): _default = None - def __init__(self, api_key, region="us"): + def __init__(self, api_key: str, region: str = "us"): """Constructor""" if self._default: for key in self._default.__dict__.keys(): @@ -33,24 +34,14 @@ class Configuration(object): # Authentication Settings # dict to store API key(s) - self.api_key = {} - self.api_key['Auth-API-Key'] = 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 - self.password = "" + self.api_key = api_key {{#authMethods}}{{#isOAuth}} # access token for OAuth self.access_token = "" {{/isOAuth}}{{/authMethods}} # Logging Settings - self.logger = {} - self.logger["package_logger"] = logging.getLogger("{{packageName}}") - self.logger["urllib3_logger"] = logging.getLogger("urllib3") + self.logger = {"package_logger": logging.getLogger("{{packageName}}"), + "urllib3_logger": logging.getLogger("urllib3")} # Log format self.logger_format = '%(asctime)s %(levelname)s %(message)s' # Log stream handler @@ -95,26 +86,24 @@ class Configuration(object): cls._default = default @property - def logger_file(self): + def logger_file(self) -> str: """The logger file. If the logger_file is None, then add stream handler and remove file handler. Otherwise, add file handler and remove stream handler. - :param value: The logger_file path. - :type: str + :return: The logger_file path. """ return self.__logger_file @logger_file.setter - def logger_file(self, value): + def logger_file(self, value: str): """The logger file. If the logger_file is None, then add stream handler and remove file handler. Otherwise, add file handler and remove stream handler. :param value: The logger_file path. - :type: str """ self.__logger_file = value if self.__logger_file: @@ -137,20 +126,18 @@ class Configuration(object): logger.removeHandler(self.logger_file_handler) @property - def debug(self): + def debug(self) -> bool: """Debug status :param value: The debug status, True or False. - :type: bool """ return self.__debug @debug.setter - def debug(self, value): + def debug(self, value: bool): """Debug status :param value: The debug status, True or False. - :type: bool """ self.__debug = value if self.__debug: @@ -168,56 +155,27 @@ class Configuration(object): httplib.HTTPConnection.debuglevel = 0 @property - def logger_format(self): + def logger_format(self) -> str: """The logger format. The logger_formatter will be updated when sets logger_format. - :param value: The format string. - :type: str + :return: The format string. """ return self.__logger_format @logger_format.setter - def logger_format(self, value): + def logger_format(self, value: str): """The logger format. The logger_formatter will be updated when sets logger_format. :param value: The format string. - :type: str """ self.__logger_format = value self.logger_formatter = logging.Formatter(self.__logger_format) - def get_api_key_with_prefix(self, identifier): - """Gets API key (with prefix if set). - - :param identifier: The identifier of apiKey. - :return: The token for api key authentication. - """ - - 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). - - :return: The token for basic HTTP authentication. - """ - return urllib3.util.make_headers( - basic_auth=self.username + ':' + self.password - ).get('authorization') - - def auth_settings(self): + def auth_settings(self) -> Dict[str, Dict[str, str]]: """Gets Auth Settings dict for api client. :return: The Auth Settings information dict. @@ -225,34 +183,19 @@ class Configuration(object): return { {{#authMethods}} {{#isApiKey}} +{{#isKeyInHeader}} '{{name}}': { 'type': 'api_key', - 'in': {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}, - 'key': '{{keyParamName}}', - 'value': self.get_api_key_with_prefix('{{keyParamName}}') - }, -{{/isApiKey}} -{{#isBasic}} - '{{name}}': - { - 'type': 'basic', - 'in': 'header', - 'key': 'Authorization', - 'value': self.get_basic_auth_token() - }, -{{/isBasic}}{{#isOAuth}} - '{{name}}': - { - 'type': 'oauth2', 'in': 'header', - 'key': 'Authorization', - 'value': 'Bearer ' + self.access_token + 'key': '{{keyParamName}}', + 'value': self.api_key }, -{{/isOAuth}}{{/authMethods}} +{{/isKeyInHeader}} +{{/isApiKey}}{{/authMethods}} } - def get_host(self, region): + def get_host(self, region: str) -> str: return { "us": "https://api.fpjs.io", "eu": "https://eu.api.fpjs.io",