From 8b891be7d7d42ce242f57c7b446d3fda3ac463da Mon Sep 17 00:00:00 2001 From: baartosz Date: Wed, 22 Mar 2017 14:25:46 +0000 Subject: [PATCH] [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 --- .../resources/python/__init__package.mustache | 2 - .../src/main/resources/python/api.mustache | 11 ++--- .../main/resources/python/api_client.mustache | 25 ++++------- .../main/resources/python/api_doc.mustache | 20 ++++++--- .../resources/python/configuration.mustache | 13 ------ .../src/main/resources/python/model.mustache | 2 +- .../src/main/resources/python/rest.mustache | 32 +++++--------- .../python/petstore_api/__init__.py | 2 - .../python/petstore_api/api_client.py | 25 ++++------- .../python/petstore_api/apis/fake_api.py | 11 ++--- .../python/petstore_api/configuration.py | 14 +------ .../python/petstore_api/rest.py | 40 +++++------------- samples/client/petstore/python/README.md | 1 - .../client/petstore/python/docs/FakeApi.md | 7 ++-- samples/client/petstore/python/docs/PetApi.md | 42 +++++++++++-------- .../client/petstore/python/docs/StoreApi.md | 7 ++-- .../petstore/python/petstore_api/__init__.py | 5 +-- .../python/petstore_api/api_client.py | 25 ++++------- .../python/petstore_api/apis/__init__.py | 2 +- .../python/petstore_api/apis/fake_api.py | 11 ++--- ...3_api.py => fake_classname_tags123_api.py} | 14 +++---- .../python/petstore_api/apis/pet_api.py | 11 ++--- .../python/petstore_api/apis/store_api.py | 13 +++--- .../python/petstore_api/apis/user_api.py | 11 ++--- .../python/petstore_api/configuration.py | 14 +------ .../python/petstore_api/models/__init__.py | 1 - .../petstore/python/petstore_api/rest.py | 32 +++++--------- ....py => test_fake_classname_tags123_api.py} | 17 ++------ .../petstore/python/tests/test_api_client.py | 23 ++++++---- .../petstore/python/tests/test_pet_api.py | 36 ++++++++-------- 30 files changed, 171 insertions(+), 298 deletions(-) rename samples/client/petstore/python/petstore_api/apis/{fake_classname_tags_123_api.py => fake_classname_tags123_api.py} (93%) rename samples/client/petstore/python/test/{test_fake_classname_tags_123_api.py => test_fake_classname_tags123_api.py} (53%) diff --git a/modules/swagger-codegen/src/main/resources/python/__init__package.mustache b/modules/swagger-codegen/src/main/resources/python/__init__package.mustache index 6528c480eed..2289de872ef 100644 --- a/modules/swagger-codegen/src/main/resources/python/__init__package.mustache +++ b/modules/swagger-codegen/src/main/resources/python/__init__package.mustache @@ -14,5 +14,3 @@ from __future__ import absolute_import from .api_client import ApiClient from .configuration import Configuration - -configuration = Configuration() diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index 77c17a2ef55..f6b4547a3a9 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -11,7 +11,6 @@ import re # python 2 and python 3 compatibility library from six import iteritems -from ..configuration import Configuration from ..api_client import ApiClient @@ -24,13 +23,9 @@ class {{classname}}(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 {{#operation}} def {{operationId}}(self, {{#sortParamsByRequiredFlag}}{{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}{{/sortParamsByRequiredFlag}}**kwargs): diff --git a/modules/swagger-codegen/src/main/resources/python/api_client.mustache b/modules/swagger-codegen/src/main/resources/python/api_client.mustache index 04b53afba9b..878f892a6ae 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -50,18 +50,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 = '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/python{{/httpUserAgent}}' @@ -130,7 +127,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, @@ -487,13 +484,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 @@ -514,9 +509,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) diff --git a/modules/swagger-codegen/src/main/resources/python/api_doc.mustache b/modules/swagger-codegen/src/main/resources/python/api_doc.mustache index 689e9ff2bb7..2f00740ab40 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_doc.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_doc.mustache @@ -26,20 +26,30 @@ from {{{packageName}}}.rest import ApiException from pprint import pprint {{#hasAuthMethods}}{{#authMethods}}{{#isBasic}} # Configure HTTP basic authorization: {{{name}}} -{{{packageName}}}.configuration.username = 'YOUR_USERNAME' -{{{packageName}}}.configuration.password = 'YOUR_PASSWORD'{{/isBasic}}{{#isApiKey}} +configuration = {{{packageName}}}.Configuration() +configuration.username = 'YOUR_USERNAME' +configuration.password = 'YOUR_PASSWORD'{{/isBasic}}{{#isApiKey}} # Configure API key authorization: {{{name}}} -{{{packageName}}}.configuration.api_key['{{{keyParamName}}}'] = 'YOUR_API_KEY' +configuration = {{{packageName}}}.Configuration() +configuration.api_key['{{{keyParamName}}}'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed -# {{{packageName}}}.configuration.api_key_prefix['{{{keyParamName}}}'] = 'Bearer'{{/isApiKey}}{{#isOAuth}} +# configuration.api_key_prefix['{{{keyParamName}}}'] = 'Bearer'{{/isApiKey}}{{#isOAuth}} # Configure OAuth2 access token for authorization: {{{name}}} -{{{packageName}}}.configuration.access_token = 'YOUR_ACCESS_TOKEN'{{/isOAuth}}{{/authMethods}} +configuration = {{{packageName}}}.Configuration() +configuration.access_token = 'YOUR_ACCESS_TOKEN'{{/isOAuth}}{{/authMethods}} + +# create an instance of the API class +api_instance = {{{packageName}}}.{{{classname}}}({{{packageName}}}.ApiClient(configuration)) +{{#allParams}}{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}} +{{/allParams}} {{/hasAuthMethods}} +{{^hasAuthMethods}} # create an instance of the API class api_instance = {{{packageName}}}.{{{classname}}}() {{#allParams}}{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}} {{/allParams}} +{{/hasAuthMethods}} try: {{#summary}} # {{{.}}} diff --git a/modules/swagger-codegen/src/main/resources/python/configuration.mustache b/modules/swagger-codegen/src/main/resources/python/configuration.mustache index 47c7db9fbad..b4ef5ad00b2 100644 --- a/modules/swagger-codegen/src/main/resources/python/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/python/configuration.mustache @@ -13,17 +13,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. @@ -37,8 +26,6 @@ class Configuration(object): """ # Default Base url self.host = "{{{basePath}}}" - # Default api client - self.api_client = None # Temp file folder for downloading files self.temp_folder_path = None diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index dcf3c755020..7faf9ae5b09 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -42,7 +42,7 @@ class {{classname}}(object): def {{name}}(self): """ Gets the {{name}} of this {{classname}}. -{{#description}} +{{#description}} {{{description}}} {{/description}} diff --git a/modules/swagger-codegen/src/main/resources/python/rest.mustache b/modules/swagger-codegen/src/main/resources/python/rest.mustache index 6c2c21d3055..4bbc16a2123 100644 --- a/modules/swagger-codegen/src/main/resources/python/rest.mustache +++ b/modules/swagger-codegen/src/main/resources/python/rest.mustache @@ -15,8 +15,6 @@ import re from six import PY3 from six.moves.urllib.parse import urlencode -from .configuration import Configuration - try: import urllib3 except ImportError: @@ -49,7 +47,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 @@ -58,37 +56,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( @@ -96,11 +85,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 ) - def request(self, method, url, query_params=None, headers=None, body=None, post_params=None, _preload_content=True, _request_timeout=None): """ diff --git a/samples/client/petstore-security-test/python/petstore_api/__init__.py b/samples/client/petstore-security-test/python/petstore_api/__init__.py index a8cf9ed66eb..0573fa17d0d 100644 --- a/samples/client/petstore-security-test/python/petstore_api/__init__.py +++ b/samples/client/petstore-security-test/python/petstore_api/__init__.py @@ -23,5 +23,3 @@ from .apis.fake_api import FakeApi from .api_client import ApiClient from .configuration import Configuration - -configuration = Configuration() diff --git a/samples/client/petstore-security-test/python/petstore_api/api_client.py b/samples/client/petstore-security-test/python/petstore_api/api_client.py index 95265e49715..5920274f9b5 100644 --- a/samples/client/petstore-security-test/python/petstore_api/api_client.py +++ b/samples/client/petstore-security-test/python/petstore_api/api_client.py @@ -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) diff --git a/samples/client/petstore-security-test/python/petstore_api/apis/fake_api.py b/samples/client/petstore-security-test/python/petstore_api/apis/fake_api.py index a6961921ac4..ababbfac013 100644 --- a/samples/client/petstore-security-test/python/petstore_api/apis/fake_api.py +++ b/samples/client/petstore-security-test/python/petstore_api/apis/fake_api.py @@ -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): """ diff --git a/samples/client/petstore-security-test/python/petstore_api/configuration.py b/samples/client/petstore-security-test/python/petstore_api/configuration.py index 99212e2f180..b1da47bc8c2 100644 --- a/samples/client/petstore-security-test/python/petstore_api/configuration.py +++ b/samples/client/petstore-security-test/python/petstore_api/configuration.py @@ -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 diff --git a/samples/client/petstore-security-test/python/petstore_api/rest.py b/samples/client/petstore-security-test/python/petstore_api/rest.py index 513bbb5aac7..4f7c42a576c 100644 --- a/samples/client/petstore-security-test/python/petstore_api/rest.py +++ b/samples/client/petstore-security-test/python/petstore_api/rest.py @@ -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): """ diff --git a/samples/client/petstore/python/README.md b/samples/client/petstore/python/README.md index 6272e435e2c..248a90d45af 100644 --- a/samples/client/petstore/python/README.md +++ b/samples/client/petstore/python/README.md @@ -104,7 +104,6 @@ Class | Method | HTTP request | Description - [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md) - [ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md) - [ArrayTest](docs/ArrayTest.md) - - [Capitalization](docs/Capitalization.md) - [Cat](docs/Cat.md) - [Category](docs/Category.md) - [ClassModel](docs/ClassModel.md) diff --git a/samples/client/petstore/python/docs/FakeApi.md b/samples/client/petstore/python/docs/FakeApi.md index 0a6770ff67d..5da0556764b 100644 --- a/samples/client/petstore/python/docs/FakeApi.md +++ b/samples/client/petstore/python/docs/FakeApi.md @@ -73,11 +73,12 @@ from petstore_api.rest import ApiException from pprint import pprint # Configure HTTP basic authorization: http_basic_test -petstore_api.configuration.username = 'YOUR_USERNAME' -petstore_api.configuration.password = 'YOUR_PASSWORD' +configuration = petstore_api.Configuration() +configuration.username = 'YOUR_USERNAME' +configuration.password = 'YOUR_PASSWORD' # create an instance of the API class -api_instance = petstore_api.FakeApi() +api_instance = petstore_api.FakeApi(petstore_api.ApiClient(configuration)) number = 3.4 # float | None double = 1.2 # float | None pattern_without_delimiter = 'pattern_without_delimiter_example' # str | None diff --git a/samples/client/petstore/python/docs/PetApi.md b/samples/client/petstore/python/docs/PetApi.md index 553c8152138..0a4e4083a51 100644 --- a/samples/client/petstore/python/docs/PetApi.md +++ b/samples/client/petstore/python/docs/PetApi.md @@ -30,10 +30,11 @@ from petstore_api.rest import ApiException from pprint import pprint # Configure OAuth2 access token for authorization: petstore_auth -petstore_api.configuration.access_token = 'YOUR_ACCESS_TOKEN' +configuration = petstore_api.Configuration() +configuration.access_token = 'YOUR_ACCESS_TOKEN' # create an instance of the API class -api_instance = petstore_api.PetApi() +api_instance = petstore_api.PetApi(petstore_api.ApiClient(configuration)) body = petstore_api.Pet() # Pet | Pet object that needs to be added to the store try: @@ -80,10 +81,11 @@ from petstore_api.rest import ApiException from pprint import pprint # Configure OAuth2 access token for authorization: petstore_auth -petstore_api.configuration.access_token = 'YOUR_ACCESS_TOKEN' +configuration = petstore_api.Configuration() +configuration.access_token = 'YOUR_ACCESS_TOKEN' # create an instance of the API class -api_instance = petstore_api.PetApi() +api_instance = petstore_api.PetApi(petstore_api.ApiClient(configuration)) pet_id = 789 # int | Pet id to delete api_key = 'api_key_example' # str | (optional) @@ -132,10 +134,11 @@ from petstore_api.rest import ApiException from pprint import pprint # Configure OAuth2 access token for authorization: petstore_auth -petstore_api.configuration.access_token = 'YOUR_ACCESS_TOKEN' +configuration = petstore_api.Configuration() +configuration.access_token = 'YOUR_ACCESS_TOKEN' # create an instance of the API class -api_instance = petstore_api.PetApi() +api_instance = petstore_api.PetApi(petstore_api.ApiClient(configuration)) status = ['status_example'] # list[str] | Status values that need to be considered for filter try: @@ -183,10 +186,11 @@ from petstore_api.rest import ApiException from pprint import pprint # Configure OAuth2 access token for authorization: petstore_auth -petstore_api.configuration.access_token = 'YOUR_ACCESS_TOKEN' +configuration = petstore_api.Configuration() +configuration.access_token = 'YOUR_ACCESS_TOKEN' # create an instance of the API class -api_instance = petstore_api.PetApi() +api_instance = petstore_api.PetApi(petstore_api.ApiClient(configuration)) tags = ['tags_example'] # list[str] | Tags to filter by try: @@ -234,12 +238,13 @@ from petstore_api.rest import ApiException from pprint import pprint # Configure API key authorization: api_key -petstore_api.configuration.api_key['api_key'] = 'YOUR_API_KEY' +configuration = petstore_api.Configuration() +configuration.api_key['api_key'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed -# petstore_api.configuration.api_key_prefix['api_key'] = 'Bearer' +# configuration.api_key_prefix['api_key'] = 'Bearer' # create an instance of the API class -api_instance = petstore_api.PetApi() +api_instance = petstore_api.PetApi(petstore_api.ApiClient(configuration)) pet_id = 789 # int | ID of pet to return try: @@ -287,10 +292,11 @@ from petstore_api.rest import ApiException from pprint import pprint # Configure OAuth2 access token for authorization: petstore_auth -petstore_api.configuration.access_token = 'YOUR_ACCESS_TOKEN' +configuration = petstore_api.Configuration() +configuration.access_token = 'YOUR_ACCESS_TOKEN' # create an instance of the API class -api_instance = petstore_api.PetApi() +api_instance = petstore_api.PetApi(petstore_api.ApiClient(configuration)) body = petstore_api.Pet() # Pet | Pet object that needs to be added to the store try: @@ -337,10 +343,11 @@ from petstore_api.rest import ApiException from pprint import pprint # Configure OAuth2 access token for authorization: petstore_auth -petstore_api.configuration.access_token = 'YOUR_ACCESS_TOKEN' +configuration = petstore_api.Configuration() +configuration.access_token = 'YOUR_ACCESS_TOKEN' # create an instance of the API class -api_instance = petstore_api.PetApi() +api_instance = petstore_api.PetApi(petstore_api.ApiClient(configuration)) pet_id = 789 # int | ID of pet that needs to be updated name = 'name_example' # str | Updated name of the pet (optional) status = 'status_example' # str | Updated status of the pet (optional) @@ -391,10 +398,11 @@ from petstore_api.rest import ApiException from pprint import pprint # Configure OAuth2 access token for authorization: petstore_auth -petstore_api.configuration.access_token = 'YOUR_ACCESS_TOKEN' +configuration = petstore_api.Configuration() +configuration.access_token = 'YOUR_ACCESS_TOKEN' # create an instance of the API class -api_instance = petstore_api.PetApi() +api_instance = petstore_api.PetApi(petstore_api.ApiClient(configuration)) pet_id = 789 # int | ID of pet to update additional_metadata = 'additional_metadata_example' # str | Additional data to pass to server (optional) file = '/path/to/file.txt' # file | file to upload (optional) diff --git a/samples/client/petstore/python/docs/StoreApi.md b/samples/client/petstore/python/docs/StoreApi.md index 54007c9e8e7..8b366896834 100644 --- a/samples/client/petstore/python/docs/StoreApi.md +++ b/samples/client/petstore/python/docs/StoreApi.md @@ -73,12 +73,13 @@ from petstore_api.rest import ApiException from pprint import pprint # Configure API key authorization: api_key -petstore_api.configuration.api_key['api_key'] = 'YOUR_API_KEY' +configuration = petstore_api.Configuration() +configuration.api_key['api_key'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed -# petstore_api.configuration.api_key_prefix['api_key'] = 'Bearer' +# configuration.api_key_prefix['api_key'] = 'Bearer' # create an instance of the API class -api_instance = petstore_api.StoreApi() +api_instance = petstore_api.StoreApi(petstore_api.ApiClient(configuration)) try: # Returns pet inventories by status diff --git a/samples/client/petstore/python/petstore_api/__init__.py b/samples/client/petstore/python/petstore_api/__init__.py index f86e57eaeac..10369ffc23e 100644 --- a/samples/client/petstore/python/petstore_api/__init__.py +++ b/samples/client/petstore/python/petstore_api/__init__.py @@ -21,7 +21,6 @@ from .models.api_response import ApiResponse from .models.array_of_array_of_number_only import ArrayOfArrayOfNumberOnly from .models.array_of_number_only import ArrayOfNumberOnly from .models.array_test import ArrayTest -from .models.capitalization import Capitalization from .models.cat import Cat from .models.category import Category from .models.class_model import ClassModel @@ -49,7 +48,7 @@ from .models.user import User # import apis into sdk package from .apis.fake_api import FakeApi -from .apis.fake_classname_tags_123_api import FakeClassnameTags123Api +from .apis.fake_classname_tags123_api import FakeClassnameTags123Api from .apis.pet_api import PetApi from .apis.store_api import StoreApi from .apis.user_api import UserApi @@ -58,5 +57,3 @@ from .apis.user_api import UserApi from .api_client import ApiClient from .configuration import Configuration - -configuration = Configuration() diff --git a/samples/client/petstore/python/petstore_api/api_client.py b/samples/client/petstore/python/petstore_api/api_client.py index 620bb3e4bc8..c3c6cfda743 100644 --- a/samples/client/petstore/python/petstore_api/api_client.py +++ b/samples/client/petstore/python/petstore_api/api_client.py @@ -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) diff --git a/samples/client/petstore/python/petstore_api/apis/__init__.py b/samples/client/petstore/python/petstore_api/apis/__init__.py index af71d636229..a889c4b2544 100644 --- a/samples/client/petstore/python/petstore_api/apis/__init__.py +++ b/samples/client/petstore/python/petstore_api/apis/__init__.py @@ -2,7 +2,7 @@ from __future__ import absolute_import # import apis into api package from .fake_api import FakeApi -from .fake_classname_tags_123_api import FakeClassnameTags123Api +from .fake_classname_tags123_api import FakeClassnameTags123Api from .pet_api import PetApi from .store_api import StoreApi from .user_api import UserApi diff --git a/samples/client/petstore/python/petstore_api/apis/fake_api.py b/samples/client/petstore/python/petstore_api/apis/fake_api.py index c4563b41c75..eda9f68b266 100644 --- a/samples/client/petstore/python/petstore_api/apis/fake_api.py +++ b/samples/client/petstore/python/petstore_api/apis/fake_api.py @@ -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_client_model(self, body, **kwargs): """ diff --git a/samples/client/petstore/python/petstore_api/apis/fake_classname_tags_123_api.py b/samples/client/petstore/python/petstore_api/apis/fake_classname_tags123_api.py similarity index 93% rename from samples/client/petstore/python/petstore_api/apis/fake_classname_tags_123_api.py rename to samples/client/petstore/python/petstore_api/apis/fake_classname_tags123_api.py index 429f3ad8819..4fe6106e00f 100644 --- a/samples/client/petstore/python/petstore_api/apis/fake_classname_tags_123_api.py +++ b/samples/client/petstore/python/petstore_api/apis/fake_classname_tags123_api.py @@ -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 FakeClassnameTags123Api(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_classname(self, body, **kwargs): """ @@ -106,6 +101,7 @@ class FakeClassnameTags123Api(object): collection_formats = {} + resource_path = '/fake_classname_test'.replace('{format}', 'json') path_params = {} query_params = {} @@ -129,7 +125,7 @@ class FakeClassnameTags123Api(object): # Authentication setting auth_settings = [] - return self.api_client.call_api('/fake_classname_test', 'PATCH', + return self.api_client.call_api(resource_path, 'PATCH', path_params, query_params, header_params, diff --git a/samples/client/petstore/python/petstore_api/apis/pet_api.py b/samples/client/petstore/python/petstore_api/apis/pet_api.py index 4cca462f7db..ccd83dae12d 100644 --- a/samples/client/petstore/python/petstore_api/apis/pet_api.py +++ b/samples/client/petstore/python/petstore_api/apis/pet_api.py @@ -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 PetApi(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 add_pet(self, body, **kwargs): """ diff --git a/samples/client/petstore/python/petstore_api/apis/store_api.py b/samples/client/petstore/python/petstore_api/apis/store_api.py index c3f1b7f77bd..84470c48fb4 100644 --- a/samples/client/petstore/python/petstore_api/apis/store_api.py +++ b/samples/client/petstore/python/petstore_api/apis/store_api.py @@ -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 StoreApi(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 delete_order(self, order_id, **kwargs): """ @@ -105,6 +100,8 @@ class StoreApi(object): if ('order_id' not in params) or (params['order_id'] is None): raise ValueError("Missing the required parameter `order_id` when calling `delete_order`") + if 'order_id' in params and params['order_id'] < 1.0: + raise ValueError("Invalid value for parameter `order_id` when calling `delete_order`, must be a value greater than or equal to `1.0`") collection_formats = {} diff --git a/samples/client/petstore/python/petstore_api/apis/user_api.py b/samples/client/petstore/python/petstore_api/apis/user_api.py index 5fe1fb6f2a4..693a9013642 100644 --- a/samples/client/petstore/python/petstore_api/apis/user_api.py +++ b/samples/client/petstore/python/petstore_api/apis/user_api.py @@ -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 UserApi(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 create_user(self, body, **kwargs): """ diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index d736f570be5..8f90978db54 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -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 = "http://petstore.swagger.io/v2" - # Default api client - self.api_client = None + # Temp file folder for downloading files self.temp_folder_path = None diff --git a/samples/client/petstore/python/petstore_api/models/__init__.py b/samples/client/petstore/python/petstore_api/models/__init__.py index f2a15c2d194..844bc150b72 100644 --- a/samples/client/petstore/python/petstore_api/models/__init__.py +++ b/samples/client/petstore/python/petstore_api/models/__init__.py @@ -21,7 +21,6 @@ from .api_response import ApiResponse from .array_of_array_of_number_only import ArrayOfArrayOfNumberOnly from .array_of_number_only import ArrayOfNumberOnly from .array_test import ArrayTest -from .capitalization import Capitalization from .cat import Cat from .category import Category from .class_model import ClassModel diff --git a/samples/client/petstore/python/petstore_api/rest.py b/samples/client/petstore/python/petstore_api/rest.py index 3ed3cf31849..6618e2516ee 100644 --- a/samples/client/petstore/python/petstore_api/rest.py +++ b/samples/client/petstore/python/petstore_api/rest.py @@ -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,11 +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 ) - def request(self, method, url, query_params=None, headers=None, body=None, post_params=None, _preload_content=True, _request_timeout=None): """ diff --git a/samples/client/petstore/python/test/test_fake_classname_tags_123_api.py b/samples/client/petstore/python/test/test_fake_classname_tags123_api.py similarity index 53% rename from samples/client/petstore/python/test/test_fake_classname_tags_123_api.py rename to samples/client/petstore/python/test/test_fake_classname_tags123_api.py index c0ca01d53d4..2dab6a38fc5 100644 --- a/samples/client/petstore/python/test/test_fake_classname_tags_123_api.py +++ b/samples/client/petstore/python/test/test_fake_classname_tags123_api.py @@ -8,20 +8,9 @@ OpenAPI spec version: 1.0.0 Contact: apiteam@swagger.io Generated by: https://github.com/swagger-api/swagger-codegen.git - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. """ + from __future__ import absolute_import import os @@ -30,14 +19,14 @@ import unittest import petstore_api from petstore_api.rest import ApiException -from petstore_api.apis.fake_classname_tags_123_api import FakeClassnameTags123Api +from petstore_api.apis.fake_classname_tags123_api import FakeClassnameTags123Api class TestFakeClassnameTags123Api(unittest.TestCase): """ FakeClassnameTags123Api unit test stubs """ def setUp(self): - self.api = petstore_api.apis.fake_classname_tags_123_api.FakeClassnameTags123Api() + self.api = petstore_api.apis.fake_classname_tags123_api.FakeClassnameTags123Api() def tearDown(self): pass diff --git a/samples/client/petstore/python/tests/test_api_client.py b/samples/client/petstore/python/tests/test_api_client.py index 972175835f3..aa42eeae4ba 100644 --- a/samples/client/petstore/python/tests/test_api_client.py +++ b/samples/client/petstore/python/tests/test_api_client.py @@ -21,23 +21,28 @@ HOST = 'http://petstore.swagger.io/v2' class ApiClientTests(unittest.TestCase): def setUp(self): - self.api_client = petstore_api.ApiClient(HOST) + self.api_client = petstore_api.ApiClient() def test_configuration(self): - petstore_api.configuration.api_key['api_key'] = '123456' - petstore_api.configuration.api_key_prefix['api_key'] = 'PREFIX' - petstore_api.configuration.username = 'test_username' - petstore_api.configuration.password = 'test_password' + config = petstore_api.Configuration() + config.host = 'http://localhost/' + + config.api_key['api_key'] = '123456' + config.api_key_prefix['api_key'] = 'PREFIX' + config.username = 'test_username' + config.password = 'test_password' header_params = {'test1': 'value1'} query_params = {'test2': 'value2'} auth_settings = ['api_key', 'unknown'] + client = petstore_api.ApiClient(config) + # test prefix - self.assertEqual('PREFIX', petstore_api.configuration.api_key_prefix['api_key']) + self.assertEqual('PREFIX', client.configuration.api_key_prefix['api_key']) # update parameters based on auth setting - self.api_client.update_params_for_auth(header_params, query_params, auth_settings) + client.update_params_for_auth(header_params, query_params, auth_settings) # test api key auth self.assertEqual(header_params['test1'], 'value1') @@ -45,8 +50,8 @@ class ApiClientTests(unittest.TestCase): self.assertEqual(query_params['test2'], 'value2') # test basic auth - self.assertEqual('test_username', petstore_api.configuration.username) - self.assertEqual('test_password', petstore_api.configuration.password) + self.assertEqual('test_username', client.configuration.username) + self.assertEqual('test_password', client.configuration.password) def test_select_header_accept(self): accepts = ['APPLICATION/JSON', 'APPLICATION/XML'] diff --git a/samples/client/petstore/python/tests/test_pet_api.py b/samples/client/petstore/python/tests/test_pet_api.py index 57cb40f9596..371a252a467 100644 --- a/samples/client/petstore/python/tests/test_pet_api.py +++ b/samples/client/petstore/python/tests/test_pet_api.py @@ -11,6 +11,7 @@ import os import unittest import petstore_api +from petstore_api import Configuration from petstore_api.rest import ApiException from .util import id_gen @@ -50,7 +51,9 @@ class MockPoolManager(object): class PetApiTests(unittest.TestCase): def setUp(self): - self.api_client = petstore_api.ApiClient(HOST) + config = Configuration() + config.host = HOST + self.api_client = petstore_api.ApiClient(config) self.pet_api = petstore_api.PetApi(self.api_client) self.setUpModels() self.setUpFiles() @@ -117,25 +120,24 @@ class PetApiTests(unittest.TestCase): self.pet_api.add_pet(body=self.pet, _request_timeout=5) self.pet_api.add_pet(body=self.pet, _request_timeout=(1, 2)) - def test_create_api_instance(self): + def test_separate_default_client_instances(self): pet_api = petstore_api.PetApi() pet_api2 = petstore_api.PetApi() - api_client3 = petstore_api.ApiClient() - api_client3.user_agent = 'api client 3' - api_client4 = petstore_api.ApiClient() - api_client4.user_agent = 'api client 4' - pet_api3 = petstore_api.PetApi(api_client3) + self.assertNotEqual(pet_api.api_client, pet_api2.api_client) - # same default api client - self.assertEqual(pet_api.api_client, pet_api2.api_client) - # confirm using the default api client in the config module - self.assertEqual(pet_api.api_client, petstore_api.configuration.api_client) - # 2 different api clients are not the same - self.assertNotEqual(api_client3, api_client4) - # customized pet api not using the default api client - self.assertNotEqual(pet_api3.api_client, petstore_api.configuration.api_client) - # customized pet api not using the old pet api's api client - self.assertNotEqual(pet_api3.api_client, pet_api2.api_client) + pet_api.api_client.user_agent = 'api client 3' + pet_api2.api_client.user_agent = 'api client 4' + + self.assertNotEqual(pet_api.api_client.user_agent, pet_api2.api_client.user_agent) + + def test_separate_default_config_instances(self): + pet_api = petstore_api.PetApi() + pet_api2 = petstore_api.PetApi() + self.assertNotEqual(pet_api.api_client.configuration, pet_api2.api_client.configuration) + + pet_api.api_client.configuration.host = 'somehost' + pet_api2.api_client.configuration.host = 'someotherhost' + self.assertNotEqual(pet_api.api_client.configuration.host, pet_api2.api_client.configuration.host) def test_async_request(self): self.pet_api.add_pet(body=self.pet)