From 8565bf37b26b7c2f00a26d1cd30df99e57df1fb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 8 Jul 2016 12:31:21 +0200 Subject: [PATCH] =?UTF-8?q?Use=20dict=20for=20HTTP=20status=20code=20?= =?UTF-8?q?=E2=86=92=20exception=20mapping.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pillarsdk/api.py | 36 ++++++++---------------------------- pillarsdk/exceptions.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/pillarsdk/api.py b/pillarsdk/api.py index abf5b39..405b3e6 100644 --- a/pillarsdk/api.py +++ b/pillarsdk/api.py @@ -143,35 +143,15 @@ class Api(object): """Check HTTP response codes """ status = response.status_code - if status in (301, 302, 303, 307): - raise exceptions.Redirection(response, content) - elif 200 <= status <= 299: + + if 200 <= status <= 299: return json.loads(content) if content else {} - elif status == 400: - raise exceptions.BadRequest(response, content) - elif status == 401: - raise exceptions.UnauthorizedAccess(response, content) - elif status == 403: - raise exceptions.ForbiddenAccess(response, content) - elif status == 404: - raise exceptions.ResourceNotFound(response, content) - elif status == 405: - raise exceptions.MethodNotAllowed(response, content) - elif status == 409: - raise exceptions.ResourceConflict(response, content) - elif status == 410: - raise exceptions.ResourceGone(response, content) - elif status == 412: - raise exceptions.PreconditionFailed(response, content) - elif status == 422: - raise exceptions.ResourceInvalid(response, content) - elif 401 <= status <= 499: - raise exceptions.ClientError(response, content) - elif 500 <= status <= 599: - raise exceptions.ServerError(response, content) - else: - raise exceptions.ConnectionError(response, - content, "Unknown response code: #{response.code}") + + exception = exceptions.exception_for_status(status) + if exception: + raise exception(response, content) + + raise exceptions.ConnectionError(response, content, "Unknown response code: %s" % status) def headers(self): """Default HTTP headers diff --git a/pillarsdk/exceptions.py b/pillarsdk/exceptions.py index 468aff3..95c4047 100644 --- a/pillarsdk/exceptions.py +++ b/pillarsdk/exceptions.py @@ -101,3 +101,36 @@ class MethodNotAllowed(ClientError): def allowed_methods(self): return self.response['Allow'] + + +_exception_map = { + 301: Redirection, + 302: Redirection, + 303: Redirection, + 307: Redirection, + 400: BadRequest, + 401: UnauthorizedAccess, + 403: ForbiddenAccess, + 404: ResourceNotFound, + 405: MethodNotAllowed, + 409: ResourceConflict, + 410: ResourceGone, + 412: PreconditionFailed, + 422: ResourceInvalid, +} + + +def exception_for_status(status_code): + """Returns the exception class for the given status code.""" + + try: + return _exception_map[status_code] + except KeyError: + pass + + if 400 <= status_code <= 499: + return ClientError + elif 500 <= status_code <= 599: + return ServerError + + return None