11from typing import Any
22
3- from deepset_mcp .api .client import AsyncClientProtocol
4- from deepset_mcp .api .pipeline .models import DeepsetPipeline
3+ from deepset_mcp .api .exceptions import UnexpectedAPIError
4+ from deepset_mcp .api .pipeline .models import DeepsetPipeline , PipelineValidationResult , ValidationError
5+ from deepset_mcp .api .protocols import AsyncClientProtocol
6+ from deepset_mcp .api .transport import raise_for_status
57
68
79class PipelineResource :
@@ -16,6 +18,44 @@ def __init__(
1618 self ._client = client
1719 self ._workspace = workspace
1820
21+ async def validate (self , yaml_config : str ) -> PipelineValidationResult :
22+ """
23+ Validate a pipeline's YAML configuration against the API.
24+
25+ Args:
26+ yaml_config: The YAML configuration string to validate
27+
28+ Returns:
29+ PipelineValidationResult containing validation status and any errors
30+
31+ Raises:
32+ ValueError: If the YAML is not valid (422 error) or contains syntax errors
33+ """
34+ data = {"query_yaml" : yaml_config }
35+
36+ resp = await self ._client .request (
37+ endpoint = f"v1/workspaces/{ self ._workspace } /pipeline_validations" ,
38+ method = "POST" ,
39+ data = data ,
40+ )
41+
42+ # If successful (status 200), the YAML is valid
43+ if resp .success :
44+ return PipelineValidationResult (valid = True )
45+
46+ # If 400 error, we have validation errors to process
47+ if resp .status_code == 400 and resp .json is not None and "details" in resp .json :
48+ errors = [ValidationError (code = error ["code" ], message = error ["message" ]) for error in resp .json ["details" ]]
49+
50+ return PipelineValidationResult (valid = False , errors = errors )
51+
52+ if resp .status_code == 422 :
53+ errors = [ValidationError (code = "YAML_ERROR" , message = "Syntax error in YAML" )]
54+
55+ return PipelineValidationResult (valid = False , errors = errors )
56+
57+ raise UnexpectedAPIError (status_code = resp .status_code , message = resp .text , detail = resp .json )
58+
1959 async def list (
2060 self ,
2161 page_number : int = 1 ,
@@ -39,6 +79,8 @@ async def list(
3979 method = "GET" ,
4080 )
4181
82+ raise_for_status (resp )
83+
4284 response = resp .json
4385
4486 if response is not None :
@@ -51,13 +93,17 @@ async def list(
5193 async def get (self , pipeline_name : str , include_yaml : bool = True ) -> DeepsetPipeline :
5294 """Fetch a single pipeline by its name."""
5395 resp = await self ._client .request (endpoint = f"v1/workspaces/{ self ._workspace } /pipelines/{ pipeline_name } " )
96+ raise_for_status (resp )
97+
5498 pipeline = DeepsetPipeline .model_validate (resp .json )
5599
56100 if include_yaml :
57101 yaml_response = await self ._client .request (
58102 endpoint = f"v1/workspaces/{ self ._workspace } /pipelines/{ pipeline_name } /yaml"
59103 )
60104
105+ raise_for_status (yaml_response )
106+
61107 if yaml_response .json is not None :
62108 pipeline .yaml_config = yaml_response .json ["query_yaml" ]
63109
@@ -66,12 +112,14 @@ async def get(self, pipeline_name: str, include_yaml: bool = True) -> DeepsetPip
66112 async def create (self , name : str , yaml_config : str ) -> None :
67113 """Create a new pipeline with a name and YAML config."""
68114 data = {"name" : name , "query_yaml" : yaml_config }
69- await self ._client .request (
115+ resp = await self ._client .request (
70116 endpoint = f"v1/workspaces/{ self ._workspace } /pipelines" ,
71117 method = "POST" ,
72118 data = data ,
73119 )
74120
121+ raise_for_status (resp )
122+
75123 async def update (
76124 self ,
77125 pipeline_name : str ,
@@ -81,23 +129,21 @@ async def update(
81129 """Update name and/or YAML config of an existing pipeline."""
82130 # Handle name update first if any
83131 if updated_pipeline_name is not None :
84- await self ._client .request (
132+ name_resp = await self ._client .request (
85133 endpoint = f"v1/workspaces/{ self ._workspace } /pipelines/{ pipeline_name } " ,
86134 method = "PATCH" ,
87135 data = {"name" : updated_pipeline_name },
88136 )
89137
138+ raise_for_status (name_resp )
139+
90140 pipeline_name = updated_pipeline_name
91141
92142 if yaml_config is not None :
93- await self ._client .request (
143+ yaml_resp = await self ._client .request (
94144 endpoint = f"v1/workspaces/{ self ._workspace } /pipelines/{ pipeline_name } /yaml" ,
95145 method = "PUT" ,
96146 data = {"query_yaml" : yaml_config },
97147 )
98148
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")
149+ raise_for_status (yaml_resp )
0 commit comments