forked from loafle/openapi-generator-original
Update logging of python client
This commit is contained in:
@@ -8,3 +8,7 @@ from __future__ import absolute_import
|
||||
{{/apis}}{{/apiInfo}}
|
||||
# import ApiClient
|
||||
from .api_client import ApiClient
|
||||
|
||||
from .configuration import Configuration
|
||||
|
||||
configuration = Configuration()
|
||||
|
||||
@@ -27,19 +27,20 @@ import os
|
||||
# python 2 and python 3 compatibility library
|
||||
from six import iteritems
|
||||
|
||||
from .. import configuration
|
||||
from ..configuration import Configuration
|
||||
from ..api_client import ApiClient
|
||||
|
||||
{{#operations}}
|
||||
class {{classname}}(object):
|
||||
|
||||
def __init__(self, api_client=None):
|
||||
config = Configuration()
|
||||
if api_client:
|
||||
self.api_client = api_client
|
||||
else:
|
||||
if not configuration.api_client:
|
||||
configuration.api_client = ApiClient('{{basePath}}')
|
||||
self.api_client = configuration.api_client
|
||||
if not config.api_client:
|
||||
config.api_client = ApiClient('{{basePath}}')
|
||||
self.api_client = config.api_client
|
||||
|
||||
{{#operation}}
|
||||
def {{nickname}}(self, {{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}**kwargs):
|
||||
@@ -117,16 +118,7 @@ class {{classname}}(object):
|
||||
|
||||
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}}, auth_settings=auth_settings, callback=params.get('callback'))
|
||||
response_type={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}}, auth_settings=auth_settings, callback=params.get('callback'))
|
||||
return response
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ except ImportError:
|
||||
# for python2
|
||||
from urllib import quote
|
||||
|
||||
from . import configuration
|
||||
from .configuration import Configuration
|
||||
|
||||
class ApiClient(object):
|
||||
"""
|
||||
@@ -41,7 +41,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=configuration.host, header_name=None, header_value=None):
|
||||
def __init__(self, host=Configuration().host, header_name=None, header_value=None):
|
||||
self.default_headers = {}
|
||||
if header_name is not None:
|
||||
self.default_headers[header_name] = header_value
|
||||
@@ -62,7 +62,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, auth_settings=None, callback=None):
|
||||
body=None, post_params=None, files=None, response_type=None, auth_settings=None, callback=None):
|
||||
|
||||
# headers parameters
|
||||
header_params = header_params or {}
|
||||
@@ -221,7 +221,7 @@ class ApiClient(object):
|
||||
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, auth_settings=None, callback=None):
|
||||
response_type=None, auth_settings=None, callback=None):
|
||||
"""
|
||||
Perform http request and return deserialized data
|
||||
|
||||
@@ -247,13 +247,13 @@ class ApiClient(object):
|
||||
return self.__call_api(resource_path, method,
|
||||
path_params, query_params, header_params,
|
||||
body, post_params, files,
|
||||
response, auth_settings, callback)
|
||||
response_type, auth_settings, callback)
|
||||
else:
|
||||
thread = threading.Thread(target=self.__call_api,
|
||||
args=(resource_path, method,
|
||||
path_params, query_params, header_params,
|
||||
body, post_params, files,
|
||||
response, auth_settings, callback))
|
||||
response_type, auth_settings, callback))
|
||||
thread.start()
|
||||
return thread
|
||||
|
||||
@@ -326,11 +326,13 @@ class ApiClient(object):
|
||||
"""
|
||||
Update header and query params based on authentication setting
|
||||
"""
|
||||
config = Configuration()
|
||||
|
||||
if not auth_settings:
|
||||
return
|
||||
|
||||
for auth in auth_settings:
|
||||
auth_setting = configuration.auth_settings().get(auth)
|
||||
auth_setting = config.auth_settings().get(auth)
|
||||
if auth_setting:
|
||||
if auth_setting['in'] == 'header':
|
||||
headers[auth_setting['key']] = auth_setting['value']
|
||||
|
||||
@@ -5,73 +5,117 @@ import httplib
|
||||
import sys
|
||||
import logging
|
||||
|
||||
def get_api_key_with_prefix(key):
|
||||
global api_key
|
||||
global api_key_prefix
|
||||
def singleton(cls, *args, **kw):
|
||||
instances = {}
|
||||
|
||||
if api_key.get(key) and api_key_prefix.get(key):
|
||||
return api_key_prefix[key] + ' ' + api_key[key]
|
||||
elif api_key.get(key):
|
||||
return api_key[key]
|
||||
def _singleton():
|
||||
if cls not in instances:
|
||||
instances[cls] = cls(*args, **kw)
|
||||
return instances[cls]
|
||||
return _singleton
|
||||
|
||||
def setting_logging_enabled():
|
||||
global logging_file
|
||||
format = '%(asctime)s %(levelname)s %(message)s'
|
||||
if logging_file:
|
||||
logging.basicConfig(filename=logging_file, level=logging.DEBUG, format=format)
|
||||
else:
|
||||
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=format)
|
||||
httplib.HTTPConnection.debuglevel = 1
|
||||
|
||||
def to_debug_report():
|
||||
return "Python SDK Debug Report:\n"\
|
||||
"OS: {env}\n"\
|
||||
"Python Version: {pyversion}\n"\
|
||||
"Version of the API: {{version}}\n"\
|
||||
"SDK Package Version: {{packageVersion}}".format(env=sys.platform, pyversion=sys.version)
|
||||
@singleton
|
||||
class Configuration(object):
|
||||
|
||||
def get_basic_auth_token():
|
||||
global username
|
||||
global password
|
||||
def __init__(self):
|
||||
# Default Base url
|
||||
self.host = "{{basePath}}"
|
||||
# Default api client
|
||||
self.api_client = None
|
||||
# Authentication Settings
|
||||
self.api_key = {}
|
||||
self.api_key_prefix = {}
|
||||
self.username = ""
|
||||
self.password = ""
|
||||
# Logging Settings
|
||||
self.logging_format = '%(asctime)s %(levelname)s %(message)s'
|
||||
self.__logging_file = None
|
||||
self.__debug = False
|
||||
self.init_logger()
|
||||
|
||||
return urllib3.util.make_headers(basic_auth=username + ':' + password).get('authorization')
|
||||
def init_logger(self):
|
||||
self.logger = logging.getLogger()
|
||||
formatter = logging.Formatter(self.logging_format)
|
||||
stream_handler = logging.StreamHandler()
|
||||
stream_handler.setFormatter(formatter)
|
||||
self.logger.addHandler(stream_handler)
|
||||
if self.__debug:
|
||||
self.logger.setLevel(logging.DEBUG)
|
||||
else:
|
||||
self.logger.setLevel(logging.WARNING)
|
||||
if self.__logging_file:
|
||||
file_handler = logging.FileHandler(self.__logging_file)
|
||||
file_handler.setFormatter(formatter)
|
||||
self.logger.addFilter(file_handler)
|
||||
|
||||
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}}
|
||||
}
|
||||
@property
|
||||
def logging_file(self):
|
||||
return self.__logging_file
|
||||
|
||||
# Default Base url
|
||||
host = "{{basePath}}"
|
||||
@logging_file.setter
|
||||
def logging_file(self, value):
|
||||
self.__logging_file = value
|
||||
if self.__logging_file:
|
||||
formater = logging.Formatter(self.logging_format)
|
||||
file_handler = logging.FileHandler(self.__logging_file)
|
||||
file_handler.setFormatter(formater)
|
||||
self.logger.addHandler(file_handler)
|
||||
|
||||
# Default api client
|
||||
api_client = None
|
||||
|
||||
# Authentication settings
|
||||
@property
|
||||
def debug(self):
|
||||
return self.__debug
|
||||
|
||||
api_key = {}
|
||||
api_key_prefix = {}
|
||||
username = ''
|
||||
password = ''
|
||||
@debug.setter
|
||||
def debug(self, value):
|
||||
self.__debug = value
|
||||
if self.__debug:
|
||||
# if debug status is True, turn on debug logging
|
||||
self.logger.setLevel(logging.DEBUG)
|
||||
# turn on httplib debug
|
||||
httplib.HTTPConnection.debuglevel = 1
|
||||
else:
|
||||
# if debug status is False, turn off debug logging,
|
||||
# setting log level to default `logging.WARNING`
|
||||
self.logger.setLevel(logging.WARNING)
|
||||
|
||||
# Temp foloder for file download
|
||||
temp_folder_path = None
|
||||
|
||||
# Logging settings
|
||||
logging_file = None
|
||||
def get_api_key_with_prefix(self, key):
|
||||
""" Return api key prepend prefix for key """
|
||||
if self.api_key.get(key) and self.api_key_prefix.get(key):
|
||||
return self.api_key_prefix[key] + ' ' + self.api_key[key]
|
||||
elif self.api_key.get(key):
|
||||
return self.api_key[key]
|
||||
|
||||
def get_basic_auth_token(self):
|
||||
""" Return basic auth header string """
|
||||
return urllib3.util.make_headers(basic_auth=self.username + ':' + self.password)\
|
||||
.get('authorization')
|
||||
|
||||
def auth_settings(self):
|
||||
""" Return Auth Settings for api client """
|
||||
return { {{#authMethods}}{{#isApiKey}}
|
||||
'{{name}}': {
|
||||
'type': 'api_key',
|
||||
'in': {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
|
||||
'key': '{{keyParamName}}',
|
||||
'value': self.get_api_key_with_prefix('{{keyParamName}}')
|
||||
},
|
||||
{{/isApiKey}}{{#isBasic}}
|
||||
'{{name}}': {
|
||||
'type': 'basic',
|
||||
'in': 'header',
|
||||
'key': 'Authorization',
|
||||
'value': self.get_basic_auth_token()
|
||||
},
|
||||
{{/isBasic}}{{/authMethods}}
|
||||
}
|
||||
|
||||
def to_debug_report(self):
|
||||
return "Python SDK Debug Report:\n"\
|
||||
"OS: {env}\n"\
|
||||
"Python Version: {pyversion}\n"\
|
||||
"Version of the API: {{version}}\n"\
|
||||
"SDK Package Version: {{packageVersion}}".format(env=sys.platform, pyversion=sys.version)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -130,20 +130,14 @@ class RESTClientObject(object):
|
||||
headers=headers)
|
||||
r = RESTResponse(r)
|
||||
|
||||
# log response body
|
||||
logger.debug("response body: %s" % r.data)
|
||||
|
||||
if r.status not in range(200, 206):
|
||||
raise ApiException(r)
|
||||
|
||||
return self.process_response(r)
|
||||
|
||||
def process_response(self, response):
|
||||
# In the python 3, the response.data is bytes.
|
||||
# we need to decode it to string.
|
||||
if sys.version_info > (3,):
|
||||
r.data = r.data.decode('utf8')
|
||||
|
||||
# log response body
|
||||
logger.debug("response body: %s" % r.data)
|
||||
|
||||
if r.status not in range(200, 206):
|
||||
raise ApiException(http_resp=r)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user