Dispatch

The core of the command dispatcher.

Attention

You do not have to add this middleware yourself, the Builder will add it for you.

Usage

The dispatch middleware actually handles the dispatching of requests to their handlers. It should be the last middleware in the chain.

Registration

You do not have to add this middleware yourself, the Builder will add it for you.

Instead you setup the middleware using with_locator() and with_factory() to provide its dependencies.

Context

We add a Dispatch context instance for each handler executed. It will contain the result of the function call and the name of the handler.

result = message[banshee.Dispatch].result
name = message[banshee.Dispatch].name

Reference

class banshee.Dispatch(name, result)

Bases: object

Dispatch context.

Marks a message as having had its request processed by a specific handler and contains the result of that execution.

Parameters:
  • name (str) – handler name

  • result (Any | None) – result of handler

name: str

handler name

result: Any | None

result of handler

class banshee.DispatchMiddleware(locator, factory)

Bases: Middleware

Dispatch middleware.

Implements the command dispatcher design pattern to route requests to their configured handlers.

Uses the pre-configured HandlerLocator to retrieve a all associated HandlerReference instances, before passing the references one by one to a HandlerFactory in order to get and Handler instance to call.

A Dispatch context will be added to the message for each successful handler.

Parameters:
  • locator (HandlerLocator) – locator to lookup associated handlers for a message

  • factory (HandlerFactory) – factory to instantiate a concrete handler from a reference

async __call__(message, handle)

Handle message.

Dispatch the request to all associated handlers and store the results.

Parameters:
Returns:

processed message

Raises:

banshee.errors.DispatchError – when one or more handlers fails

Return type:

Message[T]

exception banshee.DispatchError(message, exceptions)

Dispatch error.

There was an error while handling a request.