18
18
from typing import Any
19
19
20
20
import cads_adaptors
21
+ import cads_adaptors .exceptions
21
22
import cads_catalogue
22
23
import fastapi
23
24
@@ -34,6 +35,7 @@ class RequestOrigin(str, enum.Enum):
34
35
def estimate_cost (
35
36
process_id : str = fastapi .Path (...),
36
37
request_origin : RequestOrigin = fastapi .Query ("api" ),
38
+ mandatory_inputs : bool = fastapi .Query (False ),
37
39
execution_content : models .Execute = fastapi .Body (...),
38
40
) -> models .RequestCost :
39
41
"""
@@ -61,15 +63,51 @@ def estimate_cost(
61
63
resource_id = process_id , table = table , session = catalogue_session
62
64
)
63
65
adaptor_properties = adaptors .get_adaptor_properties (dataset )
66
+ request_is_valid = check_request_validity (
67
+ request = request ,
68
+ mandatory_inputs = mandatory_inputs ,
69
+ adaptor_properties = adaptor_properties ,
70
+ )
64
71
costing_info = costing .compute_costing (
65
72
request .get ("inputs" , {}), adaptor_properties , request_origin
66
73
)
67
74
cost = costing .compute_highest_cost_limit_ratio (costing_info )
68
75
if costing_info .cost_bar_steps :
69
76
cost .cost_bar_steps = costing_info .cost_bar_steps
77
+ costing_info .request_is_valid = request_is_valid
70
78
return cost
71
79
72
80
81
+ def check_request_validity (
82
+ request : dict [str , Any ], mandatory_inputs : bool , adaptor_properties : dict [str , Any ]
83
+ ) -> bool :
84
+ """
85
+ Check if the request is valid.
86
+
87
+ Parameters
88
+ ----------
89
+ request : dict[str, Any]
90
+ Request to be processed.
91
+ mandatory_inputs : bool
92
+ Whether mandatory inputs have been provided.
93
+ adaptor_properties : dict[str, Any]
94
+ Adaptor properties.
95
+
96
+ Returns
97
+ -------
98
+ bool
99
+ Whether the request is valid.
100
+ """
101
+ if not mandatory_inputs :
102
+ return False
103
+ try :
104
+ adaptor = adaptors .instantiate_adaptor (adaptor_properties = adaptor_properties )
105
+ _ = adaptor .check_validity (request .get ("inputs" , {}))
106
+ return True
107
+ except cads_adaptors .exceptions .InvalidRequest :
108
+ return False
109
+
110
+
73
111
def compute_highest_cost_limit_ratio (
74
112
costing_info : models .CostingInfo ,
75
113
) -> models .RequestCost :
0 commit comments