Routing

Module Documentation

class doctor.routing.HTTPMethod(method, logic, allowed_exceptions=None, title=None)[source]

Bases: object

Represents and HTTP method and it’s configuration.

When instantiated the logic attribute will have 3 attributes added to it:
  • _doctor_allowed_exceptions - A list of excpetions that are allowed to be re-reaised if encountered during a request.
  • _doctor_params - A Params instance.
  • _doctor_signature - The parsed function Signature.
  • _doctor_title - The title that should be used in api documentation.
Parameters:
  • method (str) – The HTTP method. One of: (delete, get, post, put).
  • logic (Callable) – The logic function to be called for the http method.
  • allowed_exceptions (Optional[List[~T]]) – If specified, these exception classes will be re-raised instead of turning them into 500 errors.
  • title (Optional[str]) – An optional title for the http method. This will be used when generating api documentation.
class doctor.routing.Route(route, methods, heading=None, base_handler_class=None, handler_name=None)[source]

Bases: object

Represents a route.

Parameters:
  • route (str) – The route path, e.g. r’^/foo/<int:foo_id>/?$’
  • methods (Tuple[HTTPMethod]) – A tuple of defined HTTPMethods for the route.
  • heading (Optional[str]) – An optional heading that this route should be grouped under in the api documentation.
  • base_handler_class – The base handler class to use.
  • handler_name (Optional[str]) – The name that should be given to the handler class.
doctor.routing.create_http_method(logic, http_method, handle_http)[source]

Create a handler method to be used in a handler class.

Parameters:
  • logic (callable) – The underlying function to execute with the parsed and validated parameters.
  • http_method (str) – HTTP method this will handle.
  • handle_http (Callable) – The HTTP handler function that should be used to wrap the logic functions.
Return type:

Callable

Returns:

A handler function.

doctor.routing.create_routes(routes, handle_http, default_base_handler_class)[source]

Creates handler routes from the provided routes.

Parameters:
  • routes (Tuple[HTTPMethod]) – A tuple containing the route and another tuple with all http methods allowed for the route.
  • handle_http (Callable) – The HTTP handler function that should be used to wrap the logic functions.
  • default_base_handler_class (Any) – The default base handler class that should be used.
Return type:

List[Tuple[str, Any]]

Returns:

A list of tuples containing the route and generated handler.

doctor.routing.delete(func, allowed_exceptions=None, title=None)[source]

Returns a HTTPMethod instance to create a DELETE route.

See:HTTPMethod
Return type:HTTPMethod
doctor.routing.get(func, allowed_exceptions=None, title=None)[source]

Returns a HTTPMethod instance to create a GET route.

See:HTTPMethod
Return type:HTTPMethod
doctor.routing.get_handler_name(route, logic)[source]

Gets the handler name.

Parameters:
  • route (Route) – A Route instance.
  • logic (Callable) – The logic function.
Return type:

str

Returns:

A handler class name.

doctor.routing.post(func, allowed_exceptions=None, title=None)[source]

Returns a HTTPMethod instance to create a POST route.

See:HTTPMethod
Return type:HTTPMethod
doctor.routing.put(func, allowed_exceptions=None, title=None)[source]

Returns a HTTPMethod instance to create a PUT route.

See:HTTPMethod
Return type:HTTPMethod