Esempio n. 1
0
                        def let(responder=responder):
                            @wraps(responder)
                            def do_before_all(self, req, resp, **kwargs):
                                action(req, resp, kwargs)
                                responder(self, req, resp, **kwargs)

                            api_helpers._propagate_argspec(
                                do_before_all,
                                responder)

                            setattr(resource, responder_name, do_before_all)
Esempio n. 2
0
    def _before(responder_or_resource):
        if isinstance(responder_or_resource, six.class_types):
            resource = responder_or_resource

            for method in HTTP_METHODS:
                responder_name = 'on_' + method.lower()

                try:
                    responder = getattr(resource, responder_name)
                except AttributeError:
                    # resource does not implement this method
                    pass
                else:
                    # Usually expect a method, but any callable will do
                    if callable(responder):
                        # This pattern is necessary to capture the current
                        # value of responder in the do_before_all closure;
                        # otherwise, they will capture the same responder
                        # variable that is shared between iterations of the
                        # for loop, above.
                        def let(responder=responder):
                            @wraps(responder)
                            def do_before_all(self, req, resp, **kwargs):
                                action(req, resp, kwargs)
                                responder(self, req, resp, **kwargs)

                            api_helpers._propagate_argspec(
                                do_before_all,
                                responder)

                            setattr(resource, responder_name, do_before_all)

                        let()

            return resource

        else:
            responder = responder_or_resource

            @wraps(responder)
            def do_before_one(self, req, resp, **kwargs):
                action(req, resp, kwargs)
                responder(self, req, resp, **kwargs)

            api_helpers._propagate_argspec(do_before_one, responder)

            return do_before_one
Esempio n. 3
0
    def _after(responder_or_resource):
        if isinstance(responder_or_resource, six.class_types):
            resource = responder_or_resource

            for method in HTTP_METHODS:
                responder_name = 'on_' + method.lower()

                try:
                    responder = getattr(resource, responder_name)
                except AttributeError:
                    # resource does not implement this method
                    pass
                else:
                    # Usually expect a method, but any callable will do
                    if callable(responder):
                        def let(responder=responder):
                            @wraps(responder)
                            def do_after_all(self, req, resp, **kwargs):
                                responder(self, req, resp, **kwargs)
                                action(req, resp)

                            api_helpers._propagate_argspec(
                                do_after_all,
                                responder)

                            setattr(resource, responder_name, do_after_all)

                        let()

            return resource

        else:
            responder = responder_or_resource

            @wraps(responder)
            def do_after_one(self, req, resp, **kwargs):
                responder(self, req, resp, **kwargs)
                action(req, resp)

            api_helpers._propagate_argspec(do_after_one, responder)

            return do_after_one