From 975e04672a20d0799906d335e0a103d43b0157f7 Mon Sep 17 00:00:00 2001 From: Eugen Geist Date: Sun, 11 Feb 2024 15:54:56 +0100 Subject: [PATCH] fixes #1252 python asyncio: await refresh_api_key_hook, replace usage of urllib3, remove urllib3 --- .../handlebars/python/api_client.mustache | 6 ++-- .../handlebars/python/configuration.mustache | 31 +++++++++++++++---- .../handlebars/python/requirements.mustache | 5 +++ .../handlebars/python/setup.mustache | 5 ++- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/resources/handlebars/python/api_client.mustache b/src/main/resources/handlebars/python/api_client.mustache index 5c62fff24f..51b92e963c 100644 --- a/src/main/resources/handlebars/python/api_client.mustache +++ b/src/main/resources/handlebars/python/api_client.mustache @@ -133,7 +133,7 @@ class ApiClient(object): collection_formats) # auth setting - self.update_params_for_auth(header_params, query_params, auth_settings) + {{#asyncio}}await {{/asyncio}}self.update_params_for_auth(header_params, query_params, auth_settings) # body if body: @@ -488,7 +488,7 @@ class ApiClient(object): else: return content_types[0] - def update_params_for_auth(self, headers, querys, auth_settings): + {{#asyncio}}async {{/asyncio}}def update_params_for_auth(self, headers, querys, auth_settings): """Updates header and query params based on authentication setting. :param headers: Header parameters dict to be updated. @@ -499,7 +499,7 @@ class ApiClient(object): return for auth in auth_settings: - auth_setting = self.configuration.auth_settings().get(auth) + auth_setting = {{#asyncio}}(await {{/asyncio}}self.configuration.auth_settings(){{#asyncio}}){{/asyncio}}.get(auth) if auth_setting: if not auth_setting['value']: continue diff --git a/src/main/resources/handlebars/python/configuration.mustache b/src/main/resources/handlebars/python/configuration.mustache index adb6d9aa4b..c56db103e3 100644 --- a/src/main/resources/handlebars/python/configuration.mustache +++ b/src/main/resources/handlebars/python/configuration.mustache @@ -4,11 +4,19 @@ from __future__ import absolute_import +{{#asyncio}} +import aiohttp +{{/asyncio}} import copy import logging +{{^asyncio}}{{^tornado}} import multiprocessing +{{/asyncio}}{{/tornado}} import sys +{{^asyncio}} import urllib3 +{{/asyncio}} + import six from six.moves import http_client as httplib @@ -60,7 +68,12 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): # Logging Settings self.logger = {} self.logger["package_logger"] = logging.getLogger("{{packageName}}") +{{#asyncio}} + self.logger["aiohttp"] = logging.getLogger("aiohttp.client") +{{/asyncio}} +{{^asyncio}} self.logger["urllib3_logger"] = logging.getLogger("urllib3") +{{/asyncio}} # Log format self.logger_format = '%(asctime)s %(levelname)s %(message)s' # Log stream handler @@ -84,14 +97,14 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): self.key_file = None # Set this to True/False to enable/disable SSL hostname verification. self.assert_hostname = None - +{{^asyncio}}{{^tornado}} # urllib3 connection pool's maximum number of connections saved # per pool. urllib3 uses 1 connection as default value, but this is # not the best value when you are making a lot of possibly parallel # requests to the same host, which is often the case here. # cpu_count * 5 is used as default value to increase performance. self.connection_pool_maxsize = multiprocessing.cpu_count() * 5 - +{{/tornado}}{{/asyncio}} # Proxy URL self.proxy = None # Safe chars for path_param @@ -193,14 +206,14 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): self.__logger_format = value self.logger_formatter = logging.Formatter(self.__logger_format) - def get_api_key_with_prefix(self, identifier): + {{#asyncio}}async {{/asyncio}}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) + {{#asyncio}}await {{/asyncio}}self.refresh_api_key_hook(self) key = self.api_key.get(identifier) if key: @@ -217,12 +230,18 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): """ token = "" if self.username or self.password: +{{#asyncio}} + token = aiohttp.BasicAuth( + self.username, self.password).encode() +{{/asyncio}} +{{^asyncio}} token = urllib3.util.make_headers( basic_auth=self.username + ':' + self.password ).get('authorization') +{{/asyncio}} return token - def auth_settings(self): + {{#asyncio}}async {{/asyncio}}def auth_settings(self): """Gets Auth Settings dict for api client. :return: The Auth Settings information dict. @@ -235,7 +254,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)): 'type': 'api_key', 'in': {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}, 'key': '{{keyParamName}}', - 'value': self.get_api_key_with_prefix('{{keyParamName}}') + 'value': {{#asyncio}}await {{/asyncio}}self.get_api_key_with_prefix('{{keyParamName}}') }, {{/isApiKey}} {{#isBasic}} diff --git a/src/main/resources/handlebars/python/requirements.mustache b/src/main/resources/handlebars/python/requirements.mustache index bafdc07532..378df2e4b0 100644 --- a/src/main/resources/handlebars/python/requirements.mustache +++ b/src/main/resources/handlebars/python/requirements.mustache @@ -2,4 +2,9 @@ certifi >= 14.05.14 six >= 1.10 python_dateutil >= 2.5.3 setuptools >= 21.0.0 +{{#asyncio}} +aiohttp +{{/asyncio}} +{{^asyncio}} urllib3 >= 1.15.1 +{{/asyncio}} \ No newline at end of file diff --git a/src/main/resources/handlebars/python/setup.mustache b/src/main/resources/handlebars/python/setup.mustache index e7107e9be9..900d2c57f8 100644 --- a/src/main/resources/handlebars/python/setup.mustache +++ b/src/main/resources/handlebars/python/setup.mustache @@ -16,13 +16,16 @@ VERSION = "{{packageVersion}}" # prerequisite: setuptools # http://pypi.python.org/pypi/setuptools -REQUIRES = ["urllib3 >= 1.15", "six >= 1.10", "certifi", "python-dateutil"] +REQUIRES = ["six >= 1.10", "certifi", "python-dateutil"] {{#asyncio}} REQUIRES.append("aiohttp") {{/asyncio}} {{#tornado}} REQUIRES.append("tornado") {{/tornado}} +{{^asyncio}} +REQUIRES.append("urllib3 >= 1.15") +{{/asyncio}} setup( name=NAME,