11from typing import Any
22
3- from deepset_mcp .api .client import AsyncClientProtocol
3+ from deepset_mcp .api .exceptions import UnexpectedAPIError
44from 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 :
@@ -15,48 +17,44 @@ def __init__(
1517 """Initializes a PipelineResource instance."""
1618 self ._client = client
1719 self ._workspace = workspace
18-
20+
1921 async def validate (self , yaml_config : str ) -> PipelineValidationResult :
2022 """
2123 Validate a pipeline's YAML configuration against the API.
22-
24+
2325 Args:
2426 yaml_config: The YAML configuration string to validate
25-
27+
2628 Returns:
2729 PipelineValidationResult containing validation status and any errors
28-
30+
2931 Raises:
3032 ValueError: If the YAML is not valid (422 error) or contains syntax errors
3133 """
32- data = {
33- "deepset_cloud_version" : "v2" ,
34- "query_yaml" : yaml_config
35- }
36-
34+ data = {"query_yaml" : yaml_config }
35+
3736 resp = await self ._client .request (
3837 endpoint = f"v1/workspaces/{ self ._workspace } /pipeline_validations" ,
3938 method = "POST" ,
4039 data = data ,
4140 )
42-
41+
4342 # If successful (status 200), the YAML is valid
4443 if resp .success :
4544 return PipelineValidationResult (valid = True )
46-
45+
4746 # If 400 error, we have validation errors to process
48- if resp .status_code == 400 and resp .json is not None and "errors" in resp .json :
49- errors = [ValidationError (code = error ["code" ], message = error ["message" ])
50- for error in resp .json ["errors" ]]
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+
5155 return PipelineValidationResult (valid = False , errors = errors )
52-
53- # For other errors, return as an exception
54- # (like 422 for invalid YAML syntax)
55- if not resp .success :
56- if resp .json and "detail" in resp .json :
57- raise ValueError (f"Pipeline validation failed: { resp .json ['detail' ]} " )
58- else :
59- raise ValueError (f"Pipeline validation failed with status { resp .status_code } " )
56+
57+ raise UnexpectedAPIError (status_code = resp .status_code , message = resp .text , detail = resp .json )
6058
6159 async def list (
6260 self ,
@@ -81,6 +79,8 @@ async def list(
8179 method = "GET" ,
8280 )
8381
82+ raise_for_status (resp )
83+
8484 response = resp .json
8585
8686 if response is not None :
@@ -93,13 +93,17 @@ async def list(
9393 async def get (self , pipeline_name : str , include_yaml : bool = True ) -> DeepsetPipeline :
9494 """Fetch a single pipeline by its name."""
9595 resp = await self ._client .request (endpoint = f"v1/workspaces/{ self ._workspace } /pipelines/{ pipeline_name } " )
96+ raise_for_status (resp )
97+
9698 pipeline = DeepsetPipeline .model_validate (resp .json )
9799
98100 if include_yaml :
99101 yaml_response = await self ._client .request (
100102 endpoint = f"v1/workspaces/{ self ._workspace } /pipelines/{ pipeline_name } /yaml"
101103 )
102104
105+ raise_for_status (yaml_response )
106+
103107 if yaml_response .json is not None :
104108 pipeline .yaml_config = yaml_response .json ["query_yaml" ]
105109
@@ -108,12 +112,14 @@ async def get(self, pipeline_name: str, include_yaml: bool = True) -> DeepsetPip
108112 async def create (self , name : str , yaml_config : str ) -> None :
109113 """Create a new pipeline with a name and YAML config."""
110114 data = {"name" : name , "query_yaml" : yaml_config }
111- await self ._client .request (
115+ resp = await self ._client .request (
112116 endpoint = f"v1/workspaces/{ self ._workspace } /pipelines" ,
113117 method = "POST" ,
114118 data = data ,
115119 )
116120
121+ raise_for_status (resp )
122+
117123 async def update (
118124 self ,
119125 pipeline_name : str ,
@@ -123,23 +129,21 @@ async def update(
123129 """Update name and/or YAML config of an existing pipeline."""
124130 # Handle name update first if any
125131 if updated_pipeline_name is not None :
126- await self ._client .request (
132+ name_resp = await self ._client .request (
127133 endpoint = f"v1/workspaces/{ self ._workspace } /pipelines/{ pipeline_name } " ,
128134 method = "PATCH" ,
129135 data = {"name" : updated_pipeline_name },
130136 )
131137
138+ raise_for_status (name_resp )
139+
132140 pipeline_name = updated_pipeline_name
133141
134142 if yaml_config is not None :
135- await self ._client .request (
143+ yaml_resp = await self ._client .request (
136144 endpoint = f"v1/workspaces/{ self ._workspace } /pipelines/{ pipeline_name } /yaml" ,
137145 method = "PUT" ,
138146 data = {"query_yaml" : yaml_config },
139147 )
140148
141-
142- # async with AsyncDeepsetClient() as client:
143- # await client.pipelines("default").list()
144- # await client.pipelines("default").get("hello")
145- # await client.pipelines("default").update(yaml_config="blabla")
149+ raise_for_status (yaml_resp )
0 commit comments