forked from loafle/openapi-generator-original
Merge pull request #935 from geekerzp/python-file-response
[Python] Support file downloading for Python API client
This commit is contained in:
commit
dd35c672f0
3
.gitignore
vendored
3
.gitignore
vendored
@ -45,3 +45,6 @@ samples/client/petstore/php/SwaggerClient-php/vendor/
|
|||||||
|
|
||||||
samples/client/petstore/silex/SwaggerServer/composer.lock
|
samples/client/petstore/silex/SwaggerServer/composer.lock
|
||||||
samples/client/petstore/silex/SwaggerServer/venodr/
|
samples/client/petstore/silex/SwaggerServer/venodr/
|
||||||
|
|
||||||
|
samples/client/petstore/python/.projectile
|
||||||
|
samples/client/petstore/python/.venv/
|
||||||
|
@ -48,6 +48,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
typeMapping.put("date", "date");
|
typeMapping.put("date", "date");
|
||||||
typeMapping.put("DateTime", "datetime");
|
typeMapping.put("DateTime", "datetime");
|
||||||
typeMapping.put("object", "object");
|
typeMapping.put("object", "object");
|
||||||
|
typeMapping.put("file", "file");
|
||||||
|
|
||||||
// from https://docs.python.org/release/2.5.4/ref/keywords.html
|
// from https://docs.python.org/release/2.5.4/ref/keywords.html
|
||||||
reservedWords = new HashSet<String>(
|
reservedWords = new HashSet<String>(
|
||||||
|
@ -11,13 +11,13 @@ python setup.py install
|
|||||||
Or you can install from Github via pip:
|
Or you can install from Github via pip:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
pip install git+https://github.com/geekerzp/SwaggerPetstore-python.git
|
pip install git+https://github.com/geekerzp/swagger_client.git
|
||||||
```
|
```
|
||||||
|
|
||||||
To use the bindings, import the pacakge:
|
To use the bindings, import the pacakge:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import SwaggerPetstore
|
import swagger_client
|
||||||
```
|
```
|
||||||
|
|
||||||
## Manual Installation
|
## Manual Installation
|
||||||
@ -25,7 +25,7 @@ If you do not wish to use setuptools, you can download the latest release.
|
|||||||
Then, to use the bindings, import the package:
|
Then, to use the bindings, import the package:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import path.to.SwaggerPetstore-python.SwaggerPetstore
|
import path.to.swagger_client
|
||||||
```
|
```
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
@ -38,9 +38,9 @@ TODO
|
|||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
(Make sure you are running it inside of a [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/))
|
(Please make sure you have [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/) installed)
|
||||||
|
|
||||||
You can run the tests in the current python platform:
|
Execute the following command to run the tests in the current Python (v2 or v3) environment:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ make test
|
$ make test
|
||||||
@ -71,4 +71,3 @@ $ make test-all
|
|||||||
py34: commands succeeded
|
py34: commands succeeded
|
||||||
congratulations :)
|
congratulations :)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ class {{classname}}(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}}, auth_settings=auth_settings)
|
response_type={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}}, auth_settings=auth_settings)
|
||||||
{{#returnType}}
|
{{#returnType}}
|
||||||
return response
|
return response
|
||||||
{{/returnType}}{{/operation}}
|
{{/returnType}}{{/operation}}
|
||||||
|
@ -9,6 +9,7 @@ templates."""
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from . import models
|
from . import models
|
||||||
from .rest import RESTClient
|
from .rest import RESTClient
|
||||||
|
from .rest import ApiException
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
@ -17,6 +18,7 @@ import json
|
|||||||
import datetime
|
import datetime
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import random
|
import random
|
||||||
|
import tempfile
|
||||||
|
|
||||||
# python 2 and python 3 compatibility library
|
# python 2 and python 3 compatibility library
|
||||||
from six import iteritems
|
from six import iteritems
|
||||||
@ -59,7 +61,7 @@ class ApiClient(object):
|
|||||||
self.default_headers[header_name] = header_value
|
self.default_headers[header_name] = header_value
|
||||||
|
|
||||||
def call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None,
|
def call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None,
|
||||||
body=None, post_params=None, files=None, response=None, auth_settings=None):
|
body=None, post_params=None, files=None, response_type=None, auth_settings=None):
|
||||||
|
|
||||||
# headers parameters
|
# headers parameters
|
||||||
header_params = header_params or {}
|
header_params = header_params or {}
|
||||||
@ -100,9 +102,11 @@ class ApiClient(object):
|
|||||||
response_data = self.request(method, url, query_params=query_params, headers=header_params,
|
response_data = self.request(method, url, query_params=query_params, headers=header_params,
|
||||||
post_params=post_params, body=body)
|
post_params=post_params, body=body)
|
||||||
|
|
||||||
|
self.last_response = response_data
|
||||||
|
|
||||||
# deserialize response data
|
# deserialize response data
|
||||||
if response:
|
if response_type:
|
||||||
return self.deserialize(response_data, response)
|
return self.deserialize(response_data, response_type)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -151,82 +155,62 @@ class ApiClient(object):
|
|||||||
return {key: self.sanitize_for_serialization(val)
|
return {key: self.sanitize_for_serialization(val)
|
||||||
for key, val in iteritems(obj_dict)}
|
for key, val in iteritems(obj_dict)}
|
||||||
|
|
||||||
def deserialize(self, obj, obj_class):
|
def deserialize(self, response, response_type):
|
||||||
"""
|
"""
|
||||||
Derialize a JSON string into an object.
|
Derialize response into an object.
|
||||||
|
|
||||||
:param obj: string or object to be deserialized
|
:param response: RESTResponse object to be deserialized
|
||||||
:param obj_class: class literal for deserialzied object, or string of class name
|
:param response_type: class literal for deserialzied object, or string of class name
|
||||||
|
|
||||||
:return object: deserialized object
|
:return: deserialized object
|
||||||
"""
|
"""
|
||||||
# Have to accept obj_class as string or actual type. Type could be a
|
# handle file downloading - save response body into a tmp file and return the instance
|
||||||
# native Python type, or one of the model classes.
|
if "file" == response_type:
|
||||||
if type(obj_class) == str:
|
return self.__deserialize_file(response)
|
||||||
if 'list[' in obj_class:
|
|
||||||
match = re.match('list\[(.*)\]', obj_class)
|
|
||||||
sub_class = match.group(1)
|
|
||||||
return [self.deserialize(sub_obj, sub_class) for sub_obj in obj]
|
|
||||||
|
|
||||||
if 'dict(' in obj_class:
|
# fetch data from response object
|
||||||
match = re.match('dict\((.*), (.*)\)', obj_class)
|
|
||||||
sub_class = match.group(2)
|
|
||||||
return {k: self.deserialize(v, sub_class) for k, v in iteritems(obj)}
|
|
||||||
|
|
||||||
if obj_class in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime', "object"]:
|
|
||||||
obj_class = eval(obj_class)
|
|
||||||
else: # not a native type, must be model class
|
|
||||||
obj_class = eval('models.' + obj_class)
|
|
||||||
|
|
||||||
if obj_class in [int, float, dict, list, str, bool]:
|
|
||||||
return obj_class(obj)
|
|
||||||
elif obj_class == object:
|
|
||||||
return object()
|
|
||||||
elif obj_class == datetime:
|
|
||||||
return self.__parse_string_to_datetime(obj)
|
|
||||||
|
|
||||||
instance = obj_class()
|
|
||||||
|
|
||||||
for attr, attr_type in iteritems(instance.swagger_types):
|
|
||||||
if obj is not None and instance.attribute_map[attr] in obj and type(obj) in [list, dict]:
|
|
||||||
value = obj[instance.attribute_map[attr]]
|
|
||||||
if attr_type in ['str', 'int', 'float', 'bool']:
|
|
||||||
attr_type = eval(attr_type)
|
|
||||||
try:
|
try:
|
||||||
value = attr_type(value)
|
data = json.loads(response.data)
|
||||||
except UnicodeEncodeError:
|
except ValueError:
|
||||||
value = unicode(value)
|
data = response.data
|
||||||
except TypeError:
|
|
||||||
value = value
|
|
||||||
setattr(instance, attr, value)
|
|
||||||
elif attr_type == 'datetime':
|
|
||||||
setattr(instance, attr, self.__parse_string_to_datetime(value))
|
|
||||||
elif 'list[' in attr_type:
|
|
||||||
match = re.match('list\[(.*)\]', attr_type)
|
|
||||||
sub_class = match.group(1)
|
|
||||||
sub_values = []
|
|
||||||
if not value:
|
|
||||||
setattr(instance, attr, None)
|
|
||||||
else:
|
|
||||||
for sub_value in value:
|
|
||||||
sub_values.append(self.deserialize(sub_value, sub_class))
|
|
||||||
setattr(instance, attr, sub_values)
|
|
||||||
else:
|
|
||||||
setattr(instance, attr, self.deserialize(value, attr_type))
|
|
||||||
|
|
||||||
return instance
|
return self.__deserialize(data, response_type)
|
||||||
|
|
||||||
def __parse_string_to_datetime(self, string):
|
def __deserialize(self, data, klass):
|
||||||
"""
|
"""
|
||||||
Parse datetime in string to datetime.
|
:param data: dict, list or str
|
||||||
|
:param klass: class literal, or string of class name
|
||||||
|
|
||||||
The string should be in iso8601 datetime format.
|
:return: object
|
||||||
"""
|
"""
|
||||||
try:
|
if data is None:
|
||||||
from dateutil.parser import parse
|
return None
|
||||||
return parse(string)
|
|
||||||
except ImportError:
|
if type(klass) == str:
|
||||||
return string
|
if 'list[' in klass:
|
||||||
|
sub_kls = re.match('list\[(.*)\]', klass).group(1)
|
||||||
|
return [self.__deserialize(sub_data, sub_kls) for sub_data in data]
|
||||||
|
|
||||||
|
if 'dict(' in klass:
|
||||||
|
sub_kls = re.match('dict\((.*), (.*)\)', klass).group(2)
|
||||||
|
return {k: self.__deserialize(v, sub_kls) for k, v in iteritems(data)}
|
||||||
|
|
||||||
|
# convert str to class
|
||||||
|
# for native types
|
||||||
|
if klass in ['int', 'float', 'str', 'bool', 'datetime', "object"]:
|
||||||
|
klass = eval(klass)
|
||||||
|
# for model types
|
||||||
|
else:
|
||||||
|
klass = eval('models.' + klass)
|
||||||
|
|
||||||
|
if klass in [int, float, str, bool]:
|
||||||
|
return self.__deserialize_primitive(data, klass)
|
||||||
|
elif klass == object:
|
||||||
|
return self.__deserialize_object()
|
||||||
|
elif klass == datetime:
|
||||||
|
return self.__deserialize_datatime(data)
|
||||||
|
else:
|
||||||
|
return self.__deserialize_model(data, klass)
|
||||||
|
|
||||||
def request(self, method, url, query_params=None, headers=None, post_params=None, body=None):
|
def request(self, method, url, query_params=None, headers=None, post_params=None, body=None):
|
||||||
"""
|
"""
|
||||||
@ -308,3 +292,83 @@ class ApiClient(object):
|
|||||||
querys[auth_setting['key']] = auth_setting['value']
|
querys[auth_setting['key']] = auth_setting['value']
|
||||||
else:
|
else:
|
||||||
raise ValueError('Authentication token must be in `query` or `header`')
|
raise ValueError('Authentication token must be in `query` or `header`')
|
||||||
|
|
||||||
|
def __deserialize_file(self, response):
|
||||||
|
"""
|
||||||
|
Save response body into a file in (the defined) temporary folder, using the filename
|
||||||
|
from the `Content-Disposition` header if provided, otherwise a random filename.
|
||||||
|
|
||||||
|
:param response: RESTResponse
|
||||||
|
:return: file path
|
||||||
|
"""
|
||||||
|
fd, path = tempfile.mkstemp(dir=configuration.temp_folder_path)
|
||||||
|
os.close(fd)
|
||||||
|
os.remove(path)
|
||||||
|
|
||||||
|
content_disposition = response.getheader("Content-Disposition")
|
||||||
|
if content_disposition:
|
||||||
|
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', content_disposition).group(1)
|
||||||
|
path = os.path.join(os.path.dirname(path), filename)
|
||||||
|
|
||||||
|
with open(path, "w") as f:
|
||||||
|
f.write(response.data)
|
||||||
|
|
||||||
|
return path
|
||||||
|
|
||||||
|
def __deserialize_primitive(self, data, klass):
|
||||||
|
"""
|
||||||
|
Deserialize string to primitive type
|
||||||
|
|
||||||
|
:param data: str
|
||||||
|
:param klass: class literal
|
||||||
|
|
||||||
|
:return: int, float, str, bool
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
value = klass(data)
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
value = unicode(data)
|
||||||
|
except TypeError:
|
||||||
|
value = data
|
||||||
|
return value
|
||||||
|
|
||||||
|
def __deserialize_object(self):
|
||||||
|
"""
|
||||||
|
Deserialize empty object
|
||||||
|
"""
|
||||||
|
return object()
|
||||||
|
|
||||||
|
def __deserialize_datatime(self, string):
|
||||||
|
"""
|
||||||
|
Deserialize string to datetime.
|
||||||
|
|
||||||
|
The string should be in iso8601 datetime format.
|
||||||
|
|
||||||
|
:param string: str
|
||||||
|
:return: datetime
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
from dateutil.parser import parse
|
||||||
|
return parse(string)
|
||||||
|
except ImportError:
|
||||||
|
return string
|
||||||
|
except ValueError:
|
||||||
|
raise ApiException(status=0, reason="Failed to parse `{0}` into a datetime object".format(string))
|
||||||
|
|
||||||
|
def __deserialize_model(self, data, klass):
|
||||||
|
"""
|
||||||
|
Deserialize list or dict to model
|
||||||
|
|
||||||
|
:param data: dict, list
|
||||||
|
:param klass: class literal
|
||||||
|
"""
|
||||||
|
instance = klass()
|
||||||
|
|
||||||
|
for attr, attr_type in iteritems(instance.swagger_types):
|
||||||
|
if data is not None \
|
||||||
|
and instance.attribute_map[attr] in data\
|
||||||
|
and isinstance(data, (list, dict)):
|
||||||
|
value = data[instance.attribute_map[attr]]
|
||||||
|
setattr(instance, attr, self.__deserialize(value, attr_type))
|
||||||
|
|
||||||
|
return instance
|
||||||
|
@ -48,4 +48,5 @@ api_key_prefix = {}
|
|||||||
username = ''
|
username = ''
|
||||||
password = ''
|
password = ''
|
||||||
|
|
||||||
|
# Temp foloder for file download
|
||||||
|
temp_folder_path = None
|
||||||
|
@ -125,24 +125,15 @@ class RESTClientObject(object):
|
|||||||
headers=headers)
|
headers=headers)
|
||||||
r = RESTResponse(r)
|
r = RESTResponse(r)
|
||||||
|
|
||||||
if r.status not in range(200, 206):
|
|
||||||
raise ApiException(r)
|
|
||||||
|
|
||||||
return self.process_response(r)
|
|
||||||
|
|
||||||
def process_response(self, response):
|
|
||||||
# In the python 3, the response.data is bytes.
|
# In the python 3, the response.data is bytes.
|
||||||
# we need to decode it to string.
|
# we need to decode it to string.
|
||||||
if sys.version_info > (3,):
|
if sys.version_info > (3,):
|
||||||
data = response.data.decode('utf8')
|
r.data = r.data.decode('utf8')
|
||||||
else:
|
|
||||||
data = response.data
|
|
||||||
try:
|
|
||||||
resp = json.loads(data)
|
|
||||||
except ValueError:
|
|
||||||
resp = data
|
|
||||||
|
|
||||||
return resp
|
if r.status not in range(200, 206):
|
||||||
|
raise ApiException(http_resp=r)
|
||||||
|
|
||||||
|
return r
|
||||||
|
|
||||||
def GET(self, url, headers=None, query_params=None):
|
def GET(self, url, headers=None, query_params=None):
|
||||||
return self.request("GET", url, headers=headers, query_params=query_params)
|
return self.request("GET", url, headers=headers, query_params=query_params)
|
||||||
@ -164,37 +155,32 @@ class RESTClientObject(object):
|
|||||||
|
|
||||||
|
|
||||||
class ApiException(Exception):
|
class ApiException(Exception):
|
||||||
"""
|
|
||||||
Non-2xx HTTP response
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, http_resp):
|
def __init__(self, status=None, reason=None, http_resp=None):
|
||||||
|
if http_resp:
|
||||||
self.status = http_resp.status
|
self.status = http_resp.status
|
||||||
self.reason = http_resp.reason
|
self.reason = http_resp.reason
|
||||||
self.body = http_resp.data
|
self.body = http_resp.data
|
||||||
self.headers = http_resp.getheaders()
|
self.headers = http_resp.getheaders()
|
||||||
|
|
||||||
# In the python 3, the self.body is bytes.
|
|
||||||
# we need to decode it to string.
|
|
||||||
if sys.version_info > (3,):
|
|
||||||
data = self.body.decode('utf8')
|
|
||||||
else:
|
else:
|
||||||
data = self.body
|
self.status = status
|
||||||
|
self.reason = reason
|
||||||
try:
|
self.body = None
|
||||||
self.body = json.loads(data)
|
self.headers = None
|
||||||
except ValueError:
|
|
||||||
self.body = data
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""
|
"""
|
||||||
Custom error response messages
|
Custom error messages for exception
|
||||||
"""
|
"""
|
||||||
return "({0})\n"\
|
error_message = "({0})\n"\
|
||||||
"Reason: {1}\n"\
|
"Reason: {1}\n".format(self.status, self.reason)
|
||||||
"HTTP response headers: {2}\n"\
|
if self.headers:
|
||||||
"HTTP response body: {3}\n".\
|
error_message += "HTTP response headers: {0}".format(self.headers)
|
||||||
format(self.status, self.reason, self.headers, self.body)
|
|
||||||
|
if self.body:
|
||||||
|
error_message += "HTTP response body: {0}".format(self.body)
|
||||||
|
|
||||||
|
return error_message
|
||||||
|
|
||||||
class RESTClient(object):
|
class RESTClient(object):
|
||||||
"""
|
"""
|
||||||
|
@ -15,7 +15,7 @@ VERSION = "{{packageVersion}}"
|
|||||||
# Try reading the setuptools documentation:
|
# Try reading the setuptools documentation:
|
||||||
# http://pypi.python.org/pypi/setuptools
|
# http://pypi.python.org/pypi/setuptools
|
||||||
|
|
||||||
REQUIRES = ["urllib3 >= 1.10", "six >= 1.9", "certifi"]
|
REQUIRES = ["urllib3 >= 1.10", "six >= 1.9", "certifi", "python-dateutil"]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name=NAME,
|
name=NAME,
|
||||||
|
12
samples/client/petstore/python/.travis.yml
Normal file
12
samples/client/petstore/python/.travis.yml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
language: python
|
||||||
|
python:
|
||||||
|
- "2.6"
|
||||||
|
- "2.7"
|
||||||
|
- "3.2"
|
||||||
|
- "3.3"
|
||||||
|
- "3.4"
|
||||||
|
install:
|
||||||
|
- pip install . --use-mirrors
|
||||||
|
- pip install -r dev-requirements.txt --use-mirrors
|
||||||
|
script:
|
||||||
|
- nosetests
|
@ -1,37 +1,21 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
REQUIREMENTS_FILE=dev-requirements.txt
|
REQUIREMENTS_FILE=dev-requirements.txt
|
||||||
REQUIREMENTS_OUT=dev-requirements.txt.log
|
REQUIREMENTS_OUT=dev-requirements.txt.log
|
||||||
SETUP_OUT=*.egg-info
|
SETUP_OUT=*.egg-info
|
||||||
|
VENV=.venv
|
||||||
virtualenv:
|
|
||||||
ifndef VIRTUAL_ENV
|
|
||||||
$(error Must be run inside of a virtualenv\
|
|
||||||
http://docs.python-guide.org/en/latest/dev/virtualenvs/)
|
|
||||||
endif
|
|
||||||
|
|
||||||
setup: virtualenv $(SETUP_OUT)
|
|
||||||
|
|
||||||
$(SETUP_OUT): setup.py setup.cfg
|
|
||||||
python setup.py develop
|
|
||||||
touch $(SETUP_OUT)
|
|
||||||
|
|
||||||
$(REQUIREMENTS_OUT): $(REQUIREMENTS_FILE)
|
|
||||||
pip install -r $(REQUIREMENTS_FILE) | tee -a $(REQUIREMENTS_OUT)
|
|
||||||
python setup.py develop
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(REQUIREMENTS_OUT)
|
rm -rf $(REQUIREMENTS_OUT)
|
||||||
rm -rf $(SETUP_OUT)
|
rm -rf $(SETUP_OUT)
|
||||||
|
rm -rf $(VENV)
|
||||||
rm -rf .tox
|
rm -rf .tox
|
||||||
rm -rf .coverage
|
rm -rf .coverage
|
||||||
find . -name "*.py[oc]" -delete
|
find . -name "*.py[oc]" -delete
|
||||||
find . -name "__pycache__" -delete
|
find . -name "__pycache__" -delete
|
||||||
|
|
||||||
requirements: setup $(REQUIREMENTS_OUT)
|
test: clean
|
||||||
|
bash ./test.sh
|
||||||
test: clean requirements
|
|
||||||
nosetests
|
|
||||||
|
|
||||||
test-all: clean requirements
|
|
||||||
tox
|
|
||||||
|
|
||||||
|
|
||||||
|
test-all: clean
|
||||||
|
bash ./test-all.sh
|
||||||
|
@ -11,13 +11,13 @@ python setup.py install
|
|||||||
Or you can install from Github via pip:
|
Or you can install from Github via pip:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
pip install git+https://github.com/geekerzp/SwaggerPetstore-python.git
|
pip install git+https://github.com/geekerzp/swagger_client.git
|
||||||
```
|
```
|
||||||
|
|
||||||
To use the bindings, import the pacakge:
|
To use the bindings, import the pacakge:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import SwaggerPetstore
|
import swagger_client
|
||||||
```
|
```
|
||||||
|
|
||||||
## Manual Installation
|
## Manual Installation
|
||||||
@ -25,7 +25,7 @@ If you do not wish to use setuptools, you can download the latest release.
|
|||||||
Then, to use the bindings, import the package:
|
Then, to use the bindings, import the package:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import path.to.SwaggerPetstore-python.SwaggerPetstore
|
import path.to.swagger_client
|
||||||
```
|
```
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
@ -38,9 +38,9 @@ TODO
|
|||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
(Make sure you are running it inside of a [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/))
|
(Please make sure you have [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/) installed)
|
||||||
|
|
||||||
You can run the tests in the current python platform:
|
Execute the following command to run the tests in the current Python (v2 or v3) environment:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ make test
|
$ make test
|
||||||
@ -71,4 +71,3 @@ $ make test-all
|
|||||||
py34: commands succeeded
|
py34: commands succeeded
|
||||||
congratulations :)
|
congratulations :)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -44,14 +44,3 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ verbosity=2
|
|||||||
randomize=true
|
randomize=true
|
||||||
exe=true
|
exe=true
|
||||||
with-coverage=true
|
with-coverage=true
|
||||||
cover-package=SwaggerPetstore
|
cover-package=swagger_client
|
||||||
cover-erase=true
|
cover-erase=true
|
||||||
|
|
||||||
[flake8]
|
[flake8]
|
||||||
|
@ -15,7 +15,7 @@ VERSION = "1.0.0"
|
|||||||
# Try reading the setuptools documentation:
|
# Try reading the setuptools documentation:
|
||||||
# http://pypi.python.org/pypi/setuptools
|
# http://pypi.python.org/pypi/setuptools
|
||||||
|
|
||||||
REQUIRES = ["urllib3 >= 1.10", "six >= 1.9", "certifi"]
|
REQUIRES = ["urllib3 >= 1.10", "six >= 1.9", "certifi", "python-dateutil"]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name=NAME,
|
name=NAME,
|
||||||
|
@ -9,6 +9,7 @@ templates."""
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from . import models
|
from . import models
|
||||||
from .rest import RESTClient
|
from .rest import RESTClient
|
||||||
|
from .rest import ApiException
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
@ -17,6 +18,7 @@ import json
|
|||||||
import datetime
|
import datetime
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import random
|
import random
|
||||||
|
import tempfile
|
||||||
|
|
||||||
# python 2 and python 3 compatibility library
|
# python 2 and python 3 compatibility library
|
||||||
from six import iteritems
|
from six import iteritems
|
||||||
@ -59,7 +61,7 @@ class ApiClient(object):
|
|||||||
self.default_headers[header_name] = header_value
|
self.default_headers[header_name] = header_value
|
||||||
|
|
||||||
def call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None,
|
def call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None,
|
||||||
body=None, post_params=None, files=None, response=None, auth_settings=None):
|
body=None, post_params=None, files=None, response_type=None, auth_settings=None):
|
||||||
|
|
||||||
# headers parameters
|
# headers parameters
|
||||||
header_params = header_params or {}
|
header_params = header_params or {}
|
||||||
@ -100,9 +102,11 @@ class ApiClient(object):
|
|||||||
response_data = self.request(method, url, query_params=query_params, headers=header_params,
|
response_data = self.request(method, url, query_params=query_params, headers=header_params,
|
||||||
post_params=post_params, body=body)
|
post_params=post_params, body=body)
|
||||||
|
|
||||||
|
self.last_response = response_data
|
||||||
|
|
||||||
# deserialize response data
|
# deserialize response data
|
||||||
if response:
|
if response_type:
|
||||||
return self.deserialize(response_data, response)
|
return self.deserialize(response_data, response_type)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -151,82 +155,62 @@ class ApiClient(object):
|
|||||||
return {key: self.sanitize_for_serialization(val)
|
return {key: self.sanitize_for_serialization(val)
|
||||||
for key, val in iteritems(obj_dict)}
|
for key, val in iteritems(obj_dict)}
|
||||||
|
|
||||||
def deserialize(self, obj, obj_class):
|
def deserialize(self, response, response_type):
|
||||||
"""
|
"""
|
||||||
Derialize a JSON string into an object.
|
Derialize response into an object.
|
||||||
|
|
||||||
:param obj: string or object to be deserialized
|
:param response: RESTResponse object to be deserialized
|
||||||
:param obj_class: class literal for deserialzied object, or string of class name
|
:param response_type: class literal for deserialzied object, or string of class name
|
||||||
|
|
||||||
:return object: deserialized object
|
:return: deserialized object
|
||||||
"""
|
"""
|
||||||
# Have to accept obj_class as string or actual type. Type could be a
|
# handle file downloading - save response body into a tmp file and return the instance
|
||||||
# native Python type, or one of the model classes.
|
if "file" == response_type:
|
||||||
if type(obj_class) == str:
|
return self.__deserialize_file(response)
|
||||||
if 'list[' in obj_class:
|
|
||||||
match = re.match('list\[(.*)\]', obj_class)
|
|
||||||
sub_class = match.group(1)
|
|
||||||
return [self.deserialize(sub_obj, sub_class) for sub_obj in obj]
|
|
||||||
|
|
||||||
if 'dict(' in obj_class:
|
# fetch data from response object
|
||||||
match = re.match('dict\((.*), (.*)\)', obj_class)
|
|
||||||
sub_class = match.group(2)
|
|
||||||
return {k: self.deserialize(v, sub_class) for k, v in iteritems(obj)}
|
|
||||||
|
|
||||||
if obj_class in ['int', 'float', 'dict', 'list', 'str', 'bool', 'datetime', "object"]:
|
|
||||||
obj_class = eval(obj_class)
|
|
||||||
else: # not a native type, must be model class
|
|
||||||
obj_class = eval('models.' + obj_class)
|
|
||||||
|
|
||||||
if obj_class in [int, float, dict, list, str, bool]:
|
|
||||||
return obj_class(obj)
|
|
||||||
elif obj_class == object:
|
|
||||||
return object()
|
|
||||||
elif obj_class == datetime:
|
|
||||||
return self.__parse_string_to_datetime(obj)
|
|
||||||
|
|
||||||
instance = obj_class()
|
|
||||||
|
|
||||||
for attr, attr_type in iteritems(instance.swagger_types):
|
|
||||||
if obj is not None and instance.attribute_map[attr] in obj and type(obj) in [list, dict]:
|
|
||||||
value = obj[instance.attribute_map[attr]]
|
|
||||||
if attr_type in ['str', 'int', 'float', 'bool']:
|
|
||||||
attr_type = eval(attr_type)
|
|
||||||
try:
|
try:
|
||||||
value = attr_type(value)
|
data = json.loads(response.data)
|
||||||
except UnicodeEncodeError:
|
except ValueError:
|
||||||
value = unicode(value)
|
data = response.data
|
||||||
except TypeError:
|
|
||||||
value = value
|
|
||||||
setattr(instance, attr, value)
|
|
||||||
elif attr_type == 'datetime':
|
|
||||||
setattr(instance, attr, self.__parse_string_to_datetime(value))
|
|
||||||
elif 'list[' in attr_type:
|
|
||||||
match = re.match('list\[(.*)\]', attr_type)
|
|
||||||
sub_class = match.group(1)
|
|
||||||
sub_values = []
|
|
||||||
if not value:
|
|
||||||
setattr(instance, attr, None)
|
|
||||||
else:
|
|
||||||
for sub_value in value:
|
|
||||||
sub_values.append(self.deserialize(sub_value, sub_class))
|
|
||||||
setattr(instance, attr, sub_values)
|
|
||||||
else:
|
|
||||||
setattr(instance, attr, self.deserialize(value, attr_type))
|
|
||||||
|
|
||||||
return instance
|
return self.__deserialize(data, response_type)
|
||||||
|
|
||||||
def __parse_string_to_datetime(self, string):
|
def __deserialize(self, data, klass):
|
||||||
"""
|
"""
|
||||||
Parse datetime in string to datetime.
|
:param data: dict, list or str
|
||||||
|
:param klass: class literal, or string of class name
|
||||||
|
|
||||||
The string should be in iso8601 datetime format.
|
:return: object
|
||||||
"""
|
"""
|
||||||
try:
|
if data is None:
|
||||||
from dateutil.parser import parse
|
return None
|
||||||
return parse(string)
|
|
||||||
except ImportError:
|
if type(klass) == str:
|
||||||
return string
|
if 'list[' in klass:
|
||||||
|
sub_kls = re.match('list\[(.*)\]', klass).group(1)
|
||||||
|
return [self.__deserialize(sub_data, sub_kls) for sub_data in data]
|
||||||
|
|
||||||
|
if 'dict(' in klass:
|
||||||
|
sub_kls = re.match('dict\((.*), (.*)\)', klass).group(2)
|
||||||
|
return {k: self.__deserialize(v, sub_kls) for k, v in iteritems(data)}
|
||||||
|
|
||||||
|
# convert str to class
|
||||||
|
# for native types
|
||||||
|
if klass in ['int', 'float', 'str', 'bool', 'datetime', "object"]:
|
||||||
|
klass = eval(klass)
|
||||||
|
# for model types
|
||||||
|
else:
|
||||||
|
klass = eval('models.' + klass)
|
||||||
|
|
||||||
|
if klass in [int, float, str, bool]:
|
||||||
|
return self.__deserialize_primitive(data, klass)
|
||||||
|
elif klass == object:
|
||||||
|
return self.__deserialize_object()
|
||||||
|
elif klass == datetime:
|
||||||
|
return self.__deserialize_datatime(data)
|
||||||
|
else:
|
||||||
|
return self.__deserialize_model(data, klass)
|
||||||
|
|
||||||
def request(self, method, url, query_params=None, headers=None, post_params=None, body=None):
|
def request(self, method, url, query_params=None, headers=None, post_params=None, body=None):
|
||||||
"""
|
"""
|
||||||
@ -308,3 +292,83 @@ class ApiClient(object):
|
|||||||
querys[auth_setting['key']] = auth_setting['value']
|
querys[auth_setting['key']] = auth_setting['value']
|
||||||
else:
|
else:
|
||||||
raise ValueError('Authentication token must be in `query` or `header`')
|
raise ValueError('Authentication token must be in `query` or `header`')
|
||||||
|
|
||||||
|
def __deserialize_file(self, response):
|
||||||
|
"""
|
||||||
|
Save response body into a file in (the defined) temporary folder, using the filename
|
||||||
|
from the `Content-Disposition` header if provided, otherwise a random filename.
|
||||||
|
|
||||||
|
:param response: RESTResponse
|
||||||
|
:return: file path
|
||||||
|
"""
|
||||||
|
fd, path = tempfile.mkstemp(dir=configuration.temp_folder_path)
|
||||||
|
os.close(fd)
|
||||||
|
os.remove(path)
|
||||||
|
|
||||||
|
content_disposition = response.getheader("Content-Disposition")
|
||||||
|
if content_disposition:
|
||||||
|
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', content_disposition).group(1)
|
||||||
|
path = os.path.join(os.path.dirname(path), filename)
|
||||||
|
|
||||||
|
with open(path, "w") as f:
|
||||||
|
f.write(response.data)
|
||||||
|
|
||||||
|
return path
|
||||||
|
|
||||||
|
def __deserialize_primitive(self, data, klass):
|
||||||
|
"""
|
||||||
|
Deserialize string to primitive type
|
||||||
|
|
||||||
|
:param data: str
|
||||||
|
:param klass: class literal
|
||||||
|
|
||||||
|
:return: int, float, str, bool
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
value = klass(data)
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
value = unicode(data)
|
||||||
|
except TypeError:
|
||||||
|
value = data
|
||||||
|
return value
|
||||||
|
|
||||||
|
def __deserialize_object(self):
|
||||||
|
"""
|
||||||
|
Deserialize empty object
|
||||||
|
"""
|
||||||
|
return object()
|
||||||
|
|
||||||
|
def __deserialize_datatime(self, string):
|
||||||
|
"""
|
||||||
|
Deserialize string to datetime.
|
||||||
|
|
||||||
|
The string should be in iso8601 datetime format.
|
||||||
|
|
||||||
|
:param string: str
|
||||||
|
:return: datetime
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
from dateutil.parser import parse
|
||||||
|
return parse(string)
|
||||||
|
except ImportError:
|
||||||
|
return string
|
||||||
|
except ValueError:
|
||||||
|
raise ApiException(status=0, reason="Failed to parse `{0}` into a datetime object".format(string))
|
||||||
|
|
||||||
|
def __deserialize_model(self, data, klass):
|
||||||
|
"""
|
||||||
|
Deserialize list or dict to model
|
||||||
|
|
||||||
|
:param data: dict, list
|
||||||
|
:param klass: class literal
|
||||||
|
"""
|
||||||
|
instance = klass()
|
||||||
|
|
||||||
|
for attr, attr_type in iteritems(instance.swagger_types):
|
||||||
|
if data is not None \
|
||||||
|
and instance.attribute_map[attr] in data\
|
||||||
|
and isinstance(data, (list, dict)):
|
||||||
|
value = data[instance.attribute_map[attr]]
|
||||||
|
setattr(instance, attr, self.__deserialize(value, attr_type))
|
||||||
|
|
||||||
|
return instance
|
||||||
|
@ -90,7 +90,7 @@ class PetApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
def add_pet(self, **kwargs):
|
def add_pet(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -141,7 +141,7 @@ class PetApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
def find_pets_by_status(self, **kwargs):
|
def find_pets_by_status(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -192,7 +192,7 @@ class PetApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response='list[Pet]', auth_settings=auth_settings)
|
response_type='list[Pet]', auth_settings=auth_settings)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -245,7 +245,7 @@ class PetApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response='list[Pet]', auth_settings=auth_settings)
|
response_type='list[Pet]', auth_settings=auth_settings)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -302,7 +302,7 @@ class PetApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response='Pet', auth_settings=auth_settings)
|
response_type='Pet', auth_settings=auth_settings)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -367,15 +367,15 @@ class PetApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
def delete_pet(self, pet_id, **kwargs):
|
def delete_pet(self, pet_id, **kwargs):
|
||||||
"""
|
"""
|
||||||
Deletes a pet
|
Deletes a pet
|
||||||
|
|
||||||
|
|
||||||
:param str api_key: (optional)
|
|
||||||
:param int pet_id: Pet id to delete (required)
|
:param int pet_id: Pet id to delete (required)
|
||||||
|
:param str api_key: (optional)
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
@ -384,7 +384,7 @@ class PetApi(object):
|
|||||||
if pet_id is None:
|
if pet_id is None:
|
||||||
raise ValueError("Missing the required parameter `pet_id` when calling `delete_pet`")
|
raise ValueError("Missing the required parameter `pet_id` when calling `delete_pet`")
|
||||||
|
|
||||||
all_params = ['api_key', 'pet_id']
|
all_params = ['pet_id', 'api_key']
|
||||||
|
|
||||||
params = locals()
|
params = locals()
|
||||||
for key, val in iteritems(params['kwargs']):
|
for key, val in iteritems(params['kwargs']):
|
||||||
@ -426,7 +426,7 @@ class PetApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
def upload_file(self, pet_id, **kwargs):
|
def upload_file(self, pet_id, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -435,7 +435,7 @@ class PetApi(object):
|
|||||||
|
|
||||||
:param int pet_id: ID of pet to update (required)
|
:param int pet_id: ID of pet to update (required)
|
||||||
:param str additional_metadata: Additional data to pass to server (optional)
|
:param str additional_metadata: Additional data to pass to server (optional)
|
||||||
:param File file: file to upload (optional)
|
:param file file: file to upload (optional)
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
@ -489,7 +489,7 @@ class PetApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ class StoreApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response='dict(str, int)', auth_settings=auth_settings)
|
response_type='dict(str, int)', auth_settings=auth_settings)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -139,7 +139,7 @@ class StoreApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response='Order', auth_settings=auth_settings)
|
response_type='Order', auth_settings=auth_settings)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -196,7 +196,7 @@ class StoreApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response='Order', auth_settings=auth_settings)
|
response_type='Order', auth_settings=auth_settings)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -253,7 +253,7 @@ class StoreApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ class UserApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
def create_users_with_array_input(self, **kwargs):
|
def create_users_with_array_input(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -141,7 +141,7 @@ class UserApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
def create_users_with_list_input(self, **kwargs):
|
def create_users_with_list_input(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -192,7 +192,7 @@ class UserApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
def login_user(self, **kwargs):
|
def login_user(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -247,7 +247,7 @@ class UserApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response='str', auth_settings=auth_settings)
|
response_type='str', auth_settings=auth_settings)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -296,7 +296,7 @@ class UserApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
def get_user_by_name(self, username, **kwargs):
|
def get_user_by_name(self, username, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -351,7 +351,7 @@ class UserApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response='User', auth_settings=auth_settings)
|
response_type='User', auth_settings=auth_settings)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -412,7 +412,7 @@ class UserApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
def delete_user(self, username, **kwargs):
|
def delete_user(self, username, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -467,7 +467,7 @@ class UserApi(object):
|
|||||||
|
|
||||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||||
body=body_params, post_params=form_params, files=files,
|
body=body_params, post_params=form_params, files=files,
|
||||||
response=None, auth_settings=auth_settings)
|
response_type=None, auth_settings=auth_settings)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,4 +41,5 @@ api_key_prefix = {}
|
|||||||
username = ''
|
username = ''
|
||||||
password = ''
|
password = ''
|
||||||
|
|
||||||
|
# Temp foloder for file download
|
||||||
|
temp_folder_path = None
|
||||||
|
@ -34,7 +34,7 @@ class Order(object):
|
|||||||
'id': 'int',
|
'id': 'int',
|
||||||
'pet_id': 'int',
|
'pet_id': 'int',
|
||||||
'quantity': 'int',
|
'quantity': 'int',
|
||||||
'ship_date': 'DateTime',
|
'ship_date': 'datetime',
|
||||||
'status': 'str',
|
'status': 'str',
|
||||||
'complete': 'bool'
|
'complete': 'bool'
|
||||||
}
|
}
|
||||||
@ -58,7 +58,7 @@ class Order(object):
|
|||||||
self.quantity = None # int
|
self.quantity = None # int
|
||||||
|
|
||||||
|
|
||||||
self.ship_date = None # DateTime
|
self.ship_date = None # datetime
|
||||||
|
|
||||||
# Order Status
|
# Order Status
|
||||||
self.status = None # str
|
self.status = None # str
|
||||||
|
@ -125,24 +125,15 @@ class RESTClientObject(object):
|
|||||||
headers=headers)
|
headers=headers)
|
||||||
r = RESTResponse(r)
|
r = RESTResponse(r)
|
||||||
|
|
||||||
if r.status not in range(200, 206):
|
|
||||||
raise ApiException(r)
|
|
||||||
|
|
||||||
return self.process_response(r)
|
|
||||||
|
|
||||||
def process_response(self, response):
|
|
||||||
# In the python 3, the response.data is bytes.
|
# In the python 3, the response.data is bytes.
|
||||||
# we need to decode it to string.
|
# we need to decode it to string.
|
||||||
if sys.version_info > (3,):
|
if sys.version_info > (3,):
|
||||||
data = response.data.decode('utf8')
|
r.data = r.data.decode('utf8')
|
||||||
else:
|
|
||||||
data = response.data
|
|
||||||
try:
|
|
||||||
resp = json.loads(data)
|
|
||||||
except ValueError:
|
|
||||||
resp = data
|
|
||||||
|
|
||||||
return resp
|
if r.status not in range(200, 206):
|
||||||
|
raise ApiException(http_resp=r)
|
||||||
|
|
||||||
|
return r
|
||||||
|
|
||||||
def GET(self, url, headers=None, query_params=None):
|
def GET(self, url, headers=None, query_params=None):
|
||||||
return self.request("GET", url, headers=headers, query_params=query_params)
|
return self.request("GET", url, headers=headers, query_params=query_params)
|
||||||
@ -164,37 +155,32 @@ class RESTClientObject(object):
|
|||||||
|
|
||||||
|
|
||||||
class ApiException(Exception):
|
class ApiException(Exception):
|
||||||
"""
|
|
||||||
Non-2xx HTTP response
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, http_resp):
|
def __init__(self, status=None, reason=None, http_resp=None):
|
||||||
|
if http_resp:
|
||||||
self.status = http_resp.status
|
self.status = http_resp.status
|
||||||
self.reason = http_resp.reason
|
self.reason = http_resp.reason
|
||||||
self.body = http_resp.data
|
self.body = http_resp.data
|
||||||
self.headers = http_resp.getheaders()
|
self.headers = http_resp.getheaders()
|
||||||
|
|
||||||
# In the python 3, the self.body is bytes.
|
|
||||||
# we need to decode it to string.
|
|
||||||
if sys.version_info > (3,):
|
|
||||||
data = self.body.decode('utf8')
|
|
||||||
else:
|
else:
|
||||||
data = self.body
|
self.status = status
|
||||||
|
self.reason = reason
|
||||||
try:
|
self.body = None
|
||||||
self.body = json.loads(data)
|
self.headers = None
|
||||||
except ValueError:
|
|
||||||
self.body = data
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""
|
"""
|
||||||
Custom error response messages
|
Custom error messages for exception
|
||||||
"""
|
"""
|
||||||
return "({0})\n"\
|
error_message = "({0})\n"\
|
||||||
"Reason: {1}\n"\
|
"Reason: {1}\n".format(self.status, self.reason)
|
||||||
"HTTP response headers: {2}\n"\
|
if self.headers:
|
||||||
"HTTP response body: {3}\n".\
|
error_message += "HTTP response headers: {0}".format(self.headers)
|
||||||
format(self.status, self.reason, self.headers, self.body)
|
|
||||||
|
if self.body:
|
||||||
|
error_message += "HTTP response body: {0}".format(self.body)
|
||||||
|
|
||||||
|
return error_message
|
||||||
|
|
||||||
class RESTClient(object):
|
class RESTClient(object):
|
||||||
"""
|
"""
|
||||||
|
26
samples/client/petstore/python/test-all.sh
Executable file
26
samples/client/petstore/python/test-all.sh
Executable file
@ -0,0 +1,26 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
REQUIREMENTS_FILE=dev-requirements.txt
|
||||||
|
REQUIREMENTS_OUT=dev-requirements.txt.log
|
||||||
|
SETUP_OUT=*.egg-info
|
||||||
|
VENV=.venv
|
||||||
|
DEACTIVE=false
|
||||||
|
|
||||||
|
### set virtualenv
|
||||||
|
if [ -z "$VIRTUAL_ENV" ]; then
|
||||||
|
virtualenv $VENV --no-site-packages --always-copy
|
||||||
|
source $VENV/bin/activate
|
||||||
|
DEACTIVE=true
|
||||||
|
fi
|
||||||
|
|
||||||
|
### install dependencies
|
||||||
|
pip install -r $REQUIREMENTS_FILE | tee -a $REQUIREMENTS_OUT
|
||||||
|
python setup.py develop
|
||||||
|
|
||||||
|
### run tests
|
||||||
|
tox
|
||||||
|
|
||||||
|
### deactivate virtualenv
|
||||||
|
if [ $DEACTIVE == true ]; then
|
||||||
|
deactivate
|
||||||
|
fi
|
27
samples/client/petstore/python/test.sh
Executable file
27
samples/client/petstore/python/test.sh
Executable file
@ -0,0 +1,27 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
REQUIREMENTS_FILE=dev-requirements.txt
|
||||||
|
REQUIREMENTS_OUT=dev-requirements.txt.log
|
||||||
|
SETUP_OUT=*.egg-info
|
||||||
|
VENV=.venv
|
||||||
|
DEACTIVE=false
|
||||||
|
|
||||||
|
### set virtualenv
|
||||||
|
if [ -z "$VIRTUAL_ENV" ]; then
|
||||||
|
virtualenv $VENV --no-site-packages --always-copy
|
||||||
|
source $VENV/bin/activate
|
||||||
|
DEACTIVE=true
|
||||||
|
fi
|
||||||
|
|
||||||
|
### install dependencies
|
||||||
|
pip install -r $REQUIREMENTS_FILE | tee -a $REQUIREMENTS_OUT
|
||||||
|
python setup.py develop
|
||||||
|
|
||||||
|
### run tests
|
||||||
|
nosetests
|
||||||
|
|
||||||
|
### deactivate virtualenv
|
||||||
|
if [ $DEACTIVE == true ]; then
|
||||||
|
deactivate
|
||||||
|
fi
|
||||||
|
|
@ -88,43 +88,3 @@ class ApiClientTests(unittest.TestCase):
|
|||||||
content_types = []
|
content_types = []
|
||||||
content_type = self.api_client.select_header_content_type(content_types)
|
content_type = self.api_client.select_header_content_type(content_types)
|
||||||
self.assertEqual(content_type, 'application/json')
|
self.assertEqual(content_type, 'application/json')
|
||||||
|
|
||||||
def test_deserialize_to_dict(self):
|
|
||||||
# dict(str, Pet)
|
|
||||||
json = {
|
|
||||||
'pet': {
|
|
||||||
"id": 0,
|
|
||||||
"category": {
|
|
||||||
"id": 0,
|
|
||||||
"name": "string"
|
|
||||||
},
|
|
||||||
"name": "doggie",
|
|
||||||
"photoUrls": [
|
|
||||||
"string"
|
|
||||||
],
|
|
||||||
"tags": [
|
|
||||||
{
|
|
||||||
"id": 0,
|
|
||||||
"name": "string"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"status": "available"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data = self.api_client.deserialize(json, 'dict(str, Pet)')
|
|
||||||
self.assertTrue(isinstance(data, dict))
|
|
||||||
self.assertTrue(isinstance(data['pet'], swagger_client.Pet))
|
|
||||||
|
|
||||||
# dict(str, int)
|
|
||||||
json = {
|
|
||||||
'integer': 1
|
|
||||||
}
|
|
||||||
|
|
||||||
data = self.api_client.deserialize(json, 'dict(str, int)')
|
|
||||||
self.assertTrue(isinstance(data, dict))
|
|
||||||
self.assertTrue(isinstance(data['integer'], int))
|
|
||||||
|
|
||||||
def test_deserialize_to_object(self):
|
|
||||||
data = self.api_client.deserialize("", "object")
|
|
||||||
self.assertTrue(type(data) == object)
|
|
||||||
|
@ -52,7 +52,7 @@ class ApiExceptionTests(unittest.TestCase):
|
|||||||
except ApiException as e:
|
except ApiException as e:
|
||||||
self.assertEqual(e.status, 404)
|
self.assertEqual(e.status, 404)
|
||||||
self.assertEqual(e.reason, "Not Found")
|
self.assertEqual(e.reason, "Not Found")
|
||||||
self.assertDictEqual(e.body, {'message': 'Pet not found', 'code': 1, 'type': 'error'})
|
self.assertRegexpMatches(e.body, "Pet not found")
|
||||||
|
|
||||||
def test_500_error(self):
|
def test_500_error(self):
|
||||||
self.pet_api.add_pet(body=self.pet)
|
self.pet_api.add_pet(body=self.pet)
|
||||||
|
150
samples/client/petstore/python/tests/test_deserialization.py
Normal file
150
samples/client/petstore/python/tests/test_deserialization.py
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
"""
|
||||||
|
Run the tests.
|
||||||
|
$ pip install nose (optional)
|
||||||
|
$ cd SwaggerPetstore-python
|
||||||
|
$ nosetests -v
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import unittest
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
import swagger_client
|
||||||
|
|
||||||
|
|
||||||
|
class DeserializationTests(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.api_client = swagger_client.ApiClient()
|
||||||
|
self.deserialize = self.api_client._ApiClient__deserialize
|
||||||
|
|
||||||
|
def test_deserialize_dict_str_pet(self):
|
||||||
|
""" deserialize dict(str, Pet) """
|
||||||
|
data = {
|
||||||
|
'pet': {
|
||||||
|
"id": 0,
|
||||||
|
"category": {
|
||||||
|
"id": 0,
|
||||||
|
"name": "string"
|
||||||
|
},
|
||||||
|
"name": "doggie",
|
||||||
|
"photoUrls": [
|
||||||
|
"string"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
{
|
||||||
|
"id": 0,
|
||||||
|
"name": "string"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"status": "available"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deserialized = self.deserialize(data, 'dict(str, Pet)')
|
||||||
|
self.assertTrue(isinstance(deserialized, dict))
|
||||||
|
self.assertTrue(isinstance(deserialized['pet'], swagger_client.Pet))
|
||||||
|
|
||||||
|
def test_deserialize_dict_str_int(self):
|
||||||
|
""" deserialize dict(str, int) """
|
||||||
|
data = {
|
||||||
|
'integer': 1
|
||||||
|
}
|
||||||
|
|
||||||
|
deserialized = self.deserialize(data, 'dict(str, int)')
|
||||||
|
self.assertTrue(isinstance(deserialized, dict))
|
||||||
|
self.assertTrue(isinstance(deserialized['integer'], int))
|
||||||
|
|
||||||
|
def test_deserialize_str(self):
|
||||||
|
""" deserialize str """
|
||||||
|
data = "test str"
|
||||||
|
deserialized = self.deserialize(data, "str")
|
||||||
|
self.assertTrue(isinstance(deserialized, str))
|
||||||
|
|
||||||
|
def test_deserialize_datetime(self):
|
||||||
|
""" deserialize dateimte """
|
||||||
|
data = "1997-07-16T19:20:30.45+01:00"
|
||||||
|
deserialized = self.deserialize(data, "datetime")
|
||||||
|
self.assertTrue(isinstance(deserialized, datetime.datetime))
|
||||||
|
|
||||||
|
def test_deserialize_pet(self):
|
||||||
|
""" deserialize pet """
|
||||||
|
data = {
|
||||||
|
"id": 0,
|
||||||
|
"category": {
|
||||||
|
"id": 0,
|
||||||
|
"name": "string"
|
||||||
|
},
|
||||||
|
"name": "doggie",
|
||||||
|
"photoUrls": [
|
||||||
|
"string"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
{
|
||||||
|
"id": 0,
|
||||||
|
"name": "string"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"status": "available"
|
||||||
|
}
|
||||||
|
deserialized = self.deserialize(data, "Pet")
|
||||||
|
self.assertTrue(isinstance(deserialized, swagger_client.Pet))
|
||||||
|
self.assertEqual(deserialized.id, 0)
|
||||||
|
self.assertEqual(deserialized.name, "doggie")
|
||||||
|
self.assertTrue(isinstance(deserialized.category, swagger_client.Category))
|
||||||
|
self.assertEqual(deserialized.category.name, "string")
|
||||||
|
self.assertTrue(isinstance(deserialized.tags, list))
|
||||||
|
self.assertEqual(deserialized.tags[0].name, "string")
|
||||||
|
|
||||||
|
def test_deserialize_list_of_pet(self):
|
||||||
|
""" deserialize list[Pet] """
|
||||||
|
data = [{
|
||||||
|
"id": 0,
|
||||||
|
"category": {
|
||||||
|
"id": 0,
|
||||||
|
"name": "string"
|
||||||
|
},
|
||||||
|
"name": "doggie0",
|
||||||
|
"photoUrls": [
|
||||||
|
"string"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
{
|
||||||
|
"id": 0,
|
||||||
|
"name": "string"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"status": "available"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"category": {
|
||||||
|
"id": 0,
|
||||||
|
"name": "string"
|
||||||
|
},
|
||||||
|
"name": "doggie1",
|
||||||
|
"photoUrls": [
|
||||||
|
"string"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
{
|
||||||
|
"id": 0,
|
||||||
|
"name": "string"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"status": "available"
|
||||||
|
}]
|
||||||
|
deserialized = self.deserialize(data, "list[Pet]")
|
||||||
|
self.assertTrue(isinstance(deserialized, list))
|
||||||
|
self.assertTrue(isinstance(deserialized[0], swagger_client.Pet))
|
||||||
|
self.assertEqual(deserialized[0].id, 0)
|
||||||
|
self.assertEqual(deserialized[1].id, 1)
|
||||||
|
self.assertEqual(deserialized[0].name, "doggie0")
|
||||||
|
self.assertEqual(deserialized[1].name, "doggie1")
|
||||||
|
|
||||||
|
def test_deserialize_none(self):
|
||||||
|
""" deserialize None """
|
||||||
|
deserialized = self.deserialize(None, "datetime")
|
||||||
|
self.assertIsNone(deserialized)
|
@ -11,14 +11,14 @@ import os
|
|||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import SwaggerPetstore
|
import swagger_client
|
||||||
from SwaggerPetstore.rest import ApiException
|
from swagger_client.rest import ApiException
|
||||||
|
|
||||||
|
|
||||||
class StoreApiTests(unittest.TestCase):
|
class StoreApiTests(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.store_api = SwaggerPetstore.StoreApi()
|
self.store_api = swagger_client.StoreApi()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
# sleep 1 sec between two every 2 tests
|
# sleep 1 sec between two every 2 tests
|
Loading…
x
Reference in New Issue
Block a user