Skip to content

Commit

Permalink
fixes #1252 python asyncio: await refresh_api_key_hook, replace usage…
Browse files Browse the repository at this point in the history
… of urllib3, remove urllib3
  • Loading branch information
e-geist committed Feb 18, 2024
1 parent 0f7eeb2 commit 975e046
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/main/resources/handlebars/python/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
31 changes: 25 additions & 6 deletions src/main/resources/handlebars/python/configuration.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand All @@ -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}}
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/handlebars/python/requirements.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
5 changes: 4 additions & 1 deletion src/main/resources/handlebars/python/setup.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 975e046

Please sign in to comment.