glados.router module — GLaDOS 0.0.1-alpha1 documentation

# glados.router module

class `glados.router.` `GladosRoute`(route_type, route, function)

  Bases: `object`

  Represents a single route

  Parameters

    - route_type (`RouteType`) –

    - route (`str`) –

    - function (`Callable`) –

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

  Bases: `object`

  `add_route`(plugin, route)

    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

    Return type

      `NoReturn`

  `add_routes`(plugin)

    Add multiple routes to the router.

    Parameters

      - routes (GladosPlugin) – the plugin to add routes from

      - plugin (GladosPlugin) –

  `exec_route`(request)

    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, route)

    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, route)

    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