|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from deepset_mcp.api.client import AsyncClientProtocol |
| 4 | +from deepset_mcp.api.pipeline.models import DeepsetPipeline |
| 5 | + |
| 6 | + |
| 7 | +class PipelineResource: |
| 8 | + """Manages interactions with the deepset pipeline API.""" |
| 9 | + |
| 10 | + def __init__( |
| 11 | + self, |
| 12 | + client: AsyncClientProtocol, |
| 13 | + workspace: str, |
| 14 | + ) -> None: |
| 15 | + """Initializes a PipelineResource instance.""" |
| 16 | + self._client = client |
| 17 | + self._workspace = workspace |
| 18 | + |
| 19 | + async def list( |
| 20 | + self, |
| 21 | + page_number: int = 1, |
| 22 | + limit: int = 10, |
| 23 | + ) -> list[DeepsetPipeline]: |
| 24 | + """ |
| 25 | + Retrieve pipeline in the configured workspace with optional pagination. |
| 26 | +
|
| 27 | + :param page_number: Page number for paging. |
| 28 | + :param limit: Max number of items to return. |
| 29 | + :return: PipelineListResponse containing `data`, `has_more`, and `total`. |
| 30 | + """ |
| 31 | + params: dict[str, Any] = { |
| 32 | + "page_number": page_number, |
| 33 | + "limit": limit, |
| 34 | + } |
| 35 | + |
| 36 | + query = "?" + "&".join(f"{k}={v}" for k, v in params.items()) if params else "" |
| 37 | + resp = await self._client.request( |
| 38 | + endpoint=f"v1/workspaces/{self._workspace}/pipelines{query}", |
| 39 | + method="GET", |
| 40 | + ) |
| 41 | + |
| 42 | + response = resp.json |
| 43 | + |
| 44 | + if response is not None: |
| 45 | + pipelines = [DeepsetPipeline.model_validate(item) for item in response.get("data", [])] |
| 46 | + else: |
| 47 | + pipelines = [] |
| 48 | + |
| 49 | + return pipelines |
| 50 | + |
| 51 | + async def get(self, pipeline_name: str, include_yaml: bool = True) -> DeepsetPipeline: |
| 52 | + """Fetch a single pipeline by its name.""" |
| 53 | + resp = await self._client.request(endpoint=f"v1/workspaces/{self._workspace}/pipelines/{pipeline_name}") |
| 54 | + pipeline = DeepsetPipeline.model_validate(resp.json) |
| 55 | + |
| 56 | + if include_yaml: |
| 57 | + yaml_response = await self._client.request( |
| 58 | + endpoint=f"v1/workspaces/{self._workspace}/pipelines/{pipeline_name}/yaml" |
| 59 | + ) |
| 60 | + |
| 61 | + if yaml_response.json is not None: |
| 62 | + pipeline.yaml_config = yaml_response.json["query_yaml"] |
| 63 | + |
| 64 | + return pipeline |
| 65 | + |
| 66 | + async def create(self, name: str, yaml_config: str) -> None: |
| 67 | + """Create a new pipeline with a name and YAML config.""" |
| 68 | + data = {"name": name, "query_yaml": yaml_config} |
| 69 | + await self._client.request( |
| 70 | + endpoint=f"v1/workspaces/{self._workspace}/pipelines", |
| 71 | + method="POST", |
| 72 | + data=data, |
| 73 | + ) |
| 74 | + |
| 75 | + async def update( |
| 76 | + self, |
| 77 | + pipeline_name: str, |
| 78 | + updated_pipeline_name: str | None = None, |
| 79 | + yaml_config: str | None = None, |
| 80 | + ) -> None: |
| 81 | + """Update name and/or YAML config of an existing pipeline.""" |
| 82 | + # Handle name update first if any |
| 83 | + if updated_pipeline_name is not None: |
| 84 | + await self._client.request( |
| 85 | + endpoint=f"v1/workspaces/{self._workspace}/pipelines/{pipeline_name}", |
| 86 | + method="PATCH", |
| 87 | + data={"name": updated_pipeline_name}, |
| 88 | + ) |
| 89 | + |
| 90 | + pipeline_name = updated_pipeline_name |
| 91 | + |
| 92 | + if yaml_config is not None: |
| 93 | + await self._client.request( |
| 94 | + endpoint=f"v1/workspaces/{self._workspace}/pipelines/{pipeline_name}/yaml", |
| 95 | + method="PUT", |
| 96 | + data={"query_yaml": yaml_config}, |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +# async with AsyncDeepsetClient() as client: |
| 101 | +# await client.pipelines("default").list() |
| 102 | +# await client.pipelines("default").get("hello") |
| 103 | +# await client.pipelines("default").update(yaml_config="blabla") |
0 commit comments