Skip to content

Commit 87a5182

Browse files
[Python] - Migrate enable per request authentification in new python codegen (#11279)
* LDS-2166 : add request auth to api client and api call Can now overwrite request auth by request Envoyé depuis mon iPhone. P.S. : Ce commit est certifié sans gluten * LDS-2166 : Add samples Envoyé depuis mon iPhone. P.S. : Ce commit est certifié sans gluten * LDS-2166 : fix test Envoyé depuis mon iPhone. P.S. : Ce commit est certifié sans gluten * LDS-2166 : Fixing test in python_disallowAdditionalPropertiesIfNotPresent Envoyé depuis mon iPhone. P.S. : Ce commit est certifié sans gluten * LDS-2166 : add removed line break Envoyé depuis mon iPhone. P.S. : Ce commit est certifié sans gluten * LDS-2166 : add name for _request_auth params Add None when _content_type is not set Envoyé depuis mon iPhone. P.S. : Ce commit est certifié sans gluten * LDS-2166 : add tabulation Envoyé depuis mon iPhone. P.S. : Ce commit est certifié sans gluten * LDS-2166 : fix missing values Envoyé depuis mon iPhone. P.S. : Ce commit est certifié sans gluten * LDS-2166 : generate sample Add _request_auth in sample Envoyé depuis mon iPhone. P.S. : Ce commit est certifié sans gluten * Request auth can now use multiple auth Request auth is now a list of dict Envoyé depuis mon iPhone. P.S. : Ce commit est certifié sans gluten * Add request_auths in test Envoyé depuis mon iPhone. P.S. : Ce commit est certifié sans gluten Co-authored-by: Géry THRASIBULE <[email protected]>
1 parent bc2624d commit 87a5182

File tree

33 files changed

+940
-128
lines changed

33 files changed

+940
-128
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ class {{classname}}(object):
280280
_host_index (int/None): specifies the index of the server
281281
that we want to use.
282282
Default is read from the configuration.
283+
_request_auths (list): set to override the auth_settings for an a single
284+
request; this effectively ignores the authentication
285+
in the spec for a single request.
286+
Default is None
283287
async_req (bool): execute request asynchronously
284288

285289
Returns:
@@ -311,6 +315,7 @@ class {{classname}}(object):
311315
kwargs['_content_type'] = kwargs.get(
312316
'_content_type')
313317
kwargs['_host_index'] = kwargs.get('_host_index')
318+
kwargs['_request_auths'] = kwargs.get('_request_auths', None)
314319
{{#requiredParams}}
315320
kwargs['{{paramName}}'] = \
316321
{{paramName}}

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

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ class ApiClient(object):
130130
_request_timeout: typing.Optional[typing.Union[int, float, typing.Tuple]] = None,
131131
_host: typing.Optional[str] = None,
132132
_check_type: typing.Optional[bool] = None,
133-
_content_type: typing.Optional[str] = None
133+
_content_type: typing.Optional[str] = None,
134+
_request_auths: typing.Optional[typing.List[typing.Dict[str, typing.Any]]] = None
134135
):
135136

136137
config = self.configuration
@@ -180,7 +181,8 @@ class ApiClient(object):
180181

181182
# auth setting
182183
self.update_params_for_auth(header_params, query_params,
183-
auth_settings, resource_path, method, body)
184+
auth_settings, resource_path, method, body,
185+
request_auths=_request_auths)
184186

185187
# request url
186188
if _host is None:
@@ -362,7 +364,8 @@ class ApiClient(object):
362364
_preload_content: bool = True,
363365
_request_timeout: typing.Optional[typing.Union[int, float, typing.Tuple]] = None,
364366
_host: typing.Optional[str] = None,
365-
_check_type: typing.Optional[bool] = None
367+
_check_type: typing.Optional[bool] = None,
368+
_request_auths: typing.Optional[typing.List[typing.Dict[str, typing.Any]]] = None
366369
):
367370
"""Makes the HTTP request (synchronous) and returns deserialized data.
368371

@@ -410,6 +413,10 @@ class ApiClient(object):
410413
:param _check_type: boolean describing if the data back from the server
411414
should have its type checked.
412415
:type _check_type: bool, optional
416+
:param _request_auths: set to override the auth_settings for an a single
417+
request; this effectively ignores the authentication
418+
in the spec for a single request.
419+
:type _request_auths: list, optional
413420
:return:
414421
If async_req parameter is True,
415422
the request will be called asynchronously.
@@ -424,7 +431,7 @@ class ApiClient(object):
424431
response_type, auth_settings,
425432
_return_http_data_only, collection_formats,
426433
_preload_content, _request_timeout, _host,
427-
_check_type)
434+
_check_type, _request_auths=_request_auths)
428435

429436
return self.pool.apply_async(self.__call_api, (resource_path,
430437
method, path_params,
@@ -437,7 +444,7 @@ class ApiClient(object):
437444
collection_formats,
438445
_preload_content,
439446
_request_timeout,
440-
_host, _check_type))
447+
_host, _check_type, None, _request_auths))
441448

442449
def request(self, method, url, query_params=None, headers=None,
443450
post_params=None, body=None, _preload_content=True,
@@ -609,7 +616,7 @@ class ApiClient(object):
609616
return content_types[0]
610617

611618
def update_params_for_auth(self, headers, queries, auth_settings,
612-
resource_path, method, body):
619+
resource_path, method, body, request_auths=None):
613620
"""Updates header and query params based on authentication setting.
614621

615622
:param headers: Header parameters dict to be updated.
@@ -619,33 +626,43 @@ class ApiClient(object):
619626
:param method: A string representation of the HTTP request method.
620627
:param body: A object representing the body of the HTTP request.
621628
The object type is the return value of _encoder.default().
629+
:param request_auths: if set, the provided settings will
630+
override the token in the configuration.
622631
"""
623632
if not auth_settings:
624633
return
625634

635+
if request_auths:
636+
for auth_setting in request_auths:
637+
self._apply_auth_params(headers, queries, resource_path, method, body, auth_setting)
638+
return
639+
626640
for auth in auth_settings:
627641
auth_setting = self.configuration.auth_settings().get(auth)
628642
if auth_setting:
629-
if auth_setting['in'] == 'cookie':
630-
headers['Cookie'] = auth_setting['value']
631-
elif auth_setting['in'] == 'header':
632-
if auth_setting['type'] != 'http-signature':
633-
headers[auth_setting['key']] = auth_setting['value']
643+
self._apply_auth_params(headers, queries, resource_path, method, body, auth_setting)
644+
645+
def _apply_auth_params(self, headers, queries, resource_path, method, body, auth_setting):
646+
if auth_setting['in'] == 'cookie':
647+
headers['Cookie'] = auth_setting['value']
648+
elif auth_setting['in'] == 'header':
649+
if auth_setting['type'] != 'http-signature':
650+
headers[auth_setting['key']] = auth_setting['value']
634651
{{#hasHttpSignatureMethods}}
635-
else:
636-
# The HTTP signature scheme requires multiple HTTP headers
637-
# that are calculated dynamically.
638-
signing_info = self.configuration.signing_info
639-
auth_headers = signing_info.get_http_signature_headers(
640-
resource_path, method, headers, body, queries)
641-
headers.update(auth_headers)
652+
else:
653+
# The HTTP signature scheme requires multiple HTTP headers
654+
# that are calculated dynamically.
655+
signing_info = self.configuration.signing_info
656+
auth_headers = signing_info.get_http_signature_headers(
657+
resource_path, method, headers, body, queries)
658+
headers.update(auth_headers)
642659
{{/hasHttpSignatureMethods}}
643-
elif auth_setting['in'] == 'query':
644-
queries.append((auth_setting['key'], auth_setting['value']))
645-
else:
646-
raise ApiValueError(
647-
'Authentication token must be in `query` or `header`'
648-
)
660+
elif auth_setting['in'] == 'query':
661+
queries.append((auth_setting['key'], auth_setting['value']))
662+
else:
663+
raise ApiValueError(
664+
'Authentication token must be in `query` or `header`'
665+
)
649666

650667

651668
class Endpoint(object):
@@ -695,7 +712,8 @@ class Endpoint(object):
695712
'_check_input_type',
696713
'_check_return_type',
697714
'_content_type',
698-
'_spec_property_naming'
715+
'_spec_property_naming',
716+
'_request_auths'
699717
])
700718
self.params_map['nullable'].extend(['_request_timeout'])
701719
self.validations = root_map['validations']
@@ -710,7 +728,8 @@ class Endpoint(object):
710728
'_check_input_type': (bool,),
711729
'_check_return_type': (bool,),
712730
'_spec_property_naming': (bool,),
713-
'_content_type': (none_type, str)
731+
'_content_type': (none_type, str),
732+
'_request_auths': (none_type, list)
714733
}
715734
self.openapi_types.update(extra_types)
716735
self.attribute_map = root_map['attribute_map']
@@ -885,4 +904,5 @@ class Endpoint(object):
885904
_preload_content=kwargs['_preload_content'],
886905
_request_timeout=kwargs['_request_timeout'],
887906
_host=_host,
907+
_request_auths=kwargs['_request_auths'],
888908
collection_formats=params['collection_format'])

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ def call_123_test_special_tags(
129129
_host_index (int/None): specifies the index of the server
130130
that we want to use.
131131
Default is read from the configuration.
132+
_request_auths (list): set to override the auth_settings for an a single
133+
request; this effectively ignores the authentication
134+
in the spec for a single request.
135+
Default is None
132136
async_req (bool): execute request asynchronously
133137
134138
Returns:
@@ -160,6 +164,7 @@ def call_123_test_special_tags(
160164
kwargs['_content_type'] = kwargs.get(
161165
'_content_type')
162166
kwargs['_host_index'] = kwargs.get('_host_index')
167+
kwargs['_request_auths'] = kwargs.get('_request_auths', None)
163168
kwargs['body'] = \
164169
body
165170
return self.call_123_test_special_tags_endpoint.call_with_http_info(**kwargs)

0 commit comments

Comments
 (0)