forked from loafle/openapi-generator-original
[python] add method to set/get default configuration (#5315)
* [python] add method to set/get default configuration * [python] change method name and fix handling api_key * python: using modified deepcopy to set/get default configuration * python: update samples * python: update samples
This commit is contained in:
@@ -68,7 +68,7 @@ class ApiClient(object):
|
||||
def __init__(self, configuration=None, header_name=None, header_value=None,
|
||||
cookie=None, pool_threads=1):
|
||||
if configuration is None:
|
||||
configuration = Configuration()
|
||||
configuration = Configuration.get_default_copy()
|
||||
self.configuration = configuration
|
||||
self.pool_threads = pool_threads
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
import copy
|
||||
import logging
|
||||
import multiprocessing
|
||||
import sys
|
||||
@@ -83,6 +84,8 @@ class Configuration(object):
|
||||
)
|
||||
"""
|
||||
|
||||
_default = None
|
||||
|
||||
def __init__(self, host="http://petstore.swagger.io:80/v2",
|
||||
api_key=None, api_key_prefix=None,
|
||||
username=None, password=None,
|
||||
@@ -182,6 +185,45 @@ class Configuration(object):
|
||||
# Disable client side validation
|
||||
self.client_side_validation = True
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
cls = self.__class__
|
||||
result = cls.__new__(cls)
|
||||
memo[id(self)] = result
|
||||
for k, v in self.__dict__.items():
|
||||
if k not in ('logger', 'logger_file_handler'):
|
||||
setattr(result, k, copy.deepcopy(v, memo))
|
||||
# shallow copy of loggers
|
||||
result.logger = copy.copy(self.logger)
|
||||
# use setters to configure loggers
|
||||
result.logger_file = self.logger_file
|
||||
result.debug = self.debug
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def set_default(cls, default):
|
||||
"""Set default instance of configuration.
|
||||
|
||||
It stores default configuration, which can be
|
||||
returned by get_default_copy method.
|
||||
|
||||
:param default: object of Configuration
|
||||
"""
|
||||
cls._default = copy.deepcopy(default)
|
||||
|
||||
@classmethod
|
||||
def get_default_copy(cls):
|
||||
"""Return new instance of configuration.
|
||||
|
||||
This method returns newly created, based on default constructor,
|
||||
object of Configuration class or returns a copy of default
|
||||
configuration passed by the set_default method.
|
||||
|
||||
:return: The configuration object.
|
||||
"""
|
||||
if cls._default is not None:
|
||||
return copy.deepcopy(cls._default)
|
||||
return Configuration()
|
||||
|
||||
@property
|
||||
def logger_file(self):
|
||||
"""The logger file.
|
||||
|
||||
Reference in New Issue
Block a user