gidgetlab.routing — A router for webhook events

When a single web service is used to perform multiple actions based on a single webhook event, it is easier to do those multiple steps in some sort of routing mechanism to make sure the right objects are called is provided. This module is meant to provide such a router for gidgetlab.sansio.Event instances. This allows for individual async functions to be written per event type to help keep logic separated and focused instead of having to differentiate between different events manually in user code.

class gidgetlab.routing.Router(*other_routers)

An object to route a gidgetlab.sansio.Event instance to appropriate registered asynchronous callbacks.

The initializer for this class takes an arbitrary number of other routers to help build a single router from sub-routers. Typically this is used when each semantic set of features has a router and are then used to construct a server-wide router.

Each callback registered with this class is expected to be awaitable and to accept at least a single Event instance:

async def callback(event, *args, **kwargs):
    ...
add(func, event_type, **object_attribute)

Add an asynchronous callback for an event.

The event_type argument corresponds to the gidgetlab.sansio.Event.event attribute of the event that the callback is interested in. The arbitrary keyword arguments is used as a key/value pair to compare against what is provided in gidgetlab.sansio.Event.object_attributes. Only 0 or 1 keyword-only arguments may be provided, otherwise TypeError is raised.

For example, to register a callback for any opened issues, you would call:

async def callback(event):
    ...

router.add(callback, "Issue Hook", action="open")
Return type:

None

register(event_type, **object_attribute)

A decorator that calls add() on the decorated function.

router = gidgetlab.routing.Router()

@router.register("Issue Hook", action="open")
async def callback(event):
    ...
Return type:

Callable[[Callable[..., Awaitable[None]]], Callable[..., Awaitable[None]]]

async dispatch(event, *args, **kwargs)

Call the appropriate asynchronous callbacks for the event. The provided event and any other arguments will be passed down to the callback unmodified.

Return type:

None