From fc474eab5fc4c74c5bcdbe0199c6c8f9351834d4 Mon Sep 17 00:00:00 2001 From: gyoganathan <84407907+gyoganathan@users.noreply.github.com> Date: Sat, 10 Jun 2023 00:27:18 -0700 Subject: [PATCH] Added BadRequestException as a subclass in python (#15722) Co-authored-by: Gomathi Yoganathan --- .../src/main/resources/python/exceptions.mustache | 4 ++++ .../src/main/resources/python/rest.mustache | 5 ++++- samples/client/echo_api/python/openapi_client/exceptions.py | 4 ++++ samples/client/echo_api/python/openapi_client/rest.py | 5 ++++- .../petstore/python-aiohttp/petstore_api/exceptions.py | 4 ++++ .../client/petstore/python/petstore_api/exceptions.py | 4 ++++ samples/openapi3/client/petstore/python/petstore_api/rest.py | 5 ++++- 7 files changed, 28 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/exceptions.mustache b/modules/openapi-generator/src/main/resources/python/exceptions.mustache index b1cecfa8b6b5..baee9004b1ea 100644 --- a/modules/openapi-generator/src/main/resources/python/exceptions.mustache +++ b/modules/openapi-generator/src/main/resources/python/exceptions.mustache @@ -116,6 +116,10 @@ class ApiException(OpenApiException): return error_message +class BadRequestException(ApiException): + + def __init__(self, status=None, reason=None, http_resp=None): + super(BadRequestException, self).__init__(status, reason, http_resp) class NotFoundException(ApiException): diff --git a/modules/openapi-generator/src/main/resources/python/rest.mustache b/modules/openapi-generator/src/main/resources/python/rest.mustache index e874b24f749e..c8748d7a6ef0 100644 --- a/modules/openapi-generator/src/main/resources/python/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/rest.mustache @@ -11,7 +11,7 @@ import ssl from urllib.parse import urlencode, quote_plus import urllib3 -from {{packageName}}.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError +from {{packageName}}.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError, BadRequestException logger = logging.getLogger(__name__) @@ -208,6 +208,9 @@ class RESTClientObject(object): logger.debug("response body: %s", r.data) if not 200 <= r.status <= 299: + if r.status == 400: + raise BadRequestException(http_resp=r) + if r.status == 401: raise UnauthorizedException(http_resp=r) diff --git a/samples/client/echo_api/python/openapi_client/exceptions.py b/samples/client/echo_api/python/openapi_client/exceptions.py index 8851d062b9cc..1b3d7a919195 100644 --- a/samples/client/echo_api/python/openapi_client/exceptions.py +++ b/samples/client/echo_api/python/openapi_client/exceptions.py @@ -127,6 +127,10 @@ class ApiException(OpenApiException): return error_message +class BadRequestException(ApiException): + + def __init__(self, status=None, reason=None, http_resp=None): + super(BadRequestException, self).__init__(status, reason, http_resp) class NotFoundException(ApiException): diff --git a/samples/client/echo_api/python/openapi_client/rest.py b/samples/client/echo_api/python/openapi_client/rest.py index 52b9dfd095d6..7a7cf80674b6 100644 --- a/samples/client/echo_api/python/openapi_client/rest.py +++ b/samples/client/echo_api/python/openapi_client/rest.py @@ -22,7 +22,7 @@ import ssl from urllib.parse import urlencode, quote_plus import urllib3 -from openapi_client.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError +from openapi_client.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError, BadRequestException logger = logging.getLogger(__name__) @@ -219,6 +219,9 @@ class RESTClientObject(object): logger.debug("response body: %s", r.data) if not 200 <= r.status <= 299: + if r.status == 400: + raise BadRequestException(http_resp=r) + if r.status == 401: raise UnauthorizedException(http_resp=r) diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/exceptions.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/exceptions.py index cb957db48b9d..0235e2d4d1d9 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/exceptions.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/exceptions.py @@ -126,6 +126,10 @@ class ApiException(OpenApiException): return error_message +class BadRequestException(ApiException): + + def __init__(self, status=None, reason=None, http_resp=None): + super(BadRequestException, self).__init__(status, reason, http_resp) class NotFoundException(ApiException): diff --git a/samples/openapi3/client/petstore/python/petstore_api/exceptions.py b/samples/openapi3/client/petstore/python/petstore_api/exceptions.py index cb957db48b9d..0235e2d4d1d9 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/exceptions.py +++ b/samples/openapi3/client/petstore/python/petstore_api/exceptions.py @@ -126,6 +126,10 @@ class ApiException(OpenApiException): return error_message +class BadRequestException(ApiException): + + def __init__(self, status=None, reason=None, http_resp=None): + super(BadRequestException, self).__init__(status, reason, http_resp) class NotFoundException(ApiException): diff --git a/samples/openapi3/client/petstore/python/petstore_api/rest.py b/samples/openapi3/client/petstore/python/petstore_api/rest.py index 51c0ff683bd8..babca7e51708 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python/petstore_api/rest.py @@ -21,7 +21,7 @@ import ssl from urllib.parse import urlencode, quote_plus import urllib3 -from petstore_api.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError +from petstore_api.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError, BadRequestException logger = logging.getLogger(__name__) @@ -218,6 +218,9 @@ class RESTClientObject(object): logger.debug("response body: %s", r.data) if not 200 <= r.status <= 299: + if r.status == 400: + raise BadRequestException(http_resp=r) + if r.status == 401: raise UnauthorizedException(http_resp=r)