Skip to content

Commit

Permalink
Refactor add/delete inst/det endpoints into one
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaytan committed Aug 29, 2024
1 parent 3ea0c1a commit 7b9a077
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 40 deletions.
86 changes: 50 additions & 36 deletions src/nsls2api/api/v1/beamline_api.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions src/nsls2api/services/beamline_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,15 @@ 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
)
if detector_name in current_detector_names:
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}"
Expand Down

0 comments on commit 7b9a077

Please sign in to comment.