From 1cc968983c0200211031c227779151c654443118 Mon Sep 17 00:00:00 2001 From: LeafyLappa <31704619+LeafyLappa@users.noreply.github.com> Date: Thu, 12 Aug 2021 15:12:32 +0300 Subject: [PATCH 1/2] Move handler execution into a separate class method --- starlette_jsonapi/resource.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/starlette_jsonapi/resource.py b/starlette_jsonapi/resource.py index 5c66c61..455ce9e 100644 --- a/starlette_jsonapi/resource.py +++ b/starlette_jsonapi/resource.py @@ -135,9 +135,7 @@ async def handle_request( try: if request.method not in cls.allowed_methods: raise JSONAPIException(status_code=405) - resource = cls(request, request_context, *args, **kwargs) - handler = getattr(resource, handler_name, None) - response = await handler(*args, **kwargs) + response = await cls.execute_handler(request, request_context, handler_name, *args, **kwargs) except Exception as e: response = await cls.handle_error(request, request_context, exc=e) @@ -148,7 +146,24 @@ async def handle_request( response = await cls.handle_error(request, request_context, exc=after_request_exc) return response + + @classmethod + async def execute_handler( + cls, request: Request, request_context: dict, handler_name: str, + *args, **kwargs + ) -> Response: + """ + Finds a handler on this resource given its name and calls it. + :param request: current HTTP request + :param request_context: current request context + :param handler_name: name of the handler callable + """ + resource = cls(request, request_context, *args, **kwargs) + handler = getattr(resource, handler_name, None) + response = await handler(*args, **kwargs) + return response + def process_sparse_fields_request(self, serialized_data: dict, many: bool = False) -> dict: """ Processes sparse fields requests by calling From 1cd7268fe78983c0203e4f65549f974d3f5d968f Mon Sep 17 00:00:00 2001 From: LeafyLappa <31704619+LeafyLappa@users.noreply.github.com> Date: Mon, 16 Aug 2021 15:52:44 +0300 Subject: [PATCH 2/2] Remove whitespace in empty lines --- starlette_jsonapi/resource.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/starlette_jsonapi/resource.py b/starlette_jsonapi/resource.py index 455ce9e..ff9249d 100644 --- a/starlette_jsonapi/resource.py +++ b/starlette_jsonapi/resource.py @@ -146,7 +146,7 @@ async def handle_request( response = await cls.handle_error(request, request_context, exc=after_request_exc) return response - + @classmethod async def execute_handler( cls, request: Request, request_context: dict, handler_name: str, @@ -163,7 +163,7 @@ async def execute_handler( handler = getattr(resource, handler_name, None) response = await handler(*args, **kwargs) return response - + def process_sparse_fields_request(self, serialized_data: dict, many: bool = False) -> dict: """ Processes sparse fields requests by calling