mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-05-18 15:54:42 +00:00
added authentication for python client.
completed.
This commit is contained in:
@@ -81,9 +81,12 @@ class {{classname}}(object):
|
||||
# HTTP header `Content-Type`
|
||||
header_params['Content-Type'] = self.api_client.select_header_content_type([{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}])
|
||||
|
||||
# Authentication setting
|
||||
auth_settings = [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}]
|
||||
|
||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||
body=body_params, post_params=form_params, files=files,
|
||||
response={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}})
|
||||
response={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}}, auth_settings=auth_settings)
|
||||
{{#returnType}}
|
||||
return response
|
||||
{{/returnType}}{{/operation}}
|
||||
|
||||
@@ -1,2 +1,49 @@
|
||||
import base64
|
||||
|
||||
|
||||
def get_api_key_with_prefix(key):
|
||||
global api_key
|
||||
global api_key_prefix
|
||||
|
||||
if api_key_prefix[key]:
|
||||
return api_key_prefix[key] + ' ' + api_key[key]
|
||||
else:
|
||||
return api_key[key]
|
||||
|
||||
def get_basic_auth_token():
|
||||
global username
|
||||
global password
|
||||
|
||||
return base64.base64encode('Basic ' + username + password)
|
||||
|
||||
def auth_settings():
|
||||
return { {{#authMethods}}{{#isApiKey}}
|
||||
'{{name}}': {
|
||||
'type': 'api_key',
|
||||
'in': {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
|
||||
'key': '{{keyParamName}}',
|
||||
'value': get_api_key_with_prefix('{{keyParamName}}')
|
||||
},
|
||||
{{/isApiKey}}{{#isBasic}}
|
||||
'{{name}}': {
|
||||
'type': 'basic',
|
||||
'in': 'header',
|
||||
'key': 'Authorization',
|
||||
'value': get_basic_auth_token()
|
||||
},
|
||||
{{/isBasic}}{{/authMethods}}
|
||||
}
|
||||
|
||||
|
||||
|
||||
# Default Base url
|
||||
HOST = "{{basePath}}"
|
||||
host = "{{basePath}}"
|
||||
|
||||
# Authentication settings
|
||||
|
||||
api_key = {}
|
||||
api_key_prefix = {}
|
||||
username = ''
|
||||
password = ''
|
||||
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class ApiClient(object):
|
||||
:param header_name: a header to pass when making calls to the API
|
||||
:param header_value: a header value to pass when making calls to the API
|
||||
"""
|
||||
def __init__(self, host=config.HOST, header_name=None, header_value=None):
|
||||
def __init__(self, host=config.host, header_name=None, header_value=None):
|
||||
self.default_headers = {}
|
||||
if header_name is not None:
|
||||
self.default_headers[header_name] = header_value
|
||||
@@ -59,7 +59,7 @@ class ApiClient(object):
|
||||
self.default_headers[header_name] = header_value
|
||||
|
||||
def call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None,
|
||||
body=None, post_params=None, files=None, response=None):
|
||||
body=None, post_params=None, files=None, response=None, auth_settings=None):
|
||||
|
||||
# headers parameters
|
||||
headers = self.default_headers.copy()
|
||||
@@ -86,6 +86,9 @@ class ApiClient(object):
|
||||
post_params = self.prepare_post_parameters(post_params, files)
|
||||
post_params = self.sanitize_for_serialization(post_params)
|
||||
|
||||
# auth setting
|
||||
self.update_params_for_auth(header_params, query_params, auth_settings)
|
||||
|
||||
# body
|
||||
if body:
|
||||
body = self.sanitize_for_serialization(body)
|
||||
@@ -281,22 +284,21 @@ class ApiClient(object):
|
||||
else:
|
||||
return content_types[0]
|
||||
|
||||
def get_api_key_with_prefix(api_key):
|
||||
"""
|
||||
Get API key (with prefix if possible)
|
||||
"""
|
||||
if config.api_prefix[api_key]:
|
||||
return config.api_prefix[api_key] + config.api_key[api_key]
|
||||
else:
|
||||
return config.api_key[api_key]
|
||||
|
||||
def update_params_for_auth(headers, querys, auths):
|
||||
def update_params_for_auth(self, headers, querys, auth_settings):
|
||||
"""
|
||||
Update header and query params based on authentication setting
|
||||
"""
|
||||
auths_map = {
|
||||
{{#authMethods}}
|
||||
|
||||
{{/authMethods}}
|
||||
}
|
||||
if not auth_settings:
|
||||
return
|
||||
|
||||
for auth in auth_settings:
|
||||
auth_setting = config.auth_settings().get(auth)
|
||||
if auth_setting:
|
||||
if auth_setting['in'] == 'header':
|
||||
headers[auth_setting['key']] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'query':
|
||||
querys[auth_setting['key']] = auth_setting['value']
|
||||
else:
|
||||
raise ValueError('Authentication token must be in `query` or `header`')
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user