11"""Base builder class that all builders extend from."""
22
33from abc import ABC , abstractmethod
4- from typing import Any , Literal
4+ from typing import Literal , Union , overload
55
66from nutrient_dws .builder .staged_builders import (
77 TypedWorkflowResult ,
88)
9+ from nutrient_dws .errors import ValidationError
910from nutrient_dws .http import (
1011 AnalyzeBuildRequestData ,
1112 BuildRequestData ,
1213 NutrientClientOptions ,
1314 RequestConfig ,
15+ is_post_analyse_build_request_config ,
16+ is_post_build_request_config ,
1417 send_request ,
1518)
19+ from nutrient_dws .types .analyze_response import AnalyzeBuildResponse
20+ from nutrient_dws .types .build_response_json import BuildResponseJsonContents
1621
1722
1823class BaseBuilder (ABC ):
@@ -23,21 +28,35 @@ class BaseBuilder(ABC):
2328 def __init__ (self , client_options : NutrientClientOptions ) -> None :
2429 self .client_options = client_options
2530
31+ @overload
32+ async def _send_request (
33+ self , path : Literal ["/build" ], options : BuildRequestData
34+ ) -> Union [BuildResponseJsonContents , bytes , str ]: ...
35+
36+ @overload
37+ async def _send_request (
38+ self , path : Literal ["/analyze_build" ], options : AnalyzeBuildRequestData
39+ ) -> AnalyzeBuildResponse : ...
40+
2641 async def _send_request (
2742 self ,
28- path : Literal ["/build" ] | Literal [ "/analyze_build" ],
43+ path : Literal ["/build" , "/analyze_build" ],
2944 options : BuildRequestData | AnalyzeBuildRequestData ,
30- ) -> Any :
45+ ) -> Union [ BuildResponseJsonContents , bytes , str , AnalyzeBuildResponse ] :
3146 """Sends a request to the API."""
32- config : RequestConfig [BuildRequestData | AnalyzeBuildRequestData ] = {
33- "endpoint" : path ,
34- "method" : "POST" ,
35- "data" : options ,
36- "headers" : None ,
37- }
38-
39- response : Any = await send_request (config , self .client_options )
40- return response ["data" ]
47+ config = RequestConfig (endpoint = path , method = "POST" , data = options , headers = None )
48+
49+ if is_post_build_request_config (config ):
50+ response = await send_request (config , self .client_options )
51+ return response ["data" ]
52+
53+ if is_post_analyse_build_request_config (config ):
54+ analyze_response = await send_request (config , self .client_options )
55+ return analyze_response ["data" ]
56+
57+ raise ValidationError (
58+ "Invalid _send_request args" , {"path" : path , "options" : options }
59+ )
4160
4261 @abstractmethod
4362 async def execute (self ) -> TypedWorkflowResult :
0 commit comments