Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about?
default_copy = Configuration.get_default_copy()
configuration = Configuration() if default_copy is None else default_copy

self.configuration = configuration
self.pool_threads = pool_threads

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import absolute_import

import copy
import logging
{{^asyncio}}
import multiprocessing
Expand Down Expand Up @@ -117,6 +118,8 @@ class Configuration(object):
{{/hasAuthMethods}}
"""

_default = None

def __init__(self, host="{{{basePath}}}",
api_key=None, api_key_prefix=None,
username=None, password=None,
Expand Down Expand Up @@ -241,6 +244,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()

@spacether spacether Feb 22, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about returning None if the default copy is not present?
Then we can do truthy/none checks with the result.
This lets our users see and handle the case when a default is unset.

Or if we want to always return an instance how about naming it
default_copy_or_new_instance?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think get_default_copy is good enough for this usage.


@property
def logger_file(self):
"""The logger file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from __future__ import absolute_import

import copy
import logging
import sys
import urllib3
Expand Down Expand Up @@ -71,6 +72,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,
Expand Down Expand Up @@ -165,6 +168,45 @@ def __init__(self, host="http://petstore.swagger.io:80/v2",
# 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from __future__ import absolute_import

import copy
import logging
import multiprocessing
import sys
Expand Down Expand Up @@ -72,6 +73,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,
Expand Down Expand Up @@ -169,6 +172,45 @@ def __init__(self, host="http://petstore.swagger.io:80/v2",
# 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from __future__ import absolute_import

import copy
import logging
import multiprocessing
import sys
Expand Down Expand Up @@ -72,6 +73,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,
Expand Down Expand Up @@ -169,6 +172,45 @@ def __init__(self, host="http://petstore.swagger.io:80/v2",
# 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.
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/python/petstore_api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 42 additions & 0 deletions samples/client/petstore/python/petstore_api/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from __future__ import absolute_import

import copy
import logging
import multiprocessing
import sys
Expand Down Expand Up @@ -72,6 +73,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,
Expand Down Expand Up @@ -169,6 +172,45 @@ def __init__(self, host="http://petstore.swagger.io:80/v2",
# 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.
Expand Down
22 changes: 19 additions & 3 deletions samples/client/petstore/python/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,30 @@ def setUp(self):
pass

def tearDown(self):
pass
# reset Configuration
petstore_api.Configuration.set_default(None)

def testConfiguration(self):
# check that different instances use different dictionaries
c1 = petstore_api.Configuration()
c2 = petstore_api.Configuration()
assert id(c1.api_key) != id(c2.api_key)
assert id(c1.api_key_prefix) != id(c2.api_key_prefix)
self.assertNotEqual(id(c1.api_key), id(c2.api_key))
self.assertNotEqual(id(c1.api_key_prefix), id(c2.api_key_prefix))

def testDefaultConfiguration(self):

# prepare default configuration
c1 = petstore_api.Configuration(host="example.com")
c1.debug = True
petstore_api.Configuration.set_default(c1)

# get default configuration
c2 = petstore_api.Configuration.get_default_copy()
self.assertEqual(c2.host, "example.com")
Comment thread
spacether marked this conversation as resolved.
self.assertTrue(c2.debug)

self.assertNotEqual(id(c1.api_key), id(c2.api_key))
self.assertNotEqual(id(c1.api_key_prefix), id(c2.api_key_prefix))


if __name__ == '__main__':
Expand Down
Loading