forked from loafle/openapi-generator-original
[python] exposes deserialized bodies for non-2XX responses (#14095)
* Template update and sample update * Samples regenerated * Adds verification test * Template update * Samples regen, fixes exception instantiation
This commit is contained in:
@@ -8,6 +8,10 @@
|
||||
The version of the OpenAPI document: 1.0.0
|
||||
Generated by: https://openapi-generator.tech
|
||||
"""
|
||||
import dataclasses
|
||||
import typing
|
||||
|
||||
from urllib3._collections import HTTPHeaderDict
|
||||
|
||||
|
||||
class OpenApiException(Exception):
|
||||
@@ -97,19 +101,26 @@ class ApiKeyError(OpenApiException, KeyError):
|
||||
super(ApiKeyError, self).__init__(full_msg)
|
||||
|
||||
|
||||
class ApiException(OpenApiException):
|
||||
T = typing.TypeVar("T")
|
||||
|
||||
def __init__(self, status=None, reason=None, api_response: 'dynamic_servers.api_client.ApiResponse' = None):
|
||||
if api_response:
|
||||
self.status = api_response.response.status
|
||||
self.reason = api_response.response.reason
|
||||
self.body = api_response.response.data
|
||||
self.headers = api_response.response.getheaders()
|
||||
else:
|
||||
self.status = status
|
||||
self.reason = reason
|
||||
self.body = None
|
||||
self.headers = None
|
||||
|
||||
@dataclasses.dataclass
|
||||
class ApiException(OpenApiException, typing.Generic[T]):
|
||||
status: int
|
||||
reason: str
|
||||
api_response: typing.Optional[T] = None
|
||||
|
||||
@property
|
||||
def body(self) -> typing.Union[str, bytes, None]:
|
||||
if not self.api_response:
|
||||
return None
|
||||
return self.api_response.response.data
|
||||
|
||||
@property
|
||||
def headers(self) -> typing.Optional[HTTPHeaderDict]:
|
||||
if not self.api_response:
|
||||
return None
|
||||
return self.api_response.response.getheaders()
|
||||
|
||||
def __str__(self):
|
||||
"""Custom error messages for exception"""
|
||||
|
||||
@@ -183,7 +183,11 @@ class BaseApi(api_client.Api):
|
||||
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
|
||||
|
||||
if not 200 <= response.status <= 299:
|
||||
raise exceptions.ApiException(api_response=api_response)
|
||||
raise exceptions.ApiException(
|
||||
status=response.status,
|
||||
reason=response.reason,
|
||||
api_response=api_response
|
||||
)
|
||||
|
||||
return api_response
|
||||
|
||||
|
||||
@@ -128,7 +128,11 @@ class BaseApi(api_client.Api):
|
||||
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
|
||||
|
||||
if not 200 <= response.status <= 299:
|
||||
raise exceptions.ApiException(api_response=api_response)
|
||||
raise exceptions.ApiException(
|
||||
status=response.status,
|
||||
reason=response.reason,
|
||||
api_response=api_response
|
||||
)
|
||||
|
||||
return api_response
|
||||
|
||||
|
||||
@@ -126,7 +126,11 @@ class BaseApi(api_client.Api):
|
||||
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
|
||||
|
||||
if not 200 <= response.status <= 299:
|
||||
raise exceptions.ApiException(api_response=api_response)
|
||||
raise exceptions.ApiException(
|
||||
status=response.status,
|
||||
reason=response.reason,
|
||||
api_response=api_response
|
||||
)
|
||||
|
||||
return api_response
|
||||
|
||||
|
||||
@@ -121,7 +121,11 @@ class BaseApi(api_client.Api):
|
||||
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
|
||||
|
||||
if not 200 <= response.status <= 299:
|
||||
raise exceptions.ApiException(api_response=api_response)
|
||||
raise exceptions.ApiException(
|
||||
status=response.status,
|
||||
reason=response.reason,
|
||||
api_response=api_response
|
||||
)
|
||||
|
||||
return api_response
|
||||
|
||||
|
||||
Reference in New Issue
Block a user