forked from loafle/openapi-generator-original
Update Python client.
Throw ApiException if fail to parse string to datetime.
This commit is contained in:
@@ -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
|
||||||
@@ -102,7 +103,7 @@ class ApiClient(object):
|
|||||||
post_params=post_params, body=body)
|
post_params=post_params, body=body)
|
||||||
|
|
||||||
self.last_response = response_data
|
self.last_response = response_data
|
||||||
|
|
||||||
# deserialize response data
|
# deserialize response data
|
||||||
if response_type:
|
if response_type:
|
||||||
return self.deserialize(response_data, response_type)
|
return self.deserialize(response_data, response_type)
|
||||||
@@ -278,7 +279,7 @@ class ApiClient(object):
|
|||||||
"""
|
"""
|
||||||
if not auth_settings:
|
if not auth_settings:
|
||||||
return
|
return
|
||||||
|
|
||||||
for auth in auth_settings:
|
for auth in auth_settings:
|
||||||
auth_setting = configuration.auth_settings().get(auth)
|
auth_setting = configuration.auth_settings().get(auth)
|
||||||
if auth_setting:
|
if auth_setting:
|
||||||
@@ -294,13 +295,13 @@ class ApiClient(object):
|
|||||||
Save response body into a file in (the defined) temporary folder, using the filename
|
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.
|
from the `Content-Disposition` header if provided, otherwise a random filename.
|
||||||
|
|
||||||
:param response: RESTResponse
|
:param response: RESTResponse
|
||||||
:return: file path
|
:return: file path
|
||||||
"""
|
"""
|
||||||
fd, path = tempfile.mkstemp(dir=configuration.temp_folder_path)
|
fd, path = tempfile.mkstemp(dir=configuration.temp_folder_path)
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
os.remove(path)
|
os.remove(path)
|
||||||
|
|
||||||
content_disposition = response.getheader("Content-Disposition")
|
content_disposition = response.getheader("Content-Disposition")
|
||||||
if content_disposition:
|
if content_disposition:
|
||||||
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', content_disposition).group(1)
|
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', content_disposition).group(1)
|
||||||
@@ -314,7 +315,7 @@ class ApiClient(object):
|
|||||||
def __deserialize_primitive(self, data, klass):
|
def __deserialize_primitive(self, data, klass):
|
||||||
"""
|
"""
|
||||||
Deserialize string to primitive type
|
Deserialize string to primitive type
|
||||||
|
|
||||||
:param data: str
|
:param data: str
|
||||||
:param klass: class literal
|
:param klass: class literal
|
||||||
|
|
||||||
@@ -348,6 +349,8 @@ class ApiClient(object):
|
|||||||
return parse(string)
|
return parse(string)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
return string
|
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):
|
def __deserialize_model(self, data, klass):
|
||||||
"""
|
"""
|
||||||
@@ -378,11 +381,3 @@ class ApiClient(object):
|
|||||||
setattr(instance, attr, self.__deserialize(value, attr_type))
|
setattr(instance, attr, self.__deserialize(value, attr_type))
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ class RESTClientObject(object):
|
|||||||
r.data = r.data.decode('utf8')
|
r.data = r.data.decode('utf8')
|
||||||
|
|
||||||
if r.status not in range(200, 206):
|
if r.status not in range(200, 206):
|
||||||
raise ApiException(r)
|
raise ApiException(http_resp=r)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
@@ -155,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):
|
||||||
self.status = http_resp.status
|
if http_resp:
|
||||||
self.reason = http_resp.reason
|
self.status = http_resp.status
|
||||||
self.body = http_resp.data
|
self.reason = http_resp.reason
|
||||||
self.headers = http_resp.getheaders()
|
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')
|
|
||||||
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):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -102,7 +103,7 @@ class ApiClient(object):
|
|||||||
post_params=post_params, body=body)
|
post_params=post_params, body=body)
|
||||||
|
|
||||||
self.last_response = response_data
|
self.last_response = response_data
|
||||||
|
|
||||||
# deserialize response data
|
# deserialize response data
|
||||||
if response_type:
|
if response_type:
|
||||||
return self.deserialize(response_data, response_type)
|
return self.deserialize(response_data, response_type)
|
||||||
@@ -278,7 +279,7 @@ class ApiClient(object):
|
|||||||
"""
|
"""
|
||||||
if not auth_settings:
|
if not auth_settings:
|
||||||
return
|
return
|
||||||
|
|
||||||
for auth in auth_settings:
|
for auth in auth_settings:
|
||||||
auth_setting = configuration.auth_settings().get(auth)
|
auth_setting = configuration.auth_settings().get(auth)
|
||||||
if auth_setting:
|
if auth_setting:
|
||||||
@@ -294,13 +295,13 @@ class ApiClient(object):
|
|||||||
Save response body into a file in (the defined) temporary folder, using the filename
|
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.
|
from the `Content-Disposition` header if provided, otherwise a random filename.
|
||||||
|
|
||||||
:param response: RESTResponse
|
:param response: RESTResponse
|
||||||
:return: file path
|
:return: file path
|
||||||
"""
|
"""
|
||||||
fd, path = tempfile.mkstemp(dir=configuration.temp_folder_path)
|
fd, path = tempfile.mkstemp(dir=configuration.temp_folder_path)
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
os.remove(path)
|
os.remove(path)
|
||||||
|
|
||||||
content_disposition = response.getheader("Content-Disposition")
|
content_disposition = response.getheader("Content-Disposition")
|
||||||
if content_disposition:
|
if content_disposition:
|
||||||
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', content_disposition).group(1)
|
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', content_disposition).group(1)
|
||||||
@@ -314,7 +315,7 @@ class ApiClient(object):
|
|||||||
def __deserialize_primitive(self, data, klass):
|
def __deserialize_primitive(self, data, klass):
|
||||||
"""
|
"""
|
||||||
Deserialize string to primitive type
|
Deserialize string to primitive type
|
||||||
|
|
||||||
:param data: str
|
:param data: str
|
||||||
:param klass: class literal
|
:param klass: class literal
|
||||||
|
|
||||||
@@ -348,6 +349,8 @@ class ApiClient(object):
|
|||||||
return parse(string)
|
return parse(string)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
return string
|
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):
|
def __deserialize_model(self, data, klass):
|
||||||
"""
|
"""
|
||||||
@@ -378,11 +381,3 @@ class ApiClient(object):
|
|||||||
setattr(instance, attr, self.__deserialize(value, attr_type))
|
setattr(instance, attr, self.__deserialize(value, attr_type))
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ class RESTClientObject(object):
|
|||||||
r.data = r.data.decode('utf8')
|
r.data = r.data.decode('utf8')
|
||||||
|
|
||||||
if r.status not in range(200, 206):
|
if r.status not in range(200, 206):
|
||||||
raise ApiException(r)
|
raise ApiException(http_resp=r)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
@@ -155,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):
|
||||||
self.status = http_resp.status
|
if http_resp:
|
||||||
self.reason = http_resp.reason
|
self.status = http_resp.status
|
||||||
self.body = http_resp.data
|
self.reason = http_resp.reason
|
||||||
self.headers = http_resp.getheaders()
|
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')
|
|
||||||
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):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user