Skip to content

Commit 933ab06

Browse files
authored
[python-client] Enable per-request auth settings (#6569)
* Enable per-request access token in Python client. * Add missing regenerated sample files. * Rework to the more general case of overriding the auth settings for a request. * Add unit tests. * Update api_client.mustache so that request_auth overrides all other auth settings when specified.
1 parent ebcce67 commit 933ab06

File tree

32 files changed

+1437
-382
lines changed

32 files changed

+1437
-382
lines changed

modules/openapi-generator/src/main/resources/python/api.mustache

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class {{classname}}(object):
103103
number provided, it will be total request
104104
timeout. It can also be a pair (tuple) of
105105
(connection, read) timeouts.
106+
:param _request_auth: set to override the auth_settings for an a single
107+
request; this effectively ignores the authentication
108+
in the spec for a single request.
109+
:type _request_auth: dict, optional
106110
:return: Returns the result object.
107111
If the method is called asynchronously,
108112
returns the request thread.
@@ -137,7 +141,8 @@ class {{classname}}(object):
137141
'async_req',
138142
'_return_http_data_only',
139143
'_preload_content',
140-
'_request_timeout'
144+
'_request_timeout',
145+
'_request_auth'
141146
]
142147
)
143148

@@ -266,6 +271,7 @@ class {{classname}}(object):
266271
{{#servers.0}}
267272
_host=local_var_host,
268273
{{/servers.0}}
269-
collection_formats=collection_formats)
274+
collection_formats=collection_formats,
275+
_request_auth=local_var_params.get('_request_auth'))
270276
{{/operation}}
271277
{{/operations}}

modules/openapi-generator/src/main/resources/python/api_client.mustache

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ class ApiClient(object):
132132
query_params=None, header_params=None, body=None, post_params=None,
133133
files=None, response_type=None, auth_settings=None,
134134
_return_http_data_only=None, collection_formats=None,
135-
_preload_content=True, _request_timeout=None, _host=None):
135+
_preload_content=True, _request_timeout=None, _host=None,
136+
_request_auth=None):
136137

137138
config = self.configuration
138139

@@ -173,7 +174,9 @@ class ApiClient(object):
173174
post_params.extend(self.files_parameters(files))
174175

175176
# auth setting
176-
self.update_params_for_auth(header_params, query_params, auth_settings)
177+
self.update_params_for_auth(
178+
header_params, query_params, auth_settings,
179+
request_auth=_request_auth)
177180

178181
# body
179182
if body:
@@ -347,7 +350,8 @@ class ApiClient(object):
347350
body=None, post_params=None, files=None,
348351
response_type=None, auth_settings=None, async_req=None,
349352
_return_http_data_only=None, collection_formats=None,
350-
_preload_content=True, _request_timeout=None, _host=None):
353+
_preload_content=True, _request_timeout=None, _host=None,
354+
_request_auth=None):
351355
"""Makes the HTTP request (synchronous) and returns deserialized data.
352356

353357
To make an async_req request, set the async_req parameter.
@@ -377,6 +381,10 @@ class ApiClient(object):
377381
number provided, it will be total request
378382
timeout. It can also be a pair (tuple) of
379383
(connection, read) timeouts.
384+
:param _request_auth: set to override the auth_settings for an a single
385+
request; this effectively ignores the authentication
386+
in the spec for a single request.
387+
:type _request_token: dict, optional
380388
:return:
381389
If async_req parameter is True,
382390
the request will be called asynchronously.
@@ -390,7 +398,8 @@ class ApiClient(object):
390398
body, post_params, files,
391399
response_type, auth_settings,
392400
_return_http_data_only, collection_formats,
393-
_preload_content, _request_timeout, _host)
401+
_preload_content, _request_timeout, _host,
402+
_request_auth)
394403

395404
return self.pool.apply_async(self.__call_api, (resource_path,
396405
method, path_params,
@@ -403,7 +412,7 @@ class ApiClient(object):
403412
collection_formats,
404413
_preload_content,
405414
_request_timeout,
406-
_host))
415+
_host, _request_auth))
407416

408417
def request(self, method, url, query_params=None, headers=None,
409418
post_params=None, body=None, _preload_content=True,
@@ -550,29 +559,45 @@ class ApiClient(object):
550559
else:
551560
return content_types[0]
552561

553-
def update_params_for_auth(self, headers, querys, auth_settings):
562+
def update_params_for_auth(self, headers, querys, auth_settings,
563+
request_auth=None):
554564
"""Updates header and query params based on authentication setting.
555565

556566
:param headers: Header parameters dict to be updated.
557567
:param querys: Query parameters tuple list to be updated.
558568
:param auth_settings: Authentication setting identifiers list.
569+
:param request_auth: if set, the provided settings will
570+
override the token in the configuration.
559571
"""
560572
if not auth_settings:
561573
return
562574

575+
if request_auth:
576+
self._apply_auth_params(headers, querys, request_auth)
577+
return
578+
563579
for auth in auth_settings:
564580
auth_setting = self.configuration.auth_settings().get(auth)
565581
if auth_setting:
566-
if auth_setting['in'] == 'cookie':
567-
headers['Cookie'] = auth_setting['value']
568-
elif auth_setting['in'] == 'header':
569-
headers[auth_setting['key']] = auth_setting['value']
570-
elif auth_setting['in'] == 'query':
571-
querys.append((auth_setting['key'], auth_setting['value']))
572-
else:
573-
raise ApiValueError(
574-
'Authentication token must be in `query` or `header`'
575-
)
582+
self._apply_auth_params(headers, querys, auth_setting)
583+
584+
def _apply_auth_params(self, headers, querys, auth_setting):
585+
"""Updates the request parameters based on a single auth_setting
586+
587+
:param headers: Header parameters dict to be updated.
588+
:param querys: Query parameters tuple list to be updated.
589+
:param auth_setting: auth settings for the endpoint
590+
"""
591+
if auth_setting['in'] == 'cookie':
592+
headers['Cookie'] = auth_setting['value']
593+
elif auth_setting['in'] == 'header':
594+
headers[auth_setting['key']] = auth_setting['value']
595+
elif auth_setting['in'] == 'query':
596+
querys.append((auth_setting['key'], auth_setting['value']))
597+
else:
598+
raise ApiValueError(
599+
'Authentication token must be in `query` or `header`'
600+
)
576601

577602
def __deserialize_file(self, response):
578603
"""Deserializes body to file

samples/client/petstore/python-asyncio/petstore_api/api/another_fake_api.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ def call_123_test_special_tags_with_http_info(self, body, **kwargs): # noqa: E5
9191
number provided, it will be total request
9292
timeout. It can also be a pair (tuple) of
9393
(connection, read) timeouts.
94+
:param _request_auth: set to override the auth_settings for an a single
95+
request; this effectively ignores the authentication
96+
in the spec for a single request.
97+
:type _request_auth: dict, optional
9498
:return: Returns the result object.
9599
If the method is called asynchronously,
96100
returns the request thread.
@@ -107,7 +111,8 @@ def call_123_test_special_tags_with_http_info(self, body, **kwargs): # noqa: E5
107111
'async_req',
108112
'_return_http_data_only',
109113
'_preload_content',
110-
'_request_timeout'
114+
'_request_timeout',
115+
'_request_auth'
111116
]
112117
)
113118

@@ -163,4 +168,5 @@ def call_123_test_special_tags_with_http_info(self, body, **kwargs): # noqa: E5
163168
_return_http_data_only=local_var_params.get('_return_http_data_only'), # noqa: E501
164169
_preload_content=local_var_params.get('_preload_content', True),
165170
_request_timeout=local_var_params.get('_request_timeout'),
166-
collection_formats=collection_formats)
171+
collection_formats=collection_formats,
172+
_request_auth=local_var_params.get('_request_auth'))

0 commit comments

Comments
 (0)