forked from loafle/openapi-generator-original
[Python] remove singleton (#5012)
* updated tests relying on configuration's singleton * regenerated samples * removed singleton from templates * regenerated samples * removed broken sample and its test * reverted accidental change * regenerated security/ samples * removed singleton from RESTClientObject * removed unused Configuration import * reverted unrelated change * updated usage examples in api_doc template
This commit is contained in:
@@ -23,5 +23,3 @@ from .apis.fake_api import FakeApi
|
||||
from .api_client import ApiClient
|
||||
|
||||
from .configuration import Configuration
|
||||
|
||||
configuration = Configuration()
|
||||
|
||||
@@ -59,18 +59,15 @@ class ApiClient(object):
|
||||
'object': object,
|
||||
}
|
||||
|
||||
def __init__(self, host=None, header_name=None, header_value=None, cookie=None):
|
||||
"""
|
||||
Constructor of the class.
|
||||
"""
|
||||
self.rest_client = RESTClientObject()
|
||||
def __init__(self, configuration=None, header_name=None, header_value=None, cookie=None):
|
||||
if configuration is None:
|
||||
configuration = Configuration()
|
||||
self.configuration = configuration
|
||||
|
||||
self.rest_client = RESTClientObject(configuration)
|
||||
self.default_headers = {}
|
||||
if header_name is not None:
|
||||
self.default_headers[header_name] = header_value
|
||||
if host is None:
|
||||
self.host = Configuration().host
|
||||
else:
|
||||
self.host = host
|
||||
self.cookie = cookie
|
||||
# Set default User-Agent.
|
||||
self.user_agent = 'Swagger-Codegen/1.0.0/python'
|
||||
@@ -139,7 +136,7 @@ class ApiClient(object):
|
||||
body = self.sanitize_for_serialization(body)
|
||||
|
||||
# request url
|
||||
url = self.host + resource_path
|
||||
url = self.configuration.host + resource_path
|
||||
|
||||
# perform request and return response
|
||||
response_data = self.request(method, url,
|
||||
@@ -496,13 +493,11 @@ class ApiClient(object):
|
||||
:param querys: Query parameters tuple list to be updated.
|
||||
:param auth_settings: Authentication setting identifiers list.
|
||||
"""
|
||||
config = Configuration()
|
||||
|
||||
if not auth_settings:
|
||||
return
|
||||
|
||||
for auth in auth_settings:
|
||||
auth_setting = config.auth_settings().get(auth)
|
||||
auth_setting = self.configuration.auth_settings().get(auth)
|
||||
if auth_setting:
|
||||
if not auth_setting['value']:
|
||||
continue
|
||||
@@ -523,9 +518,7 @@ class ApiClient(object):
|
||||
:param response: RESTResponse.
|
||||
:return: file path.
|
||||
"""
|
||||
config = Configuration()
|
||||
|
||||
fd, path = tempfile.mkstemp(dir=config.temp_folder_path)
|
||||
fd, path = tempfile.mkstemp(dir=self.configuration.temp_folder_path)
|
||||
os.close(fd)
|
||||
os.remove(path)
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ import re
|
||||
# python 2 and python 3 compatibility library
|
||||
from six import iteritems
|
||||
|
||||
from ..configuration import Configuration
|
||||
from ..api_client import ApiClient
|
||||
|
||||
|
||||
@@ -32,13 +31,9 @@ class FakeApi(object):
|
||||
"""
|
||||
|
||||
def __init__(self, api_client=None):
|
||||
config = Configuration()
|
||||
if api_client:
|
||||
self.api_client = api_client
|
||||
else:
|
||||
if not config.api_client:
|
||||
config.api_client = ApiClient()
|
||||
self.api_client = config.api_client
|
||||
if api_client is None:
|
||||
api_client = ApiClient()
|
||||
self.api_client = api_client
|
||||
|
||||
def test_code_inject____end__rn_n_r(self, **kwargs):
|
||||
"""
|
||||
|
||||
@@ -22,17 +22,6 @@ from six import iteritems
|
||||
from six.moves import http_client as httplib
|
||||
|
||||
|
||||
def singleton(cls, *args, **kw):
|
||||
instances = {}
|
||||
|
||||
def _singleton():
|
||||
if cls not in instances:
|
||||
instances[cls] = cls(*args, **kw)
|
||||
return instances[cls]
|
||||
return _singleton
|
||||
|
||||
|
||||
@singleton
|
||||
class Configuration(object):
|
||||
"""
|
||||
NOTE: This class is auto generated by the swagger code generator program.
|
||||
@@ -46,8 +35,7 @@ class Configuration(object):
|
||||
"""
|
||||
# Default Base url
|
||||
self.host = "https://petstore.swagger.io */ ' \" =end -- \\r\\n \\n \\r/v2 */ ' \" =end -- \\r\\n \\n \\r"
|
||||
# Default api client
|
||||
self.api_client = None
|
||||
|
||||
# Temp file folder for downloading files
|
||||
self.temp_folder_path = None
|
||||
|
||||
|
||||
@@ -24,8 +24,6 @@ import re
|
||||
from six import PY3
|
||||
from six.moves.urllib.parse import urlencode
|
||||
|
||||
from .configuration import Configuration
|
||||
|
||||
try:
|
||||
import urllib3
|
||||
except ImportError:
|
||||
@@ -58,7 +56,7 @@ class RESTResponse(io.IOBase):
|
||||
|
||||
class RESTClientObject(object):
|
||||
|
||||
def __init__(self, pools_size=4, maxsize=4):
|
||||
def __init__(self, configuration, pools_size=4, maxsize=4):
|
||||
# urllib3.PoolManager will pass all kw parameters to connectionpool
|
||||
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75
|
||||
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680
|
||||
@@ -67,37 +65,28 @@ class RESTClientObject(object):
|
||||
# http://stackoverflow.com/a/23957365/2985775
|
||||
|
||||
# cert_reqs
|
||||
if Configuration().verify_ssl:
|
||||
if configuration.verify_ssl:
|
||||
cert_reqs = ssl.CERT_REQUIRED
|
||||
else:
|
||||
cert_reqs = ssl.CERT_NONE
|
||||
|
||||
# ca_certs
|
||||
if Configuration().ssl_ca_cert:
|
||||
ca_certs = Configuration().ssl_ca_cert
|
||||
if configuration.ssl_ca_cert:
|
||||
ca_certs = configuration.ssl_ca_cert
|
||||
else:
|
||||
# if not set certificate file, use Mozilla's root certificates.
|
||||
ca_certs = certifi.where()
|
||||
|
||||
# cert_file
|
||||
cert_file = Configuration().cert_file
|
||||
|
||||
# key file
|
||||
key_file = Configuration().key_file
|
||||
|
||||
# proxy
|
||||
proxy = Configuration().proxy
|
||||
|
||||
# https pool manager
|
||||
if proxy:
|
||||
if configuration.proxy:
|
||||
self.pool_manager = urllib3.ProxyManager(
|
||||
num_pools=pools_size,
|
||||
maxsize=maxsize,
|
||||
cert_reqs=cert_reqs,
|
||||
ca_certs=ca_certs,
|
||||
cert_file=cert_file,
|
||||
key_file=key_file,
|
||||
proxy_url=proxy
|
||||
cert_file=configuration.cert_file,
|
||||
key_file=configuration.key_file,
|
||||
proxy_url=configuration.proxy
|
||||
)
|
||||
else:
|
||||
self.pool_manager = urllib3.PoolManager(
|
||||
@@ -105,19 +94,10 @@ class RESTClientObject(object):
|
||||
maxsize=maxsize,
|
||||
cert_reqs=cert_reqs,
|
||||
ca_certs=ca_certs,
|
||||
cert_file=cert_file,
|
||||
key_file=key_file
|
||||
cert_file=configuration.cert_file,
|
||||
key_file=configuration.key_file
|
||||
)
|
||||
|
||||
# https pool manager
|
||||
self.pool_manager = urllib3.PoolManager(
|
||||
num_pools=pools_size,
|
||||
cert_reqs=cert_reqs,
|
||||
ca_certs=ca_certs,
|
||||
cert_file=cert_file,
|
||||
key_file=key_file
|
||||
)
|
||||
|
||||
def request(self, method, url, query_params=None, headers=None,
|
||||
body=None, post_params=None, _preload_content=True, _request_timeout=None):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user