Skip to content

Commit

Permalink
Add new API endpoint for retrieving current operating cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcampbell committed Feb 1, 2024
1 parent ac036ce commit 1490e77
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/nsls2api/api/models/facility_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ class FacilityName(Enum):

class FacilityCyclesResponseModel(pydantic.BaseModel):
facility: str
cycles: list[str]
cycles: list[str]

class FacilityCurrentOperatingCycleResponseModel(pydantic.BaseModel):
facility: str
cycle: str
36 changes: 27 additions & 9 deletions src/nsls2api/api/v1/facility_api.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,59 @@
import fastapi

from nsls2api.api.models.facility_model import FacilityName, FacilityCyclesResponseModel
from nsls2api.api.models.facility_model import (
FacilityName,
FacilityCyclesResponseModel,
FacilityCurrentOperatingCycleResponseModel,
)
from nsls2api.services import proposal_service, facility_service

router = fastapi.APIRouter()


# TODO: Add back into schema when implemented.
@router.get(
"/facility/{facility}/cycles/current", response_model=FacilityCyclesResponseModel, include_in_schema=True
"/facility/{facility}/cycles/current",
response_model=FacilityCurrentOperatingCycleResponseModel,
include_in_schema=True,
)
async def get_current_operating_cycle(facility: FacilityName):
current_cycle = await facility_service.current_operating_cycle(facility.name)
if current_cycle is None:
return fastapi.responses.JSONResponse(
{"error": f"No current operating cycle was found for facility {facility.name}"},
{
"error": f"No current operating cycle was found for facility {facility.name}"
},
status_code=404,
)

# TODO: Maybe add a health check here
# print("Are we healthy ? ")
# print(await facility_service.is_healthy(facility.name))

response_model = FacilityCyclesResponseModel(facility=facility.name, cycles=[current_cycle])

response_model = FacilityCurrentOperatingCycleResponseModel(
facility=facility.name, cycle=current_cycle
)

return response_model

@router.get("/facility/{facility}/cycles", include_in_schema=True)

@router.get(
"/facility/{facility}/cycles",
response_model=FacilityCyclesResponseModel,
include_in_schema=True,
)
async def get_facility_cycles(facility: FacilityName):
cycle_list = await facility_service.facility_cycles(facility.name)
if cycle_list is None:
return fastapi.responses.JSONResponse(
{"error": f"No cycles were found for facility {facility.name}"},
status_code=404,
)
data = {"facility": facility.name, "cycles": cycle_list}
return data
response_model = FacilityCyclesResponseModel(
facility=facility.name, cycles=cycle_list
)
return response_model


@router.get("/facility/{facility}/cycle/{cycle}/proposals", include_in_schema=True)
async def get_proposals_for_cycle(facility: FacilityName, cycle: str):
Expand Down

0 comments on commit 1490e77

Please sign in to comment.