|
| 1 | +# SPDX-FileCopyrightText: 2025-present deepset GmbH <info@deepset.ai> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +"""Resource implementation for search history API.""" |
| 6 | + |
| 7 | +from typing import TYPE_CHECKING |
| 8 | +from urllib.parse import quote |
| 9 | + |
| 10 | +from deepset_mcp.api.search_history.models import SearchHistoryEntry |
| 11 | +from deepset_mcp.api.search_history.protocols import SearchHistoryResourceProtocol |
| 12 | +from deepset_mcp.api.shared_models import PaginatedResponse |
| 13 | +from deepset_mcp.api.transport import raise_for_status |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from deepset_mcp.api.protocols import AsyncClientProtocol |
| 17 | + |
| 18 | + |
| 19 | +class SearchHistoryResource(SearchHistoryResourceProtocol): |
| 20 | + """Manages interactions with the deepset search history API.""" |
| 21 | + |
| 22 | + def __init__(self, client: "AsyncClientProtocol", workspace: str) -> None: |
| 23 | + """Initialize the search history resource. |
| 24 | +
|
| 25 | + :param client: The async REST client. |
| 26 | + :param workspace: The workspace to use. |
| 27 | + """ |
| 28 | + self._client = client |
| 29 | + self._workspace = workspace |
| 30 | + |
| 31 | + def _base_path(self) -> str: |
| 32 | + return f"v1/workspaces/{quote(self._workspace, safe='')}/search_history" |
| 33 | + |
| 34 | + def _pipeline_path(self, pipeline_name: str) -> str: |
| 35 | + return ( |
| 36 | + f"v1/workspaces/{quote(self._workspace, safe='')}/pipelines/{quote(pipeline_name, safe='')}/search_history" |
| 37 | + ) |
| 38 | + |
| 39 | + async def list(self, limit: int = 10, after: str | None = None) -> PaginatedResponse[SearchHistoryEntry]: |
| 40 | + """List search history entries in the workspace. |
| 41 | +
|
| 42 | + :param limit: Maximum number of entries to return per page. |
| 43 | + :param after: Cursor to fetch the next page of results. |
| 44 | + :returns: Paginated response of search history entries. |
| 45 | + """ |
| 46 | + params: dict[str, str | int] = {"limit": limit} |
| 47 | + if after is not None: |
| 48 | + params["after"] = after |
| 49 | + |
| 50 | + resp = await self._client.request( |
| 51 | + endpoint=self._base_path(), |
| 52 | + method="GET", |
| 53 | + params=params, |
| 54 | + timeout=70.0, |
| 55 | + ) |
| 56 | + |
| 57 | + raise_for_status(resp) |
| 58 | + |
| 59 | + if resp.json is None: |
| 60 | + return PaginatedResponse( |
| 61 | + data=[], |
| 62 | + has_more=False, |
| 63 | + total=0, |
| 64 | + next_cursor=None, |
| 65 | + ) |
| 66 | + |
| 67 | + # API may return paginated shape: { "data": [...], "has_more": bool, "total": int } |
| 68 | + data = resp.json if isinstance(resp.json, dict) else {"data": resp.json} |
| 69 | + items = data.get("data", []) |
| 70 | + if not isinstance(items, list): |
| 71 | + items = [] |
| 72 | + |
| 73 | + return PaginatedResponse[SearchHistoryEntry].create_with_cursor_field( |
| 74 | + { |
| 75 | + "data": items, |
| 76 | + "has_more": data.get("has_more", False), |
| 77 | + "total": data.get("total"), |
| 78 | + }, |
| 79 | + "created_at", |
| 80 | + ) |
| 81 | + |
| 82 | + async def list_pipeline( |
| 83 | + self, pipeline_name: str, limit: int = 10, after: str | None = None |
| 84 | + ) -> PaginatedResponse[SearchHistoryEntry]: |
| 85 | + """List search history entries for a specific pipeline with pagination. |
| 86 | +
|
| 87 | + Uses the pipeline search history archive endpoint (full history, most recent first). |
| 88 | +
|
| 89 | + :param pipeline_name: Name of the pipeline. |
| 90 | + :param limit: Maximum number of entries to return per page. |
| 91 | + :param after: Cursor to fetch the next page of results. |
| 92 | + :returns: Paginated response of search history entries. |
| 93 | + """ |
| 94 | + params: dict[str, str | int] = {"limit": limit} |
| 95 | + if after is not None: |
| 96 | + params["after"] = after |
| 97 | + |
| 98 | + resp = await self._client.request( |
| 99 | + endpoint=f"{self._pipeline_path(pipeline_name)}_archive", |
| 100 | + method="GET", |
| 101 | + params=params, |
| 102 | + timeout=70.0, |
| 103 | + ) |
| 104 | + |
| 105 | + raise_for_status(resp) |
| 106 | + |
| 107 | + if resp.json is None: |
| 108 | + return PaginatedResponse( |
| 109 | + data=[], |
| 110 | + has_more=False, |
| 111 | + total=0, |
| 112 | + next_cursor=None, |
| 113 | + ) |
| 114 | + |
| 115 | + data = resp.json if isinstance(resp.json, dict) else {"data": resp.json} |
| 116 | + items = data.get("data", []) |
| 117 | + if not isinstance(items, list): |
| 118 | + items = [] |
| 119 | + |
| 120 | + return PaginatedResponse[SearchHistoryEntry].create_with_cursor_field( |
| 121 | + { |
| 122 | + "data": items, |
| 123 | + "has_more": data.get("has_more", False), |
| 124 | + "total": data.get("total"), |
| 125 | + }, |
| 126 | + "created_at", |
| 127 | + ) |
0 commit comments