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))
def set_default_route(self, default_resource): """Route all the unrouted requests to a default resource Args: default_resource: Object which works like an HTTP/REST resource. Falcon will pass "GET" requests to on_get, "PUT" requests to on_put, etc. If you want to exclude some HTTP method from the default routing, just simply don't define the corresponding request handlers. """ self._default_route = helpers.create_http_method_map( default_resource, set(), self._before, self._after)
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))
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))
def set_default_route(self, default_resource): """DEPRECATED: Route all the unrouted requests to a default resource. NOTE: If a default route is defined, all sinks are ignored. Args: default_resource (instance): Object which works like a RESTful resource. Falcon will pass "GET" requests to on_get, "PUT" requests to on_put, etc. If you want to exclude any HTTP methods from the routing, simply do not define the corresponding request handlers, and Falcon will respond with "405 Method Not Allowed" when those methods are requested. """ self._default_route = helpers.create_http_method_map( default_resource, set(), self._before, self._after)