[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:
Tomasz Prus
2020-02-23 21:10:44 +01:00
committed by GitHub
parent e4823cf4e6
commit 3f0c163f0c
13 changed files with 318 additions and 8 deletions

View File

@@ -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

View File

@@ -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.