From 30a36ef629af9efb9ed9a7dd619a1f36435b589f Mon Sep 17 00:00:00 2001 From: Thomas Kluyver <thomas.kluyver@xfel.eu> Date: Wed, 4 Dec 2024 19:02:16 +0100 Subject: [PATCH] Get rid of CalCatInjector class, less indirection --- src/cal_tools/constants.py | 61 +++++++++++++------------------------- 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/src/cal_tools/constants.py b/src/cal_tools/constants.py index ac56c1e7a..edd00892c 100644 --- a/src/cal_tools/constants.py +++ b/src/cal_tools/constants.py @@ -29,32 +29,6 @@ class CCVAlreadyInjectedError(InjectionError): ... - -class CalCatInjector: - """Helper methods for requests to CalCat""" - def __init__(self, client: CalCatAPIClient): - self.client = client - - # Posting APIs - def set_expected_condition(self, conditions: dict): - return self.client.post("conditions/set_expected_condition", conditions) - - def create_calibration_constant(self, calibration_constant: dict): - return self.client.post("calibration_constants", calibration_constant) - - def get_or_create_report(self, report: dict): - # Based on create or get API - return self.client.post("reports/set", report) - - def create_calibration_constant_version(self, ccv: dict): - try: - return self.client.post("calibration_constant_versions", ccv) - except CalCatAPIError as e: - if e.status_code == 422: - raise CCVAlreadyInjectedError - raise - - @dataclass class ParameterConditionAttribute: value: str @@ -250,7 +224,7 @@ def get_condition_dict( def get_or_create_calibration_constant( - injector: CalCatInjector, + client: CalCatAPIClient, calibration: str, detector_type: str, condition_id: int, @@ -259,8 +233,8 @@ def get_or_create_calibration_constant( cond_id = condition_id # Prepare some parameters to set Calibration Constant. - cal_id = injector.client.calibration_by_name(calibration)['id'] - det_type_id = injector.client.detector_type_by_name(detector_type)['id'] + cal_id = client.calibration_by_name(calibration)['id'] + det_type_id = client.detector_type_by_name(detector_type)['id'] cc_name = create_unique_cc_name(detector_type, calibration, condition_name) calibration_constant = dict( @@ -273,19 +247,19 @@ def get_or_create_calibration_constant( description = "", ) try: - cc_id = injector.client.get( - f"calibrations/{cal_id}/get_calibration_constant", # noqa + cc_id = client.get( + f"calibrations/{cal_id}/get_calibration_constant", calibration_constant ) except CalCatAPIError as e: if e.status_code != 404: raise - cc_id = injector.create_calibration_constant(calibration_constant)['id'] + cc_id = client.post("calibration_constants", calibration_constant)['id'] return cc_id def create_condition( - injector: CalCatInjector, + client: CalCatAPIClient, detector_type: str, pdu_name: str, pdu_uuid: float, @@ -297,7 +271,7 @@ def create_condition( # Add the missing parameter_id value in `ParameterConditionAttribute`s. for param, cond in cond_params.items(): - cond.parameter_id = injector.client.parameter_by_name(param)['id'] + cond.parameter_id = client.parameter_by_name(param)['id'] cond_params[param] = asdict(cond) # Create condition table in database, if not available. @@ -308,7 +282,9 @@ def create_condition( event_at = str(datetime.today()), # TODO: Why is this needed? it is not written in swagger. description = '', ) - resp = injector.set_expected_condition({"condition":cond}) + resp = client.post( + "conditions/set_expected_condition", {"condition": cond} + ) return resp['id'], cond_name @@ -356,12 +332,12 @@ def inject_ccv(const_src, ccv_root, report_to=None, client=None): const_src (str or Path): Path to CCV data file. ccv_root (str): CCV HDF group name. report_to (str): Metadata location. + client (CalCatAPIClient, optional): Client for CalCat API. Raises: RuntimeError: If CalCat POST request fails. """ if client is None: client = get_client() - injector = CalCatInjector(client) pdu_name, calibration, _ = ccv_root.lstrip('/').split('/') @@ -377,17 +353,17 @@ def inject_ccv(const_src, ccv_root, report_to=None, client=None): try: condition_id, condition_name = create_condition( - injector, detector_type, pdu_name, pdu_uuid, cond_params) + client, detector_type, pdu_name, pdu_uuid, cond_params) # Create Calibration Constant in database, if not available. cc_id = get_or_create_calibration_constant( - injector, calibration, detector_type, condition_id, condition_name) + client, calibration, detector_type, condition_id, condition_name) # Create report in database, if not available report_id = None if report_to: report_path = Path(report_to).absolute().with_suffix('.pdf') - resp = injector.get_or_create_report(dict( + resp = client.post("reports/set", dict( name=report_path.stem, file_path=str(report_path), flg_available='true', @@ -418,7 +394,12 @@ def inject_ccv(const_src, ccv_root, report_to=None, client=None): flg_good_quality = 'true', description = '', ) - injector.create_calibration_constant_version(ccv) + try: + client.post("calibration_constant_versions", ccv) + except CalCatAPIError as e: + if e.status_code == 422: + raise CCVAlreadyInjectedError + raise except Exception: const_dest.unlink() # Delete already copied CCV file. raise -- GitLab