Coder Social home page Coder Social logo

Comments (1)

swistakm avatar swistakm commented on May 22, 2024

Idea - simply add context argument to every implementation of http method calls

Implementation:
Implementation would require modifying every HTTP method handler in the resource.mixins module. Example:

class ListMixin(BaseMixin):
    ...

    def on_get(self, req, resp, handler=None, **kwargs):
        kwargs['context'] = req.context

        self.handle(
            handler or self.list, req, resp, **kwargs
        )

As the effect developer may use three methods to provide custom per-request context variables:

  • using HTTP method overrides

    class MyResource(ListAPI):
        def on_get(req, resp, **kwargs):
            req.context['session'] = Session()
            super().on_get(req, resp, **kwargs)
    
        def list(self, params, meta, context, **kwargs):
            # context object content available here
            ...
  • using middleware

    class SessionMiddleware:
        def process_resource(self, req, resp, resource, params):
            req.context['session'] = Session()
  • using hooks:

    def create_session(req, resp, resource, params):
        req.context['session'] = Session()
    
    @falcon.before(create_session)
    class MyResource(ListAPI):
        def on_get(req, resp, **kwargs):
            req.context['session'] = Session()
            super().on_get(req, resp, **kwargs)
    
        def list(self, params, meta, context, **kwargs):
            # context object content available here
            ...

Pros of this approach are:

  • simple implementation
  • low risk of breaking backwards incompatibility
  • compatible with existing techniques of providing context in falcon
  • compatible with any existing falcon extension built using hooks or middleware

Also if we improve support for falcon hooks (per-method falcon hooks with falcon.before) this solution will still work as expected.

Backward compatibility concerns:

Good practice is to allow passing any set of keyword arguments to resource manipulation methods using generic and basic resources in graceful. Anyway we cannot be sure that every developer uses this approach. We should expect that graceful users sometimes do following:

class MyResourceList(ListAPI):
    def list(params, meta):
        pass

# or

class MyResourceItem(RetrieveAPI):
    def retrieve(params, meta, object_id):  # <= object id from URI template
        pass

We could ensure backwards compatibility by using class keyword arguments:

class MyResourceItemWithCtx(RetrieveAPI, with_context=True):
    def retrieve(params, meta, context, **kwargs):
        pass

class MyResourceItemWithoutCtx(RetrieveAPI, with_context=False):
    def retrieve(params, meta, **kwargs):
        pass

This is feature of Python 3 but we do not target anything below Python 3.3. Of course default for with_context for 0.x branch would be False and for 1.x would be True

from graceful.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.