glados.router module — GLaDOS 0.0.1.dev14 documentation

# glados.router module

class `glados.router.` `GladosRoute`(route_type: glados.route_type.RouteType, route: str, function: Callable)

  Bases: `object`

  Represents a single route

class `glados.router.` `GladosRouter`(**kwargs)

  Bases: `object`

  `add_route`(plugin: GladosPlugin, route: glados.router.GladosRoute) → NoReturn

    Add a route to the router

    Parameters

      - plugin (GladosPlugin) – the plugin the route belongs to

      - route (GladosRoute) – the route to be added

    Raises

      KeyError – a route with the same type and same name already exists

  `add_routes`(plugin: GladosPlugin)

    Add multiple routes to the router.

    Parameters

      routes (GladosPlugin) – the plugin to add routes from

  `exec_route`(request: glados.request.GladosRequest)

    Execute a route function directly

    Parameters

      request (GladosRequest) – the GLaDOS request

    Returns

    Return type

      the data returned by the plugin

    Examples

        >>> def mock_function(request: GladosRequest):
        ...     print(f"Mock Function: {request.params.message}")
        ...     return True
        >>> router = GladosRouter()
        >>> route = GladosRoute(RouteType.SendMessage, "send_mock", mock_function)
        >>> router.add_route(route)
        >>> request = GladosRequest(RouteType.SendMessage, "send_mock", message="Hello World!")
        >>> successful = router.exec_route(request)
        Mock Function: Hello World!
        >>> print(successful)
        True

        >>> def mock_function(request: GladosRequest):
        ...     print(f"Mock Function: {request.params.message}")
        ...     return True
        >>> router = GladosRouter()
        >>> route = GladosRoute(RouteType.SendMessage, "send_mock", mock_function)
        >>> router.add_route(route)
        >>> request = GladosRequest(RouteType.SendMessage, "send_mock_fail", message="Hello World!")
        >>> successful = router.exec_route(request)
        >>> print(successful)
        False

  `get_route`(route_type: glados.route_type.RouteType, route: str) → Optional[glados.router.GladosRoute]

    Get a GladosRoute object for the requested route.

    Parameters

      - route_type (RouteType) – the type of route to get

      - route (str) – the route to get

    Returns

      return the request route

    Return type

      GladosRoute

    Raises

      GladosRouteNotFoundError – the requested route is not found

  `route_function`(route_type: glados.route_type.RouteType, route: str) → Callable

    Return only the callable function for the requested GladosRoute.

    Parameters

      - route_type (RouteType) – the type of route to get

      - route (str) – the route to get

    Returns

      return the requested routes callable function

    Return type

      Callable