inveniosoftware / flask-resources

REST APIs for Flask
https://flask-resources.readthedocs.io
MIT License
3 stars 21 forks source link

views: context, loaders and handlers refactor #44

Closed ppanero closed 4 years ago

ppanero commented 4 years ago

The selection of loader and response handler is done in the content negotiation and it is stored in the resource_requestctx. Then this object is updated based on the result of a call to a function from its attributes... Sounds a bit wrong. Propsal:

Option 1:

Move the _response_handlers and _request_loader to the BaseView since all views seem to have it.

Create a function or decorator similar to the following. It would be called at the beginning of each method (get, post, etc.)

    def _select_from_mimetype():
        """Selects the response handler and request loader based on the MIMETypes."""

        self.request_loader = self._request_loaders[resource_requestctx.accept_mimetype]
        self.response_handler = self._request_loaders[
            resource_requestctx.payload_mimetype
        ]

Option 2:

Create 2 decorators:

    def _select_response_handler_from_mimetype():
        """Selects the response handler based on the MIMETypes."""

        self.response_handler = self._request_loaders[
            resource_requestctx.payload_mimetype
        ]

    def _select_request_loader_from_mimetype():
        """Selects the request loader based on the MIMETypes."""

        self.request_loader = self._request_loaders[resource_requestctx.accept_mimetype]

Since not every function requires both loader and handler, only those that require would call them.

fenekku commented 4 years ago

@ppanero is this still applicable? or can we close it?

ppanero commented 4 years ago

Nope :) We actually have an unused update function in the resource_requestctx... Im not sure if it is worth removing since it could actually be useful. In any case... closing.