inveniosoftware / flask-resources

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

context: update context based on dict #43

Closed ppanero closed 4 years ago

ppanero commented 4 years ago

Requires #42 Closes https://github.com/inveniosoftware/flask-resources/issues/38

Changes:

Open questions:

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.

To allow this PR to be merged, opened a different issue to tackle said problem: https://github.com/inveniosoftware/flask-resources/issues/44