This commit is contained in:
wing328 2017-03-22 23:03:20 +08:00
commit bf7a2b22cd
30 changed files with 171 additions and 298 deletions

View File

@ -14,5 +14,3 @@ from __future__ import absolute_import
from .api_client import ApiClient from .api_client import ApiClient
from .configuration import Configuration from .configuration import Configuration
configuration = Configuration()

View File

@ -11,7 +11,6 @@ import re
# python 2 and python 3 compatibility library # python 2 and python 3 compatibility library
from six import iteritems from six import iteritems
from ..configuration import Configuration
from ..api_client import ApiClient from ..api_client import ApiClient
@ -24,13 +23,9 @@ class {{classname}}(object):
""" """
def __init__(self, api_client=None): def __init__(self, api_client=None):
config = Configuration() if api_client is None:
if api_client: api_client = ApiClient()
self.api_client = api_client self.api_client = api_client
else:
if not config.api_client:
config.api_client = ApiClient()
self.api_client = config.api_client
{{#operation}} {{#operation}}
def {{operationId}}(self, {{#sortParamsByRequiredFlag}}{{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}{{/sortParamsByRequiredFlag}}**kwargs): def {{operationId}}(self, {{#sortParamsByRequiredFlag}}{{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}{{/sortParamsByRequiredFlag}}**kwargs):

View File

@ -50,18 +50,15 @@ class ApiClient(object):
'object': object, 'object': object,
} }
def __init__(self, host=None, header_name=None, header_value=None, cookie=None): def __init__(self, configuration=None, header_name=None, header_value=None, cookie=None):
""" if configuration is None:
Constructor of the class. configuration = Configuration()
""" self.configuration = configuration
self.rest_client = RESTClientObject()
self.rest_client = RESTClientObject(configuration)
self.default_headers = {} self.default_headers = {}
if header_name is not None: if header_name is not None:
self.default_headers[header_name] = header_value self.default_headers[header_name] = header_value
if host is None:
self.host = Configuration().host
else:
self.host = host
self.cookie = cookie self.cookie = cookie
# Set default User-Agent. # Set default User-Agent.
self.user_agent = '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/python{{/httpUserAgent}}' self.user_agent = '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/python{{/httpUserAgent}}'
@ -130,7 +127,7 @@ class ApiClient(object):
body = self.sanitize_for_serialization(body) body = self.sanitize_for_serialization(body)
# request url # request url
url = self.host + resource_path url = self.configuration.host + resource_path
# perform request and return response # perform request and return response
response_data = self.request(method, url, response_data = self.request(method, url,
@ -487,13 +484,11 @@ class ApiClient(object):
:param querys: Query parameters tuple list to be updated. :param querys: Query parameters tuple list to be updated.
:param auth_settings: Authentication setting identifiers list. :param auth_settings: Authentication setting identifiers list.
""" """
config = Configuration()
if not auth_settings: if not auth_settings:
return return
for auth in auth_settings: for auth in auth_settings:
auth_setting = config.auth_settings().get(auth) auth_setting = self.configuration.auth_settings().get(auth)
if auth_setting: if auth_setting:
if not auth_setting['value']: if not auth_setting['value']:
continue continue
@ -514,9 +509,7 @@ class ApiClient(object):
:param response: RESTResponse. :param response: RESTResponse.
:return: file path. :return: file path.
""" """
config = Configuration() fd, path = tempfile.mkstemp(dir=self.configuration.temp_folder_path)
fd, path = tempfile.mkstemp(dir=config.temp_folder_path)
os.close(fd) os.close(fd)
os.remove(path) os.remove(path)

View File

@ -26,20 +26,30 @@ from {{{packageName}}}.rest import ApiException
from pprint import pprint from pprint import pprint
{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}} {{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}
# Configure HTTP basic authorization: {{{name}}} # Configure HTTP basic authorization: {{{name}}}
{{{packageName}}}.configuration.username = 'YOUR_USERNAME' configuration = {{{packageName}}}.Configuration()
{{{packageName}}}.configuration.password = 'YOUR_PASSWORD'{{/isBasic}}{{#isApiKey}} configuration.username = 'YOUR_USERNAME'
configuration.password = 'YOUR_PASSWORD'{{/isBasic}}{{#isApiKey}}
# Configure API key authorization: {{{name}}} # 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 # 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}}} # 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}}
{{^hasAuthMethods}}
# create an instance of the API class # create an instance of the API class
api_instance = {{{packageName}}}.{{{classname}}}() api_instance = {{{packageName}}}.{{{classname}}}()
{{#allParams}}{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}} {{#allParams}}{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
{{/allParams}} {{/allParams}}
{{/hasAuthMethods}}
try: try:
{{#summary}} # {{{.}}} {{#summary}} # {{{.}}}

View File

@ -13,17 +13,6 @@ from six import iteritems
from six.moves import http_client as httplib 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): class Configuration(object):
""" """
NOTE: This class is auto generated by the swagger code generator program. NOTE: This class is auto generated by the swagger code generator program.
@ -37,8 +26,6 @@ class Configuration(object):
""" """
# Default Base url # Default Base url
self.host = "{{{basePath}}}" self.host = "{{{basePath}}}"
# Default api client
self.api_client = None
# Temp file folder for downloading files # Temp file folder for downloading files
self.temp_folder_path = None self.temp_folder_path = None

View File

@ -42,7 +42,7 @@ class {{classname}}(object):
def {{name}}(self): def {{name}}(self):
""" """
Gets the {{name}} of this {{classname}}. Gets the {{name}} of this {{classname}}.
{{#description}} {{#description}}
{{{description}}} {{{description}}}
{{/description}} {{/description}}

View File

@ -15,8 +15,6 @@ import re
from six import PY3 from six import PY3
from six.moves.urllib.parse import urlencode from six.moves.urllib.parse import urlencode
from .configuration import Configuration
try: try:
import urllib3 import urllib3
except ImportError: except ImportError:
@ -49,7 +47,7 @@ class RESTResponse(io.IOBase):
class RESTClientObject(object): 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 # 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/poolmanager.py#L75
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680
@ -58,37 +56,28 @@ class RESTClientObject(object):
# http://stackoverflow.com/a/23957365/2985775 # http://stackoverflow.com/a/23957365/2985775
# cert_reqs # cert_reqs
if Configuration().verify_ssl: if configuration.verify_ssl:
cert_reqs = ssl.CERT_REQUIRED cert_reqs = ssl.CERT_REQUIRED
else: else:
cert_reqs = ssl.CERT_NONE cert_reqs = ssl.CERT_NONE
# ca_certs # ca_certs
if Configuration().ssl_ca_cert: if configuration.ssl_ca_cert:
ca_certs = Configuration().ssl_ca_cert ca_certs = configuration.ssl_ca_cert
else: else:
# if not set certificate file, use Mozilla's root certificates. # if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where() 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 # https pool manager
if proxy: if configuration.proxy:
self.pool_manager = urllib3.ProxyManager( self.pool_manager = urllib3.ProxyManager(
num_pools=pools_size, num_pools=pools_size,
maxsize=maxsize, maxsize=maxsize,
cert_reqs=cert_reqs, cert_reqs=cert_reqs,
ca_certs=ca_certs, ca_certs=ca_certs,
cert_file=cert_file, cert_file=configuration.cert_file,
key_file=key_file, key_file=configuration.key_file,
proxy_url=proxy proxy_url=configuration.proxy
) )
else: else:
self.pool_manager = urllib3.PoolManager( self.pool_manager = urllib3.PoolManager(
@ -96,11 +85,10 @@ class RESTClientObject(object):
maxsize=maxsize, maxsize=maxsize,
cert_reqs=cert_reqs, cert_reqs=cert_reqs,
ca_certs=ca_certs, ca_certs=ca_certs,
cert_file=cert_file, cert_file=configuration.cert_file,
key_file=key_file key_file=configuration.key_file
) )
def request(self, method, url, query_params=None, headers=None, def request(self, method, url, query_params=None, headers=None,
body=None, post_params=None, _preload_content=True, _request_timeout=None): body=None, post_params=None, _preload_content=True, _request_timeout=None):
""" """

View File

@ -23,5 +23,3 @@ from .apis.fake_api import FakeApi
from .api_client import ApiClient from .api_client import ApiClient
from .configuration import Configuration from .configuration import Configuration
configuration = Configuration()

View File

@ -59,18 +59,15 @@ class ApiClient(object):
'object': object, 'object': object,
} }
def __init__(self, host=None, header_name=None, header_value=None, cookie=None): def __init__(self, configuration=None, header_name=None, header_value=None, cookie=None):
""" if configuration is None:
Constructor of the class. configuration = Configuration()
""" self.configuration = configuration
self.rest_client = RESTClientObject()
self.rest_client = RESTClientObject(configuration)
self.default_headers = {} self.default_headers = {}
if header_name is not None: if header_name is not None:
self.default_headers[header_name] = header_value self.default_headers[header_name] = header_value
if host is None:
self.host = Configuration().host
else:
self.host = host
self.cookie = cookie self.cookie = cookie
# Set default User-Agent. # Set default User-Agent.
self.user_agent = 'Swagger-Codegen/1.0.0/python' self.user_agent = 'Swagger-Codegen/1.0.0/python'
@ -139,7 +136,7 @@ class ApiClient(object):
body = self.sanitize_for_serialization(body) body = self.sanitize_for_serialization(body)
# request url # request url
url = self.host + resource_path url = self.configuration.host + resource_path
# perform request and return response # perform request and return response
response_data = self.request(method, url, response_data = self.request(method, url,
@ -496,13 +493,11 @@ class ApiClient(object):
:param querys: Query parameters tuple list to be updated. :param querys: Query parameters tuple list to be updated.
:param auth_settings: Authentication setting identifiers list. :param auth_settings: Authentication setting identifiers list.
""" """
config = Configuration()
if not auth_settings: if not auth_settings:
return return
for auth in auth_settings: for auth in auth_settings:
auth_setting = config.auth_settings().get(auth) auth_setting = self.configuration.auth_settings().get(auth)
if auth_setting: if auth_setting:
if not auth_setting['value']: if not auth_setting['value']:
continue continue
@ -523,9 +518,7 @@ class ApiClient(object):
:param response: RESTResponse. :param response: RESTResponse.
:return: file path. :return: file path.
""" """
config = Configuration() fd, path = tempfile.mkstemp(dir=self.configuration.temp_folder_path)
fd, path = tempfile.mkstemp(dir=config.temp_folder_path)
os.close(fd) os.close(fd)
os.remove(path) os.remove(path)

View File

@ -20,7 +20,6 @@ import re
# python 2 and python 3 compatibility library # python 2 and python 3 compatibility library
from six import iteritems from six import iteritems
from ..configuration import Configuration
from ..api_client import ApiClient from ..api_client import ApiClient
@ -32,13 +31,9 @@ class FakeApi(object):
""" """
def __init__(self, api_client=None): def __init__(self, api_client=None):
config = Configuration() if api_client is None:
if api_client: api_client = ApiClient()
self.api_client = api_client self.api_client = api_client
else:
if not config.api_client:
config.api_client = ApiClient()
self.api_client = config.api_client
def test_code_inject____end__rn_n_r(self, **kwargs): def test_code_inject____end__rn_n_r(self, **kwargs):
""" """

View File

@ -22,17 +22,6 @@ from six import iteritems
from six.moves import http_client as httplib 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): class Configuration(object):
""" """
NOTE: This class is auto generated by the swagger code generator program. NOTE: This class is auto generated by the swagger code generator program.
@ -46,8 +35,7 @@ class Configuration(object):
""" """
# Default Base url # Default Base url
self.host = "https://petstore.swagger.io */ ' \" =end -- \\r\\n \\n \\r/v2 */ ' \" =end -- \\r\\n \\n \\r" 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 # Temp file folder for downloading files
self.temp_folder_path = None self.temp_folder_path = None

View File

@ -24,8 +24,6 @@ import re
from six import PY3 from six import PY3
from six.moves.urllib.parse import urlencode from six.moves.urllib.parse import urlencode
from .configuration import Configuration
try: try:
import urllib3 import urllib3
except ImportError: except ImportError:
@ -58,7 +56,7 @@ class RESTResponse(io.IOBase):
class RESTClientObject(object): 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 # 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/poolmanager.py#L75
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680
@ -67,37 +65,28 @@ class RESTClientObject(object):
# http://stackoverflow.com/a/23957365/2985775 # http://stackoverflow.com/a/23957365/2985775
# cert_reqs # cert_reqs
if Configuration().verify_ssl: if configuration.verify_ssl:
cert_reqs = ssl.CERT_REQUIRED cert_reqs = ssl.CERT_REQUIRED
else: else:
cert_reqs = ssl.CERT_NONE cert_reqs = ssl.CERT_NONE
# ca_certs # ca_certs
if Configuration().ssl_ca_cert: if configuration.ssl_ca_cert:
ca_certs = Configuration().ssl_ca_cert ca_certs = configuration.ssl_ca_cert
else: else:
# if not set certificate file, use Mozilla's root certificates. # if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where() 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 # https pool manager
if proxy: if configuration.proxy:
self.pool_manager = urllib3.ProxyManager( self.pool_manager = urllib3.ProxyManager(
num_pools=pools_size, num_pools=pools_size,
maxsize=maxsize, maxsize=maxsize,
cert_reqs=cert_reqs, cert_reqs=cert_reqs,
ca_certs=ca_certs, ca_certs=ca_certs,
cert_file=cert_file, cert_file=configuration.cert_file,
key_file=key_file, key_file=configuration.key_file,
proxy_url=proxy proxy_url=configuration.proxy
) )
else: else:
self.pool_manager = urllib3.PoolManager( self.pool_manager = urllib3.PoolManager(
@ -105,19 +94,10 @@ class RESTClientObject(object):
maxsize=maxsize, maxsize=maxsize,
cert_reqs=cert_reqs, cert_reqs=cert_reqs,
ca_certs=ca_certs, ca_certs=ca_certs,
cert_file=cert_file, cert_file=configuration.cert_file,
key_file=key_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, def request(self, method, url, query_params=None, headers=None,
body=None, post_params=None, _preload_content=True, _request_timeout=None): body=None, post_params=None, _preload_content=True, _request_timeout=None):
""" """

View File

@ -104,7 +104,6 @@ Class | Method | HTTP request | Description
- [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md) - [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md)
- [ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md) - [ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md)
- [ArrayTest](docs/ArrayTest.md) - [ArrayTest](docs/ArrayTest.md)
- [Capitalization](docs/Capitalization.md)
- [Cat](docs/Cat.md) - [Cat](docs/Cat.md)
- [Category](docs/Category.md) - [Category](docs/Category.md)
- [ClassModel](docs/ClassModel.md) - [ClassModel](docs/ClassModel.md)

View File

@ -73,11 +73,12 @@ from petstore_api.rest import ApiException
from pprint import pprint from pprint import pprint
# Configure HTTP basic authorization: http_basic_test # Configure HTTP basic authorization: http_basic_test
petstore_api.configuration.username = 'YOUR_USERNAME' configuration = petstore_api.Configuration()
petstore_api.configuration.password = 'YOUR_PASSWORD' configuration.username = 'YOUR_USERNAME'
configuration.password = 'YOUR_PASSWORD'
# create an instance of the API class # 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 number = 3.4 # float | None
double = 1.2 # float | None double = 1.2 # float | None
pattern_without_delimiter = 'pattern_without_delimiter_example' # str | None pattern_without_delimiter = 'pattern_without_delimiter_example' # str | None

View File

@ -30,10 +30,11 @@ from petstore_api.rest import ApiException
from pprint import pprint from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth # 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 # 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 body = petstore_api.Pet() # Pet | Pet object that needs to be added to the store
try: try:
@ -80,10 +81,11 @@ from petstore_api.rest import ApiException
from pprint import pprint from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth # 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 # 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 pet_id = 789 # int | Pet id to delete
api_key = 'api_key_example' # str | (optional) api_key = 'api_key_example' # str | (optional)
@ -132,10 +134,11 @@ from petstore_api.rest import ApiException
from pprint import pprint from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth # 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 # 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 status = ['status_example'] # list[str] | Status values that need to be considered for filter
try: try:
@ -183,10 +186,11 @@ from petstore_api.rest import ApiException
from pprint import pprint from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth # 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 # 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 tags = ['tags_example'] # list[str] | Tags to filter by
try: try:
@ -234,12 +238,13 @@ from petstore_api.rest import ApiException
from pprint import pprint from pprint import pprint
# Configure API key authorization: api_key # 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 # 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 # 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 pet_id = 789 # int | ID of pet to return
try: try:
@ -287,10 +292,11 @@ from petstore_api.rest import ApiException
from pprint import pprint from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth # 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 # 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 body = petstore_api.Pet() # Pet | Pet object that needs to be added to the store
try: try:
@ -337,10 +343,11 @@ from petstore_api.rest import ApiException
from pprint import pprint from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth # 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 # 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 pet_id = 789 # int | ID of pet that needs to be updated
name = 'name_example' # str | Updated name of the pet (optional) name = 'name_example' # str | Updated name of the pet (optional)
status = 'status_example' # str | Updated status 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 from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth # 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 # 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 pet_id = 789 # int | ID of pet to update
additional_metadata = 'additional_metadata_example' # str | Additional data to pass to server (optional) additional_metadata = 'additional_metadata_example' # str | Additional data to pass to server (optional)
file = '/path/to/file.txt' # file | file to upload (optional) file = '/path/to/file.txt' # file | file to upload (optional)

View File

@ -73,12 +73,13 @@ from petstore_api.rest import ApiException
from pprint import pprint from pprint import pprint
# Configure API key authorization: api_key # 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 # 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 # create an instance of the API class
api_instance = petstore_api.StoreApi() api_instance = petstore_api.StoreApi(petstore_api.ApiClient(configuration))
try: try:
# Returns pet inventories by status # Returns pet inventories by status

View File

@ -21,7 +21,6 @@ from .models.api_response import ApiResponse
from .models.array_of_array_of_number_only import ArrayOfArrayOfNumberOnly from .models.array_of_array_of_number_only import ArrayOfArrayOfNumberOnly
from .models.array_of_number_only import ArrayOfNumberOnly from .models.array_of_number_only import ArrayOfNumberOnly
from .models.array_test import ArrayTest from .models.array_test import ArrayTest
from .models.capitalization import Capitalization
from .models.cat import Cat from .models.cat import Cat
from .models.category import Category from .models.category import Category
from .models.class_model import ClassModel from .models.class_model import ClassModel
@ -49,7 +48,7 @@ from .models.user import User
# import apis into sdk package # import apis into sdk package
from .apis.fake_api import FakeApi 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.pet_api import PetApi
from .apis.store_api import StoreApi from .apis.store_api import StoreApi
from .apis.user_api import UserApi from .apis.user_api import UserApi
@ -58,5 +57,3 @@ from .apis.user_api import UserApi
from .api_client import ApiClient from .api_client import ApiClient
from .configuration import Configuration from .configuration import Configuration
configuration = Configuration()

View File

@ -59,18 +59,15 @@ class ApiClient(object):
'object': object, 'object': object,
} }
def __init__(self, host=None, header_name=None, header_value=None, cookie=None): def __init__(self, configuration=None, header_name=None, header_value=None, cookie=None):
""" if configuration is None:
Constructor of the class. configuration = Configuration()
""" self.configuration = configuration
self.rest_client = RESTClientObject()
self.rest_client = RESTClientObject(configuration)
self.default_headers = {} self.default_headers = {}
if header_name is not None: if header_name is not None:
self.default_headers[header_name] = header_value self.default_headers[header_name] = header_value
if host is None:
self.host = Configuration().host
else:
self.host = host
self.cookie = cookie self.cookie = cookie
# Set default User-Agent. # Set default User-Agent.
self.user_agent = 'Swagger-Codegen/1.0.0/python' self.user_agent = 'Swagger-Codegen/1.0.0/python'
@ -139,7 +136,7 @@ class ApiClient(object):
body = self.sanitize_for_serialization(body) body = self.sanitize_for_serialization(body)
# request url # request url
url = self.host + resource_path url = self.configuration.host + resource_path
# perform request and return response # perform request and return response
response_data = self.request(method, url, response_data = self.request(method, url,
@ -496,13 +493,11 @@ class ApiClient(object):
:param querys: Query parameters tuple list to be updated. :param querys: Query parameters tuple list to be updated.
:param auth_settings: Authentication setting identifiers list. :param auth_settings: Authentication setting identifiers list.
""" """
config = Configuration()
if not auth_settings: if not auth_settings:
return return
for auth in auth_settings: for auth in auth_settings:
auth_setting = config.auth_settings().get(auth) auth_setting = self.configuration.auth_settings().get(auth)
if auth_setting: if auth_setting:
if not auth_setting['value']: if not auth_setting['value']:
continue continue
@ -523,9 +518,7 @@ class ApiClient(object):
:param response: RESTResponse. :param response: RESTResponse.
:return: file path. :return: file path.
""" """
config = Configuration() fd, path = tempfile.mkstemp(dir=self.configuration.temp_folder_path)
fd, path = tempfile.mkstemp(dir=config.temp_folder_path)
os.close(fd) os.close(fd)
os.remove(path) os.remove(path)

View File

@ -2,7 +2,7 @@ from __future__ import absolute_import
# import apis into api package # import apis into api package
from .fake_api import FakeApi 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 .pet_api import PetApi
from .store_api import StoreApi from .store_api import StoreApi
from .user_api import UserApi from .user_api import UserApi

View File

@ -20,7 +20,6 @@ import re
# python 2 and python 3 compatibility library # python 2 and python 3 compatibility library
from six import iteritems from six import iteritems
from ..configuration import Configuration
from ..api_client import ApiClient from ..api_client import ApiClient
@ -32,13 +31,9 @@ class FakeApi(object):
""" """
def __init__(self, api_client=None): def __init__(self, api_client=None):
config = Configuration() if api_client is None:
if api_client: api_client = ApiClient()
self.api_client = api_client self.api_client = api_client
else:
if not config.api_client:
config.api_client = ApiClient()
self.api_client = config.api_client
def test_client_model(self, body, **kwargs): def test_client_model(self, body, **kwargs):
""" """

View File

@ -20,7 +20,6 @@ import re
# python 2 and python 3 compatibility library # python 2 and python 3 compatibility library
from six import iteritems from six import iteritems
from ..configuration import Configuration
from ..api_client import ApiClient from ..api_client import ApiClient
@ -32,13 +31,9 @@ class FakeClassnameTags123Api(object):
""" """
def __init__(self, api_client=None): def __init__(self, api_client=None):
config = Configuration() if api_client is None:
if api_client: api_client = ApiClient()
self.api_client = api_client self.api_client = api_client
else:
if not config.api_client:
config.api_client = ApiClient()
self.api_client = config.api_client
def test_classname(self, body, **kwargs): def test_classname(self, body, **kwargs):
""" """
@ -106,6 +101,7 @@ class FakeClassnameTags123Api(object):
collection_formats = {} collection_formats = {}
resource_path = '/fake_classname_test'.replace('{format}', 'json')
path_params = {} path_params = {}
query_params = {} query_params = {}
@ -129,7 +125,7 @@ class FakeClassnameTags123Api(object):
# Authentication setting # Authentication setting
auth_settings = [] auth_settings = []
return self.api_client.call_api('/fake_classname_test', 'PATCH', return self.api_client.call_api(resource_path, 'PATCH',
path_params, path_params,
query_params, query_params,
header_params, header_params,

View File

@ -20,7 +20,6 @@ import re
# python 2 and python 3 compatibility library # python 2 and python 3 compatibility library
from six import iteritems from six import iteritems
from ..configuration import Configuration
from ..api_client import ApiClient from ..api_client import ApiClient
@ -32,13 +31,9 @@ class PetApi(object):
""" """
def __init__(self, api_client=None): def __init__(self, api_client=None):
config = Configuration() if api_client is None:
if api_client: api_client = ApiClient()
self.api_client = api_client self.api_client = api_client
else:
if not config.api_client:
config.api_client = ApiClient()
self.api_client = config.api_client
def add_pet(self, body, **kwargs): def add_pet(self, body, **kwargs):
""" """

View File

@ -20,7 +20,6 @@ import re
# python 2 and python 3 compatibility library # python 2 and python 3 compatibility library
from six import iteritems from six import iteritems
from ..configuration import Configuration
from ..api_client import ApiClient from ..api_client import ApiClient
@ -32,13 +31,9 @@ class StoreApi(object):
""" """
def __init__(self, api_client=None): def __init__(self, api_client=None):
config = Configuration() if api_client is None:
if api_client: api_client = ApiClient()
self.api_client = api_client self.api_client = api_client
else:
if not config.api_client:
config.api_client = ApiClient()
self.api_client = config.api_client
def delete_order(self, order_id, **kwargs): 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): if ('order_id' not in params) or (params['order_id'] is None):
raise ValueError("Missing the required parameter `order_id` when calling `delete_order`") 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 = {} collection_formats = {}

View File

@ -20,7 +20,6 @@ import re
# python 2 and python 3 compatibility library # python 2 and python 3 compatibility library
from six import iteritems from six import iteritems
from ..configuration import Configuration
from ..api_client import ApiClient from ..api_client import ApiClient
@ -32,13 +31,9 @@ class UserApi(object):
""" """
def __init__(self, api_client=None): def __init__(self, api_client=None):
config = Configuration() if api_client is None:
if api_client: api_client = ApiClient()
self.api_client = api_client self.api_client = api_client
else:
if not config.api_client:
config.api_client = ApiClient()
self.api_client = config.api_client
def create_user(self, body, **kwargs): def create_user(self, body, **kwargs):
""" """

View File

@ -22,17 +22,6 @@ from six import iteritems
from six.moves import http_client as httplib 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): class Configuration(object):
""" """
NOTE: This class is auto generated by the swagger code generator program. NOTE: This class is auto generated by the swagger code generator program.
@ -46,8 +35,7 @@ class Configuration(object):
""" """
# Default Base url # Default Base url
self.host = "http://petstore.swagger.io/v2" self.host = "http://petstore.swagger.io/v2"
# Default api client
self.api_client = None
# Temp file folder for downloading files # Temp file folder for downloading files
self.temp_folder_path = None self.temp_folder_path = None

View File

@ -21,7 +21,6 @@ from .api_response import ApiResponse
from .array_of_array_of_number_only import ArrayOfArrayOfNumberOnly from .array_of_array_of_number_only import ArrayOfArrayOfNumberOnly
from .array_of_number_only import ArrayOfNumberOnly from .array_of_number_only import ArrayOfNumberOnly
from .array_test import ArrayTest from .array_test import ArrayTest
from .capitalization import Capitalization
from .cat import Cat from .cat import Cat
from .category import Category from .category import Category
from .class_model import ClassModel from .class_model import ClassModel

View File

@ -24,8 +24,6 @@ import re
from six import PY3 from six import PY3
from six.moves.urllib.parse import urlencode from six.moves.urllib.parse import urlencode
from .configuration import Configuration
try: try:
import urllib3 import urllib3
except ImportError: except ImportError:
@ -58,7 +56,7 @@ class RESTResponse(io.IOBase):
class RESTClientObject(object): 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 # 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/poolmanager.py#L75
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680
@ -67,37 +65,28 @@ class RESTClientObject(object):
# http://stackoverflow.com/a/23957365/2985775 # http://stackoverflow.com/a/23957365/2985775
# cert_reqs # cert_reqs
if Configuration().verify_ssl: if configuration.verify_ssl:
cert_reqs = ssl.CERT_REQUIRED cert_reqs = ssl.CERT_REQUIRED
else: else:
cert_reqs = ssl.CERT_NONE cert_reqs = ssl.CERT_NONE
# ca_certs # ca_certs
if Configuration().ssl_ca_cert: if configuration.ssl_ca_cert:
ca_certs = Configuration().ssl_ca_cert ca_certs = configuration.ssl_ca_cert
else: else:
# if not set certificate file, use Mozilla's root certificates. # if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where() 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 # https pool manager
if proxy: if configuration.proxy:
self.pool_manager = urllib3.ProxyManager( self.pool_manager = urllib3.ProxyManager(
num_pools=pools_size, num_pools=pools_size,
maxsize=maxsize, maxsize=maxsize,
cert_reqs=cert_reqs, cert_reqs=cert_reqs,
ca_certs=ca_certs, ca_certs=ca_certs,
cert_file=cert_file, cert_file=configuration.cert_file,
key_file=key_file, key_file=configuration.key_file,
proxy_url=proxy proxy_url=configuration.proxy
) )
else: else:
self.pool_manager = urllib3.PoolManager( self.pool_manager = urllib3.PoolManager(
@ -105,11 +94,10 @@ class RESTClientObject(object):
maxsize=maxsize, maxsize=maxsize,
cert_reqs=cert_reqs, cert_reqs=cert_reqs,
ca_certs=ca_certs, ca_certs=ca_certs,
cert_file=cert_file, cert_file=configuration.cert_file,
key_file=key_file key_file=configuration.key_file
) )
def request(self, method, url, query_params=None, headers=None, def request(self, method, url, query_params=None, headers=None,
body=None, post_params=None, _preload_content=True, _request_timeout=None): body=None, post_params=None, _preload_content=True, _request_timeout=None):
""" """

View File

@ -8,20 +8,9 @@
OpenAPI spec version: 1.0.0 OpenAPI spec version: 1.0.0
Contact: apiteam@swagger.io Contact: apiteam@swagger.io
Generated by: https://github.com/swagger-api/swagger-codegen.git 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 from __future__ import absolute_import
import os import os
@ -30,14 +19,14 @@ import unittest
import petstore_api import petstore_api
from petstore_api.rest import ApiException 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): class TestFakeClassnameTags123Api(unittest.TestCase):
""" FakeClassnameTags123Api unit test stubs """ """ FakeClassnameTags123Api unit test stubs """
def setUp(self): 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): def tearDown(self):
pass pass

View File

@ -21,23 +21,28 @@ HOST = 'http://petstore.swagger.io/v2'
class ApiClientTests(unittest.TestCase): class ApiClientTests(unittest.TestCase):
def setUp(self): def setUp(self):
self.api_client = petstore_api.ApiClient(HOST) self.api_client = petstore_api.ApiClient()
def test_configuration(self): def test_configuration(self):
petstore_api.configuration.api_key['api_key'] = '123456' config = petstore_api.Configuration()
petstore_api.configuration.api_key_prefix['api_key'] = 'PREFIX' config.host = 'http://localhost/'
petstore_api.configuration.username = 'test_username'
petstore_api.configuration.password = 'test_password' 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'} header_params = {'test1': 'value1'}
query_params = {'test2': 'value2'} query_params = {'test2': 'value2'}
auth_settings = ['api_key', 'unknown'] auth_settings = ['api_key', 'unknown']
client = petstore_api.ApiClient(config)
# test prefix # 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 # 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 # test api key auth
self.assertEqual(header_params['test1'], 'value1') self.assertEqual(header_params['test1'], 'value1')
@ -45,8 +50,8 @@ class ApiClientTests(unittest.TestCase):
self.assertEqual(query_params['test2'], 'value2') self.assertEqual(query_params['test2'], 'value2')
# test basic auth # test basic auth
self.assertEqual('test_username', petstore_api.configuration.username) self.assertEqual('test_username', client.configuration.username)
self.assertEqual('test_password', petstore_api.configuration.password) self.assertEqual('test_password', client.configuration.password)
def test_select_header_accept(self): def test_select_header_accept(self):
accepts = ['APPLICATION/JSON', 'APPLICATION/XML'] accepts = ['APPLICATION/JSON', 'APPLICATION/XML']

View File

@ -11,6 +11,7 @@ import os
import unittest import unittest
import petstore_api import petstore_api
from petstore_api import Configuration
from petstore_api.rest import ApiException from petstore_api.rest import ApiException
from .util import id_gen from .util import id_gen
@ -50,7 +51,9 @@ class MockPoolManager(object):
class PetApiTests(unittest.TestCase): class PetApiTests(unittest.TestCase):
def setUp(self): 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.pet_api = petstore_api.PetApi(self.api_client)
self.setUpModels() self.setUpModels()
self.setUpFiles() 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=5)
self.pet_api.add_pet(body=self.pet, _request_timeout=(1, 2)) 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_api = petstore_api.PetApi()
pet_api2 = petstore_api.PetApi() pet_api2 = petstore_api.PetApi()
api_client3 = petstore_api.ApiClient() self.assertNotEqual(pet_api.api_client, pet_api2.api_client)
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)
# same default api client pet_api.api_client.user_agent = 'api client 3'
self.assertEqual(pet_api.api_client, pet_api2.api_client) pet_api2.api_client.user_agent = 'api client 4'
# confirm using the default api client in the config module
self.assertEqual(pet_api.api_client, petstore_api.configuration.api_client) self.assertNotEqual(pet_api.api_client.user_agent, pet_api2.api_client.user_agent)
# 2 different api clients are not the same
self.assertNotEqual(api_client3, api_client4) def test_separate_default_config_instances(self):
# customized pet api not using the default api client pet_api = petstore_api.PetApi()
self.assertNotEqual(pet_api3.api_client, petstore_api.configuration.api_client) pet_api2 = petstore_api.PetApi()
# customized pet api not using the old pet api's api client self.assertNotEqual(pet_api.api_client.configuration, pet_api2.api_client.configuration)
self.assertNotEqual(pet_api3.api_client, pet_api2.api_client)
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): def test_async_request(self):
self.pet_api.add_pet(body=self.pet) self.pet_api.add_pet(body=self.pet)