Routing

The falcon.routing module contains utilities used internally by falcon.API to route requests. They are exposed here for use by classes that inherit from falcon.API to implement custom routing logic, and in anticipation of a future version of the framework that will afford customization of routing via composition in lieu of inheritance.

falcon.routing.compile_uri_template(template)[source]

Compile the given URI template string into a pattern matcher.

This function currently only recognizes Level 1 URI templates, and only for the path portion of the URI.

See also: http://tools.ietf.org/html/rfc6570

Parameters:template – A Level 1 URI template. Method responders must accept, as arguments, all fields specified in the template (default ‘/’). Note that field names are restricted to ASCII a-z, A-Z, and the underscore ‘_’.
Returns:(template_field_names, template_regex)
Return type:tuple
falcon.routing.create_http_method_map(resource, uri_fields, before, after)[source]

Maps HTTP methods (e.g., ‘GET’, ‘POST’) to methods of a resource object.

Parameters:
  • resource – An object with responder methods, following the naming convention on_*, that correspond to each method the resource supports. For example, if a resource supports GET and POST, it should define on_get(self, req, resp) and on_post(self, req, resp).
  • uri_fields – A set of field names from the route’s URI template that a responder must support in order to avoid “method not allowed”.
  • before – An action hook or list of hooks to be called before each on_* responder defined by the resource.
  • after – An action hook or list of hooks to be called after each on_* responder defined by the resource.
Returns:

A mapping of HTTP methods to responders.

Return type:

dict