Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] add method to set/get default configuration #5315

Merged
merged 5 commits into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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.new_instance()
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,40 @@ class Configuration(object):
# Disable client side validation
self.client_side_validation = True

@classmethod
def copy(cls, source):
Copy link
Contributor

@spacether spacether Feb 22, 2020

Choose a reason for hiding this comment

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

How about changing this to:
__deepcopy__(self, memodict={})
That way you control how everything can be copied. For the logger only you can then make a new logger with the same properties as the old one. All other properties can be deep copied. You can use dir(self) or self.__dict__ to iterate over all of self's properties.

If you do it this way, then in new_instance you can return copy.deepcopy(cls._default)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I'd totally forgotten about this possibility. Thanks.

if source is None:
return None
ret = copy.copy(source)
ret.api_key = copy.copy(source.api_key)
ret.api_key_prefix = copy.copy(source.api_key_prefix)
return ret

@classmethod
def set_default(cls, default):
"""Set default instance of configuration.

It stores default configuration, which can be
returned by new_instance method.

:param default: object of Configuration
"""
cls._default = Configuration.copy(default)

@classmethod
def new_instance(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 Configuration.copy(cls._default)
return Configuration()
Copy link
Contributor

@spacether spacether Feb 22, 2020

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
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.new_instance()
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,40 @@ def __init__(self, host="http://petstore.swagger.io:80/v2",
# Disable client side validation
self.client_side_validation = True

@classmethod
def copy(cls, source):
if source is None:
return None
ret = copy.copy(source)
ret.api_key = copy.copy(source.api_key)
ret.api_key_prefix = copy.copy(source.api_key_prefix)
return ret

@classmethod
def set_default(cls, default):
"""Set default instance of configuration.

It stores default configuration, which can be
returned by new_instance method.

:param default: object of Configuration
"""
cls._default = Configuration.copy(default)

@classmethod
def new_instance(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 Configuration.copy(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,40 @@ def __init__(self, host="http://petstore.swagger.io:80/v2",
# Disable client side validation
self.client_side_validation = True

@classmethod
def copy(cls, source):
if source is None:
return None
ret = copy.copy(source)
ret.api_key = copy.copy(source.api_key)
ret.api_key_prefix = copy.copy(source.api_key_prefix)
return ret

@classmethod
def set_default(cls, default):
"""Set default instance of configuration.

It stores default configuration, which can be
returned by new_instance method.

:param default: object of Configuration
"""
cls._default = Configuration.copy(default)

@classmethod
def new_instance(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 Configuration.copy(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.new_instance()
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,40 @@ def __init__(self, host="http://petstore.swagger.io:80/v2",
# Disable client side validation
self.client_side_validation = True

@classmethod
def copy(cls, source):
if source is None:
return None
ret = copy.copy(source)
ret.api_key = copy.copy(source.api_key)
ret.api_key_prefix = copy.copy(source.api_key_prefix)
return ret

@classmethod
def set_default(cls, default):
"""Set default instance of configuration.

It stores default configuration, which can be
returned by new_instance method.

:param default: object of Configuration
"""
cls._default = Configuration.copy(default)

@classmethod
def new_instance(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 Configuration.copy(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.new_instance()
self.configuration = configuration
self.pool_threads = pool_threads

Expand Down
37 changes: 37 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,40 @@ def __init__(self, host="http://petstore.swagger.io:80/v2",
# Disable client side validation
self.client_side_validation = True

@classmethod
def copy(cls, source):
if source is None:
return None
ret = copy.copy(source)
ret.api_key = copy.copy(source.api_key)
ret.api_key_prefix = copy.copy(source.api_key_prefix)
return ret

@classmethod
def set_default(cls, default):
"""Set default instance of configuration.

It stores default configuration, which can be
returned by new_instance method.

:param default: object of Configuration
"""
cls._default = Configuration.copy(default)

@classmethod
def new_instance(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 Configuration.copy(cls._default)
return Configuration()

@property
def logger_file(self):
"""The logger file.
Expand Down
20 changes: 17 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,28 @@ 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")
petstore_api.Configuration.set_default(c1)

# get default configuration
c2 = petstore_api.Configuration.new_instance()
self.assertEqual(c2.host, "example.com")
spacether marked this conversation as resolved.
Show resolved Hide resolved

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