Skip to content

datarobot_genai.drmcpbase.routes.helpers

helpers

Shared query parsing and filtering helpers for paginated REST list routes.

parse_pagination

parse_pagination(request: Request, *, default_limit: int = 100) -> tuple[int, int]

Read limit and offset from the query string.

Non-integer or negative values fall back to the defaults, so a malformed query never 500s the route.

Source code in datarobot_genai/drmcpbase/routes/helpers.py
def parse_pagination(request: Request, *, default_limit: int = 100) -> tuple[int, int]:
    """Read ``limit`` and ``offset`` from the query string.

    Non-integer or negative values fall back to the defaults, so a malformed query never
    500s the route.
    """

    def _non_negative_int(name: str, default: int) -> int:
        raw = request.query_params.get(name)
        if raw is None:
            return default
        try:
            value = int(raw)
        except (TypeError, ValueError):
            return default
        return value if value >= 0 else default

    return _non_negative_int("limit", default_limit), _non_negative_int("offset", 0)

parse_list_filters

parse_list_filters(request: Request) -> tuple[str | None, list[str] | None, list[str] | None]

Read optional name, provider and category filters from the query string.

name is a single exact match. provider and category are multi-valued, mirroring the multi-select checkboxes in the gallery filter panel. Both forms are accepted and combined: - comma-separated: ?category=dr_connectors,dr_web_search - repeated params: ?category=dr_connectors&category=dr_web_search Blank tokens are dropped; a dimension with no non-blank values is treated as absent.

Source code in datarobot_genai/drmcpbase/routes/helpers.py
def parse_list_filters(
    request: Request,
) -> tuple[str | None, list[str] | None, list[str] | None]:
    """Read optional ``name``, ``provider`` and ``category`` filters from the query string.

    ``name`` is a single exact match. ``provider`` and ``category`` are **multi-valued**,
    mirroring the multi-select checkboxes in the gallery filter panel. Both forms are
    accepted and combined:
      - comma-separated: ``?category=dr_connectors,dr_web_search``
      - repeated params: ``?category=dr_connectors&category=dr_web_search``
    Blank tokens are dropped; a dimension with no non-blank values is treated as absent.
    """

    def _single(key: str) -> str | None:
        raw = request.query_params.get(key)
        if raw is None:
            return None
        raw = raw.strip()
        return raw or None

    def _multi(key: str) -> list[str] | None:
        values = [
            token.strip()
            for raw in request.query_params.getlist(key)
            for token in raw.split(",")
            if token.strip()
        ]
        return values or None

    return _single("name"), _multi("provider"), _multi("category")

apply_list_filters

apply_list_filters(items: list[dict[str, Any]], name: str | None, providers: list[str] | None, categories: list[str] | None) -> list[dict[str, Any]]

Filter list items by name, provider and/or category.

provider and category are match-any within the dimension; dimensions combine with AND. Unrecognised filter values simply match nothing.

Source code in datarobot_genai/drmcpbase/routes/helpers.py
def apply_list_filters(
    items: list[dict[str, Any]],
    name: str | None,
    providers: list[str] | None,
    categories: list[str] | None,
) -> list[dict[str, Any]]:
    """Filter list *items* by ``name``, ``provider`` and/or ``category``.

    ``provider`` and ``category`` are match-any within the dimension; dimensions combine
    with **AND**. Unrecognised filter values simply match nothing.
    """
    if name is not None:
        items = [item for item in items if item.get("name") == name]
    if providers is not None:
        wanted = set(providers)
        items = [item for item in items if item.get("provider") in wanted]
    if categories is not None:
        wanted = set(categories)
        items = [
            item
            for item in items
            if wanted.intersection(category["name"] for category in item.get("categories") or ())
        ]
    return items

enum_items

enum_items(mapping: dict[Any, str]) -> list[dict[str, str]]

Serialise an ordered value -> label map into [{"value", "label"}] items.

Source code in datarobot_genai/drmcpbase/routes/helpers.py
def enum_items(mapping: dict[Any, str]) -> list[dict[str, str]]:
    """Serialise an ordered ``value -> label`` map into ``[{"value", "label"}]`` items."""
    return [{"value": str(value), "label": label} for value, label in mapping.items()]