diff --git a/starlette_jsonapi/resource.py b/starlette_jsonapi/resource.py index 5c66c61..ff9249d 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) @@ -149,6 +147,23 @@ async def handle_request( 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