kyoukai.route

Routes are wrapped function objects that are called upon a HTTP request.

Classes

Route(function, *, …) A route object is a wrapped function.
class kyoukai.route.Route(function, *, reverse_hooks: bool = False, should_invoke_hooks: bool = True, do_argument_checking: bool = True, endpoint: str = None)[source]

Bases: object

A route object is a wrapped function. They invoke this function when invoked on routing and calling.

Parameters:
  • function – The underlying callable. This can be a function, or any other callable.
  • reverse_hooks (bool) – If the request hooks should be reversed for this request (i.e child to parent.)
  • should_invoke_hooks (bool) – If request hooks should be invoked. This is automatically False for error handlers.
  • do_argument_checking (bool) – If argument type and name checking is enabled for this route.
  • endpoint (Optional[str]) – The custom endpoint for this route.
do_argument_checking = None

If this route should do argument checking.

bp = None

The Blueprint this route is associated with.

routes = None

A list of tuples (url, methods) for this Route.

endpoint = None

The custom endpoint for this route. Could be None.

hooks = None

Our own specific hooks.

add_path(url, methods=('GET', 'HEAD'))[source]

Adds a path to the current set of paths for this route.

Parameters:
  • url (str) – The routing URL to add.
  • methods (Sequence[str]) – An iterable of methods to use for this path.

The URL and methods will be added as a pair.

get_submount()[source]
Return type:Submount
Returns:A submount that represents this route.

New in version 2.2.0.

Changed in version 2.x.x: Changed from getting a list of rules to a single submount object.

get_endpoint_name(bp=None)[source]

Gets the endpoint name for this route.

Parameters:bp – The Blueprint to use for name calculation.
Return type:str
Returns:The endpoint that can be used.
coroutine invoke_function(self, ctx, pre_hooks, post_hooks, params)[source]

Invokes the underlying callable. This is for use in chaining routes.

Parameters:
  • ctx – The HTTPRequestContext to use for this route.
  • pre_hooks (list) – A list of hooks to call before the route is invoked.
  • post_hooks (list) – A list of hooks to call after the route is invoked.
  • params – The parameters to pass to the function.
Returns:

The result of the invoked function.

check_route_args(params=None)[source]

Checks the arguments for a route.

Parameters:params (Optional[dict]) – The parameters passed in, as a dict.
Raises:TypeError – If the arguments passed in were not correct.
add_hook(type_, hook)[source]

Adds a hook to the current Route.

Parameters:
  • type (str) – The type of hook to add (currently “pre” or “post”).
  • hook – The callable function to add as a hook.
get_hooks(type_)[source]

Gets the hooks for the current Route for the type.

Parameters:type (str) – The type to get.
Returns:A list of callables.
before_request(func)[source]

Convenience decorator to add a pre-request hook.

after_request(func)[source]

Convenience decorator to add a post-request hook.

coroutine invoke(self, ctx, args=(), params=None)[source]

Invokes a route. This will run the underlying function.

Parameters:
Return type:

Response

Returns:

The result of the route’s function.