Пример #1
0
    def add_route(self, uri_template, resource):
        """Associate a URI path with a resource

        A resource is an instance of a class that defines various on_*
        "responder" methods, one for each HTTP method the resource
        allows. For example, to support GET, simply define an "on_get"
        responder. If a client requests an unsupported method, Falcon
        will respond with "405 Method not allowed".

        Responders must always define at least two arguments to receive
        request and response objects, respectively. For example:

            def on_post(self, req, resp):
                pass

        In addition, if the route's uri template contains field
        expressions, any responders that desires to receive requests
        for that route must accept arguments named after the respective
        field names defined in the template. For example, given the
        following uri template:

            /das/{thing}

        A PUT request to "/das/code" would be routed to:

            def on_put(self, req, resp, thing):
                pass

        If, on the other hand, the responder had been defined thus:

            def on_put(self, req, resp):
                pass

        Falcon would respond to the client's request with "405 Method
        not allowed." This allows you to define multiple routes to the
        same resource, e.g., in order to support GET for "/widget/1234"
        and POST to "/widgets". In this last example, a POST to
        "/widget/5000" would result in a 405 response.

        Args:
            uri_template: Relative URI template. Currently only Level 1
                templates are supported. See also RFC 6570.
            resource: Object which represents an HTTP/REST "resource". Falcon
                will pass "GET" requests to on_get, "PUT" requests to on_put,
                etc. If any HTTP methods are not supported by your resource,
                simply don't define the corresponding request handlers, and
                Falcon will do the right thing.

        """

        uri_fields, path_template = helpers.compile_uri_template(uri_template)
        method_map, na_responder = helpers.create_http_method_map(
            resource, uri_fields, self._before, self._after)

        # Insert at the head of the list in case we get duplicate
        # adds (will cause the last one to win).
        self._routes.insert(0, (path_template, method_map, na_responder))
Пример #2
0
    def add_route(self, uri_template, resource):
        """Associate a URI path with a resource

        A resource is an instance of a class that defines various on_*
        "responder" methods, one for each HTTP method the resource
        allows. For example, to support GET, simply define an "on_get"
        responder. If a client requests an unsupported method, Falcon
        will respond with "405 Method not allowed".

        Responders must always define at least two arguments to receive
        request and response objects, respectively. For example:

            def on_post(self, req, resp):
                pass

        In addition, if the route's uri template contains field
        expressions, any responders that desires to receive requests
        for that route must accept arguments named after the respective
        field names defined in the template. For example, given the
        following uri template:

            /das/{thing}

        A PUT request to "/das/code" would be routed to:

            def on_put(self, req, resp, thing):
                pass

        If, on the other hand, the responder had been defined thus:

            def on_put(self, req, resp):
                pass

        Falcon would respond to the client's request with "405 Method
        not allowed." This allows you to define multiple routes to the
        same resource, e.g., in order to support GET for "/widget/1234"
        and POST to "/widgets". In this last example, a POST to
        "/widget/5000" would result in a 405 response.

        Args:
            uri_template: Relative URI template. Currently only Level 1
                templates are supported. See also RFC 6570.
            resource: Object which represents an HTTP/REST "resource". Falcon
                will pass "GET" requests to on_get, "PUT" requests to on_put,
                etc. If any HTTP methods are not supported by your resource,
                simply don't define the corresponding request handlers, and
                Falcon will do the right thing.

        """

        uri_fields, path_template = helpers.compile_uri_template(uri_template)
        method_map, na_responder = helpers.create_http_method_map(
            resource, uri_fields, self._before, self._after)

        # Insert at the head of the list in case we get duplicate
        # adds (will cause the last one to win).
        self._routes.insert(0, (path_template, method_map, na_responder))
Пример #3
0
    def add_route(self, uri_template, resource):
        """Associates a URI path with a resource.

        A resource is an instance of a class that defines various on_*
        "responder" methods, one for each HTTP method the resource
        allows. For example, to support GET, simply define an `on_get`
        responder. If a client requests an unsupported method, Falcon
        will respond with "405 Method not allowed".

        Responders must always define at least two arguments to receive
        request and response objects, respectively. For example::

            def on_post(self, req, resp):
                pass

        In addition, if the route's uri template contains field
        expressions, any responder that desires to receive requests
        for that route must accept arguments named after the respective
        field names defined in the template. For example, given the
        following uri template::

            /das/{thing}

        A PUT request to "/das/code" would be routed to::

            def on_put(self, req, resp, thing):
                pass

        Args:
            uri_template (str): Relative URI template. Currently only Level 1
                templates are supported. See also RFC 6570. Care must be
                taken to ensure the template does not mask any sink
                patterns (see also ``add_sink``).
            resource (instance): Object which represents an HTTP/REST
                "resource". Falcon will pass "GET" requests to on_get,
                "PUT" requests to on_put, etc. If any HTTP methods are not
                supported by your resource, simply don't define the
                corresponding request handlers, and Falcon will do the right
                thing.

        """

        uri_fields, path_template = helpers.compile_uri_template(uri_template)
        method_map = helpers.create_http_method_map(resource, uri_fields,
                                                    self._before, self._after)

        # Insert at the head of the list in case we get duplicate
        # adds (will cause the last one to win).
        self._routes.insert(0, (path_template, method_map, resource))
Пример #4
0
    def add_route(self, uri_template, resource):
        """Associates a URI path with a resource.

        A resource is an instance of a class that defines various on_*
        "responder" methods, one for each HTTP method the resource
        allows. For example, to support GET, simply define an `on_get`
        responder. If a client requests an unsupported method, Falcon
        will respond with "405 Method not allowed".

        Responders must always define at least two arguments to receive
        request and response objects, respectively. For example::

            def on_post(self, req, resp):
                pass

        In addition, if the route's uri template contains field
        expressions, any responder that desires to receive requests
        for that route must accept arguments named after the respective
        field names defined in the template. For example, given the
        following uri template::

            /das/{thing}

        A PUT request to "/das/code" would be routed to::

            def on_put(self, req, resp, thing):
                pass

        Args:
            uri_template (str): Relative URI template. Currently only Level 1
                templates are supported. See also RFC 6570. Care must be
                taken to ensure the template does not mask any sink
                patterns (see also ``add_sink``).
            resource (instance): Object which represents an HTTP/REST
                "resource". Falcon will pass "GET" requests to on_get,
                "PUT" requests to on_put, etc. If any HTTP methods are not
                supported by your resource, simply don't define the
                corresponding request handlers, and Falcon will do the right
                thing.

        """

        uri_fields, path_template = helpers.compile_uri_template(uri_template)
        method_map = helpers.create_http_method_map(
            resource, uri_fields, self._before, self._after)

        # Insert at the head of the list in case we get duplicate
        # adds (will cause the last one to win).
        self._routes.insert(0, (path_template, method_map))
Пример #5
0
    def add_route(self, uri_template, resource):
        """Associates uri patterns with resource methods.

        A resource is an instance of a class that defines various methods
        to handle http requests.

        Use this class to create applications which serve a standard
        compliant ReSTful API. For example, you may have an API which manage
        monitoring data, there can be multiple implementations of the API
        using different technologies. One can use Mongodb, the other can use
        Cassandra. To make the configuration of the application very easy,
        each implementation provides a class with set of methods decorated
        by class Restify, the application can simply using single entry
        configuration to load different implementations.

        For example::

            class ExampleResource(object):
                @Restify(path='/path1/', method='post')
                def func1(self, req, res):
                    pass

                @Restify(path='/path2/{id}/key/', method='get')
                def func2(self, req, res, id):
                    pass

                def func3(self, req, res, id):
                    pass

        With the above class, the following code will add the class method
        func1, func2 to handle post and get requests respectively, method
        func3 won't be added to the routes.::

            app.add_route(None, ExampleResource())

        Args:
            uri_template (url pattern): the url pattern which a client will
                post a request against. If none, ResourceAPI will
                automatically look up the decorated methods.
            resource (instance): Object which represents an HTTP/REST
                "resource". Falcon will pass requests to various decorated
                methods to handle http requests.
        """
        if not resource:
            raise Exception('Not a valid resource')

        path_maps = {}
        try:
            if uri_template:
                super(ResourceAPI, self).add_route(uri_template, resource)
            else:
                for attr in dir(resource):
                    method = getattr(resource, attr)
                    if callable(method) and hasattr(method,
                                                    RESOURCE_METHOD_FLAG):
                        flag = getattr(method, RESOURCE_METHOD_FLAG)
                        map = path_maps.get(flag.path)
                        if not map:
                            uri_fields, template = (
                                api_helpers.compile_uri_template(flag.path))
                            map = (template, {})
                            path_maps[flag.path] = map

                        new_method = api_helpers._wrap_with_hooks(
                            self._before, self._after, method)
                        map[1][flag.method] = new_method

                for item in path_maps:
                    self._routes.insert(0, (path_maps[item][0],
                                            path_maps[item][1]))
        except Exception:
            LOG.exception('Error occurred while adding the resource')
        LOG.debug(self._routes)
Пример #6
0
    def add_route(self, uri_template, resource):
        """Associates uri patterns with resource methods.

        A resource is an instance of a class that defines various methods
        to handle http requests.

        Use this class to create applications which serve a standard
        compliant ReSTful API. For example, you may have an API which manage
        monitoring data, there can be multiple implementations of the API
        using different technologies. One can use Mongodb, the other can use
        Cassandra. To make the configuration of the application very easy,
        each implementation provides a class with set of methods decorated
        by class Restify, the application can simply using single entry
        configuration to load different implementations.

        For example::

            class ExampleResource(object):
                @Restify(path='/path1/', method='post')
                def func1(self, req, res):
                    pass

                @Restify(path='/path2/{id}/key/', method='get')
                def func2(self, req, res, id):
                    pass

                def func3(self, req, res, id):
                    pass

        With the above class, the following code will add the class method
        func1, func2 to handle post and get requests respectively, method
        func3 won't be added to the routes.::

            app.add_route(None, ExampleResource())

        Args:
            uri_template (url pattern): the url pattern which a client will
                post a request against. If none, ResourceAPI will
                automatically look up the decorated methods.
            resource (instance): Object which represents an HTTP/REST
                "resource". Falcon will pass requests to various decorated
                methods to handle http requests.
        """
        if not resource:
            raise Exception('Not a valid resource')

        path_maps = {}
        try:
            if uri_template:
                super(ResourceAPI, self).add_route(uri_template, resource)
            else:
                for attr in dir(resource):
                    method = getattr(resource, attr)
                    if callable(method) and hasattr(method,
                                                    RESOURCE_METHOD_FLAG):
                        flag = getattr(method, RESOURCE_METHOD_FLAG)
                        map = path_maps.get(flag.path)
                        if not map:
                            uri_fields, template = (
                                api_helpers.compile_uri_template(flag.path))
                            map = (template, {})
                            path_maps[flag.path] = map

                        new_method = api_helpers._wrap_with_hooks(
                            self._before, self._after, method)
                        map[1][flag.method] = new_method

                for item in path_maps:
                    self._routes.insert(
                        0, (path_maps[item][0], path_maps[item][1]))
        except Exception:
            LOG.exception('Error occurred while adding the resource')
        LOG.debug(self._routes)