forked from loafle/openapi-generator-original
rollback python3 template
This commit is contained in:
@@ -5,5 +5,5 @@ import os
|
||||
__all__ = []
|
||||
|
||||
for module in os.listdir(os.path.dirname(__file__)):
|
||||
if module != '__init__.py' and module[-3:] == '.py':
|
||||
__all__.append(module[:-3])
|
||||
if module != '__init__.py' and module[-3:] == '.py':
|
||||
__all__.append(module[:-3])
|
||||
|
||||
@@ -3,17 +3,17 @@
|
||||
{{classname}}.py
|
||||
Copyright 2015 SmartBear Software
|
||||
|
||||
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
|
||||
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
|
||||
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.
|
||||
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.
|
||||
|
||||
NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
|
||||
"""
|
||||
@@ -24,20 +24,20 @@ from .models import *
|
||||
|
||||
|
||||
{{#operations}}
|
||||
class {{classname}}(object):
|
||||
class {{classname}}(object):
|
||||
|
||||
def __init__(self, apiClient):
|
||||
self.apiClient = apiClient
|
||||
self.apiClient = apiClient
|
||||
|
||||
{{newline}}
|
||||
{{#operation}}
|
||||
def {{nickname}}(self, {{#requiredParams}}{{paramName}}{{#defaultValue}} = None{{/defaultValue}}, {{/requiredParams}}**kwargs):
|
||||
def {{nickname}}(self, {{#requiredParams}}{{paramName}}{{#defaultValue}} = None{{/defaultValue}}, {{/requiredParams}}**kwargs):
|
||||
"""{{{summary}}}
|
||||
{{{notes}}}
|
||||
|
||||
Args:
|
||||
{{#allParams}}{{paramName}}, {{dataType}}: {{{description}}} {{^optional}}(required){{/optional}}{{#optional}}(optional){{/optional}}
|
||||
{{/allParams}}
|
||||
{{#allParams}}{{paramName}}, {{dataType}}: {{{description}}} {{^optional}}(required){{/optional}}{{#optional}}(optional){{/optional}}
|
||||
{{/allParams}}
|
||||
|
||||
Returns: {{returnType}}
|
||||
"""
|
||||
@@ -46,9 +46,9 @@ from .models import *
|
||||
|
||||
params = locals()
|
||||
for (key, val) in params['kwargs'].items():
|
||||
if key not in allParams:
|
||||
raise TypeError("Got an unexpected keyword argument '%s' to method {{nickname}}" % key)
|
||||
params[key] = val
|
||||
if key not in allParams:
|
||||
raise TypeError("Got an unexpected keyword argument '%s' to method {{nickname}}" % key)
|
||||
params[key] = val
|
||||
del params['kwargs']
|
||||
|
||||
resourcePath = '{{path}}'
|
||||
@@ -59,37 +59,37 @@ from .models import *
|
||||
headerParams = {}
|
||||
|
||||
{{#queryParams}}
|
||||
if ('{{paramName}}' in params):
|
||||
if ('{{paramName}}' in params):
|
||||
queryParams['{{paramName}}'] = self.apiClient.toPathValue(params['{{paramName}}'])
|
||||
{{/queryParams}}
|
||||
|
||||
{{#headerParams}}
|
||||
if ('{{paramName}}' in params):
|
||||
if ('{{paramName}}' in params):
|
||||
headerParams['{{paramName}}'] = params['{{paramName}}']
|
||||
{{/headerParams}}
|
||||
|
||||
{{#pathParams}}
|
||||
if ('{{paramName}}' in params):
|
||||
if ('{{paramName}}' in params):
|
||||
replacement = str(self.apiClient.toPathValue(params['{{paramName}}']))
|
||||
resourcePath = resourcePath.replace('{' + '{{baseName}}' + '}',
|
||||
replacement)
|
||||
replacement)
|
||||
{{/pathParams}}
|
||||
|
||||
postData = (params['body'] if 'body' in params else None)
|
||||
|
||||
response = self.apiClient.callAPI(resourcePath, method, queryParams,
|
||||
postData, headerParams)
|
||||
postData, headerParams)
|
||||
|
||||
{{#returnType}}
|
||||
if not response:
|
||||
if not response:
|
||||
return None
|
||||
|
||||
responseObject = self.apiClient.deserialize(response, '{{returnType}}')
|
||||
return responseObject
|
||||
responseObject = self.apiClient.deserialize(response, '{{returnType}}')
|
||||
return responseObject
|
||||
{{/returnType}}
|
||||
{{newline}}
|
||||
{{newline}}
|
||||
{{/operation}}
|
||||
{{newline}}
|
||||
{{newline}}
|
||||
{{/operations}}
|
||||
{{newline}}
|
||||
|
||||
@@ -2,39 +2,39 @@
|
||||
"""
|
||||
Copyright 2015 SmartBear Software
|
||||
|
||||
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
|
||||
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
|
||||
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.
|
||||
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.
|
||||
"""
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
{{#model}}
|
||||
|
||||
class {{classname}}:
|
||||
"""NOTE: This class is auto generated by the swagger code generator program.
|
||||
Do not edit the class manually."""
|
||||
class {{classname}}:
|
||||
"""NOTE: This class is auto generated by the swagger code generator program.
|
||||
Do not edit the class manually."""
|
||||
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self):
|
||||
self.swaggerTypes = {
|
||||
{{#vars}}
|
||||
{{#vars}}
|
||||
'{{name}}': '{{{datatype}}}'{{#hasMore}},
|
||||
{{/hasMore}}
|
||||
{{/vars}}{{newline}}
|
||||
{{/vars}}{{newline}}
|
||||
}
|
||||
|
||||
|
||||
{{#vars}}
|
||||
{{#description}}#{{description}}
|
||||
{{/description}}
|
||||
self.{{name}} = None # {{{datatype}}}
|
||||
{{#description}}#{{description}}
|
||||
{{/description}}
|
||||
self.{{name}} = None # {{{datatype}}}
|
||||
{{/vars}}
|
||||
{{/model}}
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
|
||||
@@ -16,215 +16,215 @@ from .models import *
|
||||
|
||||
|
||||
class ApiClient:
|
||||
"""Generic API client for Swagger client library builds"""
|
||||
"""Generic API client for Swagger client library builds"""
|
||||
|
||||
def __init__(self, apiKey=None, apiServer=None):
|
||||
if apiKey == None:
|
||||
raise Exception('You must pass an apiKey when instantiating the '
|
||||
'APIClient')
|
||||
self.apiKey = apiKey
|
||||
self.apiServer = apiServer
|
||||
self.cookie = None
|
||||
def __init__(self, apiKey=None, apiServer=None):
|
||||
if apiKey == None:
|
||||
raise Exception('You must pass an apiKey when instantiating the '
|
||||
'APIClient')
|
||||
self.apiKey = apiKey
|
||||
self.apiServer = apiServer
|
||||
self.cookie = None
|
||||
|
||||
def callAPI(self, resourcePath, method, queryParams, postData,
|
||||
headerParams=None):
|
||||
def callAPI(self, resourcePath, method, queryParams, postData,
|
||||
headerParams=None):
|
||||
|
||||
url = self.apiServer + resourcePath
|
||||
headers = {}
|
||||
if headerParams:
|
||||
for param, value in headerParams.items():
|
||||
headers[param] = value
|
||||
url = self.apiServer + resourcePath
|
||||
headers = {}
|
||||
if headerParams:
|
||||
for param, value in headerParams.items():
|
||||
headers[param] = value
|
||||
|
||||
#headers['Content-type'] = 'application/json'
|
||||
headers['api_key'] = self.apiKey
|
||||
#headers['Content-type'] = 'application/json'
|
||||
headers['api_key'] = self.apiKey
|
||||
|
||||
if self.cookie:
|
||||
headers['Cookie'] = self.cookie
|
||||
if self.cookie:
|
||||
headers['Cookie'] = self.cookie
|
||||
|
||||
data = None
|
||||
data = None
|
||||
|
||||
if queryParams:
|
||||
# Need to remove None values, these should not be sent
|
||||
sentQueryParams = {}
|
||||
for param, value in queryParams.items():
|
||||
if value != None:
|
||||
sentQueryParams[param] = value
|
||||
url = url + '?' + urllib.parse.urlencode(sentQueryParams)
|
||||
if queryParams:
|
||||
# Need to remove None values, these should not be sent
|
||||
sentQueryParams = {}
|
||||
for param, value in queryParams.items():
|
||||
if value != None:
|
||||
sentQueryParams[param] = value
|
||||
url = url + '?' + urllib.parse.urlencode(sentQueryParams)
|
||||
|
||||
if method in ['GET']:
|
||||
if method in ['GET']:
|
||||
|
||||
#Options to add statements later on and for compatibility
|
||||
pass
|
||||
#Options to add statements later on and for compatibility
|
||||
pass
|
||||
|
||||
elif method in ['PATCH', 'POST', 'PUT', 'DELETE']:
|
||||
elif method in ['PATCH', 'POST', 'PUT', 'DELETE']:
|
||||
|
||||
if postData:
|
||||
headers['Content-type'] = 'application/json'
|
||||
data = self.sanitizeForSerialization(postData)
|
||||
data = json.dumps(data)
|
||||
if postData:
|
||||
headers['Content-type'] = 'application/json'
|
||||
data = self.sanitizeForSerialization(postData)
|
||||
data = json.dumps(data)
|
||||
|
||||
else:
|
||||
raise Exception('Method ' + method + ' is not recognized.')
|
||||
else:
|
||||
raise Exception('Method ' + method + ' is not recognized.')
|
||||
|
||||
if data:
|
||||
data = data.encode('utf-8')
|
||||
if data:
|
||||
data = data.encode('utf-8')
|
||||
|
||||
requestParams = MethodRequest(method=method, url=url,
|
||||
headers=headers, data=data)
|
||||
requestParams = MethodRequest(method=method, url=url,
|
||||
headers=headers, data=data)
|
||||
|
||||
# Make the request
|
||||
request = urllib.request.urlopen(requestParams)
|
||||
encoding = request.headers.get_content_charset()
|
||||
if not encoding:
|
||||
encoding = 'iso-8859-1'
|
||||
response = request.read().decode(encoding)
|
||||
# Make the request
|
||||
request = urllib.request.urlopen(requestParams)
|
||||
encoding = request.headers.get_content_charset()
|
||||
if not encoding:
|
||||
encoding = 'iso-8859-1'
|
||||
response = request.read().decode(encoding)
|
||||
|
||||
try:
|
||||
data = json.loads(response)
|
||||
except ValueError: # PUT requests don't return anything
|
||||
data = None
|
||||
try:
|
||||
data = json.loads(response)
|
||||
except ValueError: # PUT requests don't return anything
|
||||
data = None
|
||||
|
||||
return data
|
||||
return data
|
||||
|
||||
def toPathValue(self, obj):
|
||||
"""Convert a string or object to a path-friendly value
|
||||
Args:
|
||||
obj -- object or string value
|
||||
Returns:
|
||||
string -- quoted value
|
||||
"""
|
||||
if type(obj) == list:
|
||||
return urllib.parse.quote(','.join(obj))
|
||||
else:
|
||||
return urllib.parse.quote(str(obj))
|
||||
def toPathValue(self, obj):
|
||||
"""Convert a string or object to a path-friendly value
|
||||
Args:
|
||||
obj -- object or string value
|
||||
Returns:
|
||||
string -- quoted value
|
||||
"""
|
||||
if type(obj) == list:
|
||||
return urllib.parse.quote(','.join(obj))
|
||||
else:
|
||||
return urllib.parse.quote(str(obj))
|
||||
|
||||
def sanitizeForSerialization(self, obj):
|
||||
"""Dump an object into JSON for POSTing."""
|
||||
def sanitizeForSerialization(self, obj):
|
||||
"""Dump an object into JSON for POSTing."""
|
||||
|
||||
if type(obj) == type(None):
|
||||
return None
|
||||
elif type(obj) in [str, int, float, bool]:
|
||||
return obj
|
||||
elif type(obj) == list:
|
||||
return [self.sanitizeForSerialization(subObj) for subObj in obj]
|
||||
elif type(obj) == datetime.datetime:
|
||||
return obj.isoformat()
|
||||
else:
|
||||
if type(obj) == dict:
|
||||
objDict = obj
|
||||
else:
|
||||
objDict = obj.__dict__
|
||||
return {key: self.sanitizeForSerialization(val)
|
||||
for (key, val) in objDict.items()
|
||||
if key != 'swaggerTypes'}
|
||||
if type(obj) == type(None):
|
||||
return None
|
||||
elif type(obj) in [str, int, float, bool]:
|
||||
return obj
|
||||
elif type(obj) == list:
|
||||
return [self.sanitizeForSerialization(subObj) for subObj in obj]
|
||||
elif type(obj) == datetime.datetime:
|
||||
return obj.isoformat()
|
||||
else:
|
||||
if type(obj) == dict:
|
||||
objDict = obj
|
||||
else:
|
||||
objDict = obj.__dict__
|
||||
return {key: self.sanitizeForSerialization(val)
|
||||
for (key, val) in objDict.items()
|
||||
if key != 'swaggerTypes'}
|
||||
|
||||
def _iso8601Format(self, timesep, microsecond, offset, zulu):
|
||||
"""Format for parsing a datetime string with given properties.
|
||||
def _iso8601Format(self, timesep, microsecond, offset, zulu):
|
||||
"""Format for parsing a datetime string with given properties.
|
||||
|
||||
Args:
|
||||
timesep -- string separating time from date ('T' or 't')
|
||||
microsecond -- microsecond portion of time ('.XXX')
|
||||
offset -- time offset (+/-XX:XX) or None
|
||||
zulu -- 'Z' or 'z' for UTC, or None for time offset (+/-XX:XX)
|
||||
Args:
|
||||
timesep -- string separating time from date ('T' or 't')
|
||||
microsecond -- microsecond portion of time ('.XXX')
|
||||
offset -- time offset (+/-XX:XX) or None
|
||||
zulu -- 'Z' or 'z' for UTC, or None for time offset (+/-XX:XX)
|
||||
|
||||
Returns:
|
||||
str - format string for datetime.strptime"""
|
||||
Returns:
|
||||
str - format string for datetime.strptime"""
|
||||
|
||||
return '%Y-%m-%d{}%H:%M:%S{}{}'.format(
|
||||
timesep,
|
||||
'.%f' if microsecond else '',
|
||||
zulu or ('%z' if offset else ''))
|
||||
return '%Y-%m-%d{}%H:%M:%S{}{}'.format(
|
||||
timesep,
|
||||
'.%f' if microsecond else '',
|
||||
zulu or ('%z' if offset else ''))
|
||||
|
||||
# http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
|
||||
_iso8601Regex = re.compile(
|
||||
r'^\d\d\d\d-\d\d-\d\d([Tt])\d\d:\d\d:\d\d(\.\d+)?(([Zz])|(\+|-)\d\d:?\d\d)?$')
|
||||
# http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
|
||||
_iso8601Regex = re.compile(
|
||||
r'^\d\d\d\d-\d\d-\d\d([Tt])\d\d:\d\d:\d\d(\.\d+)?(([Zz])|(\+|-)\d\d:?\d\d)?$')
|
||||
|
||||
def _parseDatetime(self, d):
|
||||
if d is None:
|
||||
return None
|
||||
m = ApiClient._iso8601Regex.match(d)
|
||||
if not m:
|
||||
raise Exception('datetime regex match failed "%s"' % d)
|
||||
timesep, microsecond, offset, zulu, plusminus = m.groups()
|
||||
format = self._iso8601Format(timesep, microsecond, offset, zulu)
|
||||
if offset and not zulu:
|
||||
d = d.rsplit(sep=plusminus, maxsplit=1)[0] + offset.replace(':', '')
|
||||
return datetime.datetime.strptime(d, format)
|
||||
def _parseDatetime(self, d):
|
||||
if d is None:
|
||||
return None
|
||||
m = ApiClient._iso8601Regex.match(d)
|
||||
if not m:
|
||||
raise Exception('datetime regex match failed "%s"' % d)
|
||||
timesep, microsecond, offset, zulu, plusminus = m.groups()
|
||||
format = self._iso8601Format(timesep, microsecond, offset, zulu)
|
||||
if offset and not zulu:
|
||||
d = d.rsplit(sep=plusminus, maxsplit=1)[0] + offset.replace(':', '')
|
||||
return datetime.datetime.strptime(d, format)
|
||||
|
||||
def deserialize(self, obj, objClass):
|
||||
"""Derialize a JSON string into an object.
|
||||
def deserialize(self, obj, objClass):
|
||||
"""Derialize a JSON string into an object.
|
||||
|
||||
Args:
|
||||
obj -- string or object to be deserialized
|
||||
objClass -- class literal for deserialzied object, or string
|
||||
of class name
|
||||
Returns:
|
||||
object -- deserialized object"""
|
||||
Args:
|
||||
obj -- string or object to be deserialized
|
||||
objClass -- class literal for deserialzied object, or string
|
||||
of class name
|
||||
Returns:
|
||||
object -- deserialized object"""
|
||||
|
||||
# Have to accept objClass as string or actual type. Type could be a
|
||||
# native Python type, or one of the model classes.
|
||||
if type(objClass) == str:
|
||||
if 'list[' in objClass:
|
||||
match = re.match('list\[(.*)\]', objClass)
|
||||
subClass = match.group(1)
|
||||
return [self.deserialize(subObj, subClass) for subObj in obj]
|
||||
# Have to accept objClass as string or actual type. Type could be a
|
||||
# native Python type, or one of the model classes.
|
||||
if type(objClass) == str:
|
||||
if 'list[' in objClass:
|
||||
match = re.match('list\[(.*)\]', objClass)
|
||||
subClass = match.group(1)
|
||||
return [self.deserialize(subObj, subClass) for subObj in obj]
|
||||
|
||||
if (objClass in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime']):
|
||||
objClass = eval(objClass)
|
||||
else: # not a native type, must be model class
|
||||
objClass = eval(objClass + '.' + objClass)
|
||||
if (objClass in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime']):
|
||||
objClass = eval(objClass)
|
||||
else: # not a native type, must be model class
|
||||
objClass = eval(objClass + '.' + objClass)
|
||||
|
||||
if objClass in [int, float, dict, list, str, bool]:
|
||||
return objClass(obj)
|
||||
elif objClass == datetime:
|
||||
return self._parseDatetime(obj)
|
||||
if objClass in [int, float, dict, list, str, bool]:
|
||||
return objClass(obj)
|
||||
elif objClass == datetime:
|
||||
return self._parseDatetime(obj)
|
||||
|
||||
instance = objClass()
|
||||
instance = objClass()
|
||||
|
||||
for attr, attrType in instance.swaggerTypes.items():
|
||||
for attr, attrType in instance.swaggerTypes.items():
|
||||
|
||||
if attr in obj:
|
||||
value = obj[attr]
|
||||
if attrType in ['str', 'int', 'float', 'bool']:
|
||||
attrType = eval(attrType)
|
||||
try:
|
||||
value = attrType(value)
|
||||
except UnicodeEncodeError:
|
||||
value = unicode(value)
|
||||
except TypeError:
|
||||
value = value
|
||||
setattr(instance, attr, value)
|
||||
elif (attrType == 'datetime'):
|
||||
setattr(instance, attr, self._parseDatetime(value))
|
||||
elif 'list[' in attrType:
|
||||
match = re.match('list\[(.*)\]', attrType)
|
||||
subClass = match.group(1)
|
||||
subValues = []
|
||||
if not value:
|
||||
setattr(instance, attr, None)
|
||||
else:
|
||||
for subValue in value:
|
||||
subValues.append(self.deserialize(subValue,
|
||||
subClass))
|
||||
setattr(instance, attr, subValues)
|
||||
else:
|
||||
setattr(instance, attr, self.deserialize(value,
|
||||
attrType))
|
||||
if attr in obj:
|
||||
value = obj[attr]
|
||||
if attrType in ['str', 'int', 'float', 'bool']:
|
||||
attrType = eval(attrType)
|
||||
try:
|
||||
value = attrType(value)
|
||||
except UnicodeEncodeError:
|
||||
value = unicode(value)
|
||||
except TypeError:
|
||||
value = value
|
||||
setattr(instance, attr, value)
|
||||
elif (attrType == 'datetime'):
|
||||
setattr(instance, attr, self._parseDatetime(value))
|
||||
elif 'list[' in attrType:
|
||||
match = re.match('list\[(.*)\]', attrType)
|
||||
subClass = match.group(1)
|
||||
subValues = []
|
||||
if not value:
|
||||
setattr(instance, attr, None)
|
||||
else:
|
||||
for subValue in value:
|
||||
subValues.append(self.deserialize(subValue,
|
||||
subClass))
|
||||
setattr(instance, attr, subValues)
|
||||
else:
|
||||
setattr(instance, attr, self.deserialize(value,
|
||||
attrType))
|
||||
|
||||
return instance
|
||||
return instance
|
||||
|
||||
|
||||
class MethodRequest(urllib.request.Request):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Construct a MethodRequest. Usage is the same as for
|
||||
`urllib.Request` except it also takes an optional `method`
|
||||
keyword argument. If supplied, `method` will be used instead of
|
||||
the default."""
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Construct a MethodRequest. Usage is the same as for
|
||||
`urllib.Request` except it also takes an optional `method`
|
||||
keyword argument. If supplied, `method` will be used instead of
|
||||
the default."""
|
||||
|
||||
if 'method' in kwargs:
|
||||
self.method = kwargs.pop('method')
|
||||
return urllib.request.Request.__init__(self, *args, **kwargs)
|
||||
if 'method' in kwargs:
|
||||
self.method = kwargs.pop('method')
|
||||
return urllib.request.Request.__init__(self, *args, **kwargs)
|
||||
|
||||
def get_method(self):
|
||||
return getattr(self, 'method', urllib.request.Request.get_method(self))
|
||||
def get_method(self):
|
||||
return getattr(self, 'method', urllib.request.Request.get_method(self))
|
||||
|
||||
Reference in New Issue
Block a user