Skip to content

Commit

Permalink
[python] add method to set/get default configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
tomplus committed Feb 16, 2020
1 parent 04e8fe0 commit 70a5854
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 8 deletions.
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_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,31 @@ class Configuration(object):
# Disable client side validation
self.client_side_validation = True

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

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

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

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

@classmethod
def set_default(cls, default):
"""Set default instance of configuration.
It stores default configuration, which can be
returned by get_instance method.
:param default: object of Configuration
"""
cls._default = copy.copy(default)

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

@classmethod
def set_default(cls, default):
"""Set default instance of configuration.
It stores default configuration, which can be
returned by get_instance method.
:param default: object of Configuration
"""
cls._default = copy.copy(default)

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

@classmethod
def set_default(cls, default):
"""Set default instance of configuration.
It stores default configuration, which can be
returned by get_instance method.
:param default: object of Configuration
"""
cls._default = copy.copy(default)

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

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

@classmethod
def set_default(cls, default):
"""Set default instance of configuration.
It stores default configuration, which can be
returned by get_instance method.
:param default: object of Configuration
"""
cls._default = copy.copy(default)

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

@property
def logger_file(self):
"""The logger file.
Expand Down
17 changes: 14 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,25 @@ 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.get_instance()
self.assertEqual(c2.host, "example.com")


if __name__ == '__main__':
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 @@ -113,6 +114,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 @@ -216,6 +219,31 @@ def __init__(self, host="http://petstore.swagger.io:80/v2",
# Disable client side validation
self.client_side_validation = True

@classmethod
def set_default(cls, default):
"""Set default instance of configuration.
It stores default configuration, which can be
returned by get_instance method.
:param default: object of Configuration
"""
cls._default = copy.copy(default)

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

Expand Down
Loading

0 comments on commit 70a5854

Please sign in to comment.