From 7b9a077f2d1b45ba5a50003b1f83607379294a45 Mon Sep 17 00:00:00 2001 From: nmaytan Date: Thu, 22 Aug 2024 18:13:53 -0400 Subject: [PATCH] Refactor add/delete inst/det endpoints into one --- src/nsls2api/api/v1/beamline_api.py | 86 +++++++++++++---------- src/nsls2api/services/beamline_service.py | 6 +- 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/src/nsls2api/api/v1/beamline_api.py b/src/nsls2api/api/v1/beamline_api.py index 1a5a2697..95f5d7b5 100644 --- a/src/nsls2api/api/v1/beamline_api.py +++ b/src/nsls2api/api/v1/beamline_api.py @@ -1,5 +1,5 @@ import fastapi -from fastapi import HTTPException, Depends +from fastapi import HTTPException, Depends, Request from fastapi.security.api_key import APIKey from nsls2api.api.models.proposal_model import ( ProposalDirectoriesList, @@ -72,53 +72,67 @@ async def get_beamline_detectors(name: str) -> DetectorList: @router.put( - "/beamline/{name}/detector/", + "/beamline/{name}/detector/{detector_name}", include_in_schema=True, response_model=Detector, dependencies=[Depends(validate_admin_role)], ) -async def add_detector(name: str, detector: Detector): - logger.info(f"Adding detector {detector.name} to beamline {name}") - - new_detector = await beamline_service.add_detector( - beamline_name=name, - detector_name=detector.name, - directory_name=detector.directory_name, - granularity=detector.granularity, - description=detector.description, - manufacturer=detector.manufacturer, - ) - - if new_detector is None: - raise HTTPException( - status_code=409, - detail=f"Detector {detector.name} already exists in beamline {name}", - ) - - return new_detector - - -@router.put( - "/beamline/{name}/delete-detector/", +@router.delete( + "/beamline/{name}/detector/{detector_name}", include_in_schema=True, response_model=Detector, dependencies=[Depends(validate_admin_role)], ) -async def delete_detector(name: str, detector: Detector): - logger.info(f"Deleting detector {detector.name} from beamline {name}") +async def add_or_delete_detector( + request: Request, name: str, detector_name: str, detector: Detector | None = None +): + if request.method == "PUT": + logger.info(f"Adding detector {detector_name} to beamline {name}") + + if not detector: + raise HTTPException( + status_code=422, + detail=f"No detector information supplied in request body.", + ) + if detector_name != detector.name: + raise HTTPException( + status_code=400, + detail=f"Detector name in path '{detector_name}' does not match name in body '{detector.name}'.", + ) + + new_detector = await beamline_service.add_detector( + beamline_name=name, + detector_name=detector.name, + directory_name=detector.directory_name, + granularity=detector.granularity, + description=detector.description, + manufacturer=detector.manufacturer, + ) - deleted_detector = await beamline_service.delete_detector( - beamline_name=name, - detector_name=detector.name, - ) + if new_detector is None: + raise HTTPException( + status_code=409, + detail=f"Detector {detector.name} already exists in beamline {name}", + ) - if deleted_detector is None: - raise HTTPException( - status_code=404, - detail=f"Detector {detector_name} was not found for beamline {beamline_name}", + changed_detector = new_detector + elif request.method == "DELETE": + logger.info(f"Deleting detector {detector_name} from beamline {name}") + + deleted_detector = await beamline_service.delete_detector( + beamline_name=name, + detector_name=detector_name, ) - return deleted_detector + if deleted_detector is None: + raise HTTPException( + status_code=404, + detail=f"Detector {detector_name} was not found for beamline {beamline_name}", + ) + + changed_detector = deleted_detector + + return changed_detector @router.get( diff --git a/src/nsls2api/services/beamline_service.py b/src/nsls2api/services/beamline_service.py index 89d4db67..0edefa71 100644 --- a/src/nsls2api/services/beamline_service.py +++ b/src/nsls2api/services/beamline_service.py @@ -120,9 +120,7 @@ async def add_detector( manufacturer=manufacturer, ) - current_detector_names = ( - detector.name for detector in beamlines.detectors - ) + current_detector_names = (detector.name for detector in beamlines.detectors) current_directory_names = ( detector.directory_name for detector in beamline.detectors ) @@ -130,7 +128,7 @@ async def add_detector( logger.info( f"Detector with name {detector_name} already exists in beamline {beamline_name}" ) - return None: + return None elif directory_name in current_directory_names: logger.info( f"Detector with directory name {directory_name} already exists in beamline {beamline_name}"