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:
@@ -48,6 +48,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
typeMapping.put("date", "date");
|
||||
typeMapping.put("DateTime", "datetime");
|
||||
typeMapping.put("object", "object");
|
||||
typeMapping.put("file", "file");
|
||||
|
||||
// from https://docs.python.org/release/2.5.4/ref/keywords.html
|
||||
reservedWords = new HashSet<String>(
|
||||
|
||||
@@ -11,13 +11,13 @@ python setup.py install
|
||||
Or you can install from Github via pip:
|
||||
|
||||
```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:
|
||||
|
||||
```python
|
||||
import SwaggerPetstore
|
||||
import swagger_client
|
||||
```
|
||||
|
||||
## 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:
|
||||
|
||||
```python
|
||||
import path.to.SwaggerPetstore-python.SwaggerPetstore
|
||||
import path.to.swagger_client
|
||||
```
|
||||
|
||||
## Getting Started
|
||||
@@ -38,9 +38,9 @@ TODO
|
||||
|
||||
## 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
|
||||
$ make test
|
||||
@@ -71,4 +71,3 @@ $ make test-all
|
||||
py34: commands succeeded
|
||||
congratulations :)
|
||||
```
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ class {{classname}}(object):
|
||||
|
||||
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
|
||||
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}}
|
||||
return response
|
||||
{{/returnType}}{{/operation}}
|
||||
|
||||
@@ -9,6 +9,7 @@ templates."""
|
||||
from __future__ import absolute_import
|
||||
from . import models
|
||||
from .rest import RESTClient
|
||||
from .rest import ApiException
|
||||
|
||||
import os
|
||||
import re
|
||||
@@ -17,6 +18,7 @@ import json
|
||||
import datetime
|
||||
import mimetypes
|
||||
import random
|
||||
import tempfile
|
||||
|
||||
# python 2 and python 3 compatibility library
|
||||
from six import iteritems
|
||||
@@ -59,7 +61,7 @@ class ApiClient(object):
|
||||
self.default_headers[header_name] = header_value
|
||||
|
||||
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
|
||||
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,
|
||||
post_params=post_params, body=body)
|
||||
|
||||
self.last_response = response_data
|
||||
|
||||
# deserialize response data
|
||||
if response:
|
||||
return self.deserialize(response_data, response)
|
||||
if response_type:
|
||||
return self.deserialize(response_data, response_type)
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -151,82 +155,62 @@ class ApiClient(object):
|
||||
return {key: self.sanitize_for_serialization(val)
|
||||
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 obj_class: class literal for deserialzied object, or string of class name
|
||||
:param response: RESTResponse object to be deserialized
|
||||
: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
|
||||
# native Python type, or one of the model classes.
|
||||
if type(obj_class) == str:
|
||||
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]
|
||||
# handle file downloading - save response body into a tmp file and return the instance
|
||||
if "file" == response_type:
|
||||
return self.__deserialize_file(response)
|
||||
|
||||
if 'dict(' in obj_class:
|
||||
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:
|
||||
value = attr_type(value)
|
||||
except UnicodeEncodeError:
|
||||
value = unicode(value)
|
||||
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
|
||||
|
||||
def __parse_string_to_datetime(self, string):
|
||||
"""
|
||||
Parse datetime in string to datetime.
|
||||
|
||||
The string should be in iso8601 datetime format.
|
||||
"""
|
||||
# fetch data from response object
|
||||
try:
|
||||
from dateutil.parser import parse
|
||||
return parse(string)
|
||||
except ImportError:
|
||||
return string
|
||||
data = json.loads(response.data)
|
||||
except ValueError:
|
||||
data = response.data
|
||||
|
||||
return self.__deserialize(data, response_type)
|
||||
|
||||
def __deserialize(self, data, klass):
|
||||
"""
|
||||
:param data: dict, list or str
|
||||
:param klass: class literal, or string of class name
|
||||
|
||||
:return: object
|
||||
"""
|
||||
if data is None:
|
||||
return None
|
||||
|
||||
if type(klass) == str:
|
||||
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):
|
||||
"""
|
||||
@@ -298,7 +282,7 @@ class ApiClient(object):
|
||||
"""
|
||||
if not auth_settings:
|
||||
return
|
||||
|
||||
|
||||
for auth in auth_settings:
|
||||
auth_setting = configuration.auth_settings().get(auth)
|
||||
if auth_setting:
|
||||
@@ -308,3 +292,83 @@ class ApiClient(object):
|
||||
querys[auth_setting['key']] = auth_setting['value']
|
||||
else:
|
||||
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 = ''
|
||||
password = ''
|
||||
|
||||
|
||||
# Temp foloder for file download
|
||||
temp_folder_path = None
|
||||
|
||||
@@ -125,24 +125,15 @@ class RESTClientObject(object):
|
||||
headers=headers)
|
||||
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.
|
||||
# we need to decode it to string.
|
||||
if sys.version_info > (3,):
|
||||
data = response.data.decode('utf8')
|
||||
else:
|
||||
data = response.data
|
||||
try:
|
||||
resp = json.loads(data)
|
||||
except ValueError:
|
||||
resp = data
|
||||
r.data = r.data.decode('utf8')
|
||||
|
||||
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):
|
||||
return self.request("GET", url, headers=headers, query_params=query_params)
|
||||
@@ -164,37 +155,32 @@ class RESTClientObject(object):
|
||||
|
||||
|
||||
class ApiException(Exception):
|
||||
"""
|
||||
Non-2xx HTTP response
|
||||
"""
|
||||
|
||||
def __init__(self, http_resp):
|
||||
self.status = http_resp.status
|
||||
self.reason = http_resp.reason
|
||||
self.body = http_resp.data
|
||||
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')
|
||||
def __init__(self, status=None, reason=None, http_resp=None):
|
||||
if http_resp:
|
||||
self.status = http_resp.status
|
||||
self.reason = http_resp.reason
|
||||
self.body = http_resp.data
|
||||
self.headers = http_resp.getheaders()
|
||||
else:
|
||||
data = self.body
|
||||
|
||||
try:
|
||||
self.body = json.loads(data)
|
||||
except ValueError:
|
||||
self.body = data
|
||||
self.status = status
|
||||
self.reason = reason
|
||||
self.body = None
|
||||
self.headers = None
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
Custom error response messages
|
||||
Custom error messages for exception
|
||||
"""
|
||||
return "({0})\n"\
|
||||
"Reason: {1}\n"\
|
||||
"HTTP response headers: {2}\n"\
|
||||
"HTTP response body: {3}\n".\
|
||||
format(self.status, self.reason, self.headers, self.body)
|
||||
error_message = "({0})\n"\
|
||||
"Reason: {1}\n".format(self.status, self.reason)
|
||||
if self.headers:
|
||||
error_message += "HTTP response headers: {0}".format(self.headers)
|
||||
|
||||
if self.body:
|
||||
error_message += "HTTP response body: {0}".format(self.body)
|
||||
|
||||
return error_message
|
||||
|
||||
class RESTClient(object):
|
||||
"""
|
||||
|
||||
@@ -15,7 +15,7 @@ VERSION = "{{packageVersion}}"
|
||||
# Try reading the setuptools documentation:
|
||||
# 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(
|
||||
name=NAME,
|
||||
|
||||
Reference in New Issue
Block a user