Skip to content

Commit

Permalink
wrap request validity check in function
Browse files Browse the repository at this point in the history
  • Loading branch information
mcucchi9 committed Dec 3, 2024
1 parent 120e199 commit 9b5ad0d
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions cads_processing_api_service/costing.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,11 @@ def estimate_cost(
resource_id=process_id, table=table, session=catalogue_session
)
adaptor_properties = adaptors.get_adaptor_properties(dataset)
if not mandatory_inputs:
request_is_valid = False
else:
try:
adaptor = adaptors.instantiate_adaptor(
adaptor_properties=adaptor_properties
)
_ = adaptor.check_validity(request.get("inputs", {}))
request_is_valid = True
except cads_adaptors.exceptions.InvalidRequest:
request_is_valid = False
request_is_valid = check_request_validity(
request=request,
mandatory_inputs=mandatory_inputs,
adaptor_properties=adaptor_properties,
)
costing_info = costing.compute_costing(
request.get("inputs", {}), adaptor_properties, request_origin
)
Expand All @@ -84,6 +78,36 @@ def estimate_cost(
return cost


def check_request_validity(
request: dict[str, Any], mandatory_inputs: bool, adaptor_properties: dict[str, Any]
) -> bool:
"""
Check if the request is valid.
Parameters
----------
request : dict[str, Any]
Request to be processed.
mandatory_inputs : bool
Whether mandatory inputs have been provided.
adaptor_properties : dict[str, Any]
Adaptor properties.
Returns
-------
bool
Whether the request is valid.
"""
if not mandatory_inputs:
return False
try:
adaptor = adaptors.instantiate_adaptor(adaptor_properties=adaptor_properties)
_ = adaptor.check_validity(request.get("inputs", {}))
return True
except cads_adaptors.exceptions.InvalidRequest:
return False


def compute_highest_cost_limit_ratio(
costing_info: models.CostingInfo,
) -> models.RequestCost:
Expand Down

0 comments on commit 9b5ad0d

Please sign in to comment.