Skip to content

Commit

Permalink
perf(robot-server): Flatten FastAPI routers (#17169)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxColoring authored Dec 23, 2024
1 parent 03fbfc8 commit cf1c3bd
Show file tree
Hide file tree
Showing 27 changed files with 395 additions and 52 deletions.
2 changes: 1 addition & 1 deletion robot-server/robot_server/app_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ async def _lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
)

# main router
app.include_router(router=router)
router.install_on_app(app)


def _get_persistence_directory(settings: RobotServerSettings) -> Optional[Path]:
Expand Down
3 changes: 2 additions & 1 deletion robot-server/robot_server/client_data/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Annotated, Literal

import fastapi
from server_utils.fastapi_utils.light_router import LightRouter

from robot_server.client_data.store import (
ClientData,
Expand All @@ -18,7 +19,7 @@
get_client_data_publisher,
)

router = fastapi.APIRouter()
router = LightRouter()


Key = Annotated[
Expand Down
5 changes: 3 additions & 2 deletions robot-server/robot_server/commands/router.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Router for top-level /commands endpoints."""
from typing import Annotated, Final, List, Literal, Optional, cast

from fastapi import APIRouter, Depends, Query, status
from fastapi import Depends, Query, status
from server_utils.fastapi_utils.light_router import LightRouter

from opentrons.protocol_engine import CommandIntent
from opentrons.protocol_engine.errors import CommandDoesNotExistError
Expand All @@ -25,7 +26,7 @@
_DEFAULT_COMMAND_LIST_LENGTH: Final = 20


commands_router = APIRouter()
commands_router = LightRouter()


class CommandNotFound(ErrorDetails):
Expand Down
5 changes: 3 additions & 2 deletions robot-server/robot_server/data_files/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from textwrap import dedent
from typing import Annotated, Optional, Literal, Union

from fastapi import APIRouter, UploadFile, File, Form, Depends, Response, status
from fastapi import UploadFile, File, Form, Depends, Response, status
from opentrons.protocol_reader import FileHasher, FileReaderWriter
from server_utils.fastapi_utils.light_router import LightRouter

from robot_server.service.json_api import (
SimpleBody,
Expand All @@ -32,7 +33,7 @@
from ..protocols.dependencies import get_file_hasher, get_file_reader_writer
from ..service.dependencies import get_current_time, get_unique_id

datafiles_router = APIRouter()
datafiles_router = LightRouter()


class MultipleDataFileSources(ErrorDetails):
Expand Down
3 changes: 2 additions & 1 deletion robot-server/robot_server/deck_configuration/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import fastapi
from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY
from server_utils.fastapi_utils.light_router import LightRouter

from opentrons_shared_data.deck.types import DeckDefinitionV5

Expand All @@ -21,7 +22,7 @@
from .store import DeckConfigurationStore


router = fastapi.APIRouter()
router = LightRouter()


@PydanticResponse.wrap_route(
Expand Down
3 changes: 2 additions & 1 deletion robot-server/robot_server/error_recovery/settings/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from typing import Annotated

import fastapi
from server_utils.fastapi_utils.light_router import LightRouter

from robot_server.service.json_api import PydanticResponse, RequestModel, SimpleBody
from .models import RequestData, ResponseData
from .store import ErrorRecoverySettingStore, get_error_recovery_setting_store


router = fastapi.APIRouter()
router = LightRouter()
_PATH = "/errorRecovery/settings"


Expand Down
5 changes: 3 additions & 2 deletions robot-server/robot_server/instruments/router.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Instruments routes."""
from typing import Annotated, Optional, Dict, List, cast

from fastapi import APIRouter, status, Depends
from fastapi import status, Depends
from server_utils.fastapi_utils.light_router import LightRouter

from opentrons.hardware_control.instruments.ot3.instrument_calibration import (
PipetteOffsetSummary,
Expand Down Expand Up @@ -50,7 +51,7 @@

from opentrons.hardware_control import OT3HardwareControlAPI

instruments_router = APIRouter()
instruments_router = LightRouter()


def _pipette_dict_to_pipette_res(
Expand Down
12 changes: 7 additions & 5 deletions robot-server/robot_server/labware_offsets/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from typing import Annotated, Literal

import fastapi
from server_utils.fastapi_utils.light_router import LightRouter

from opentrons.protocol_engine import LabwareOffset, LabwareOffsetCreate, ModuleModel
from opentrons.types import DeckSlotName

Expand All @@ -24,12 +26,12 @@
from .fastapi_dependencies import get_labware_offset_store


router = fastapi.APIRouter(prefix="/labwareOffsets")
router = LightRouter()


@PydanticResponse.wrap_route(
router.post,
path="",
path="/labwareOffsets",
summary="Store a labware offset",
description=textwrap.dedent(
"""\
Expand Down Expand Up @@ -63,7 +65,7 @@ async def post_labware_offset( # noqa: D103

@PydanticResponse.wrap_route(
router.get,
path="",
path="/labwareOffsets",
summary="Search for labware offsets",
description=(
"Get a filtered list of all the labware offsets currently stored on the robot."
Expand Down Expand Up @@ -158,7 +160,7 @@ async def get_labware_offsets( # noqa: D103

@PydanticResponse.wrap_route(
router.delete,
path="/{id}",
path="/labwareOffsets/{id}",
summary="Delete a single labware offset",
description="Delete a single labware offset. The deleted offset is returned.",
)
Expand All @@ -181,7 +183,7 @@ async def delete_labware_offset( # noqa: D103

@PydanticResponse.wrap_route(
router.delete,
path="",
path="/labwareOffsets",
summary="Delete all labware offsets",
)
async def delete_all_labware_offsets( # noqa: D103
Expand Down
4 changes: 2 additions & 2 deletions robot-server/robot_server/maintenance_runs/router/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Maintenance Runs router."""
from fastapi import APIRouter
from server_utils.fastapi_utils.light_router import LightRouter

from .base_router import base_router

from .commands_router import commands_router
from .labware_router import labware_router

maintenance_runs_router = APIRouter()
maintenance_runs_router = LightRouter()

maintenance_runs_router.include_router(base_router)
maintenance_runs_router.include_router(commands_router)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from typing import Annotated, Optional, Callable
from typing_extensions import Literal

from fastapi import APIRouter, Depends, status
from fastapi import Depends, status
from pydantic import BaseModel, Field
from server_utils.fastapi_utils.light_router import LightRouter

from robot_server.errors.error_responses import ErrorDetails, ErrorBody
from robot_server.service.dependencies import get_current_time, get_unique_id
Expand Down Expand Up @@ -42,7 +43,7 @@
from robot_server.service.notifications import get_pe_notify_publishers

log = logging.getLogger(__name__)
base_router = APIRouter()
base_router = LightRouter()


# TODO (spp, 2023-04-10): move all error types from maintenance & regular runs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from typing import Annotated, Optional, Union
from typing_extensions import Final, Literal

from fastapi import APIRouter, Depends, Query, status
from fastapi import Depends, Query, status
from server_utils.fastapi_utils.light_router import LightRouter

from opentrons.protocol_engine import (
CommandPointer,
Expand Down Expand Up @@ -39,7 +40,7 @@

_DEFAULT_COMMAND_LIST_LENGTH: Final = 20

commands_router = APIRouter()
commands_router = LightRouter()


class CommandNotFound(ErrorDetails):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Router for /maintenance_runs endpoints dealing with labware offsets and definitions."""
from typing import Annotated
import logging
from fastapi import APIRouter, Depends, status

from fastapi import Depends, status
from server_utils.fastapi_utils.light_router import LightRouter

from opentrons.protocol_engine import LabwareOffsetCreate, LabwareOffset
from opentrons.protocols.models import LabwareDefinition
Expand All @@ -15,7 +17,7 @@
from .base_router import RunNotFound, RunNotIdle, get_run_data_from_url

log = logging.getLogger(__name__)
labware_router = APIRouter()
labware_router = LightRouter()


@PydanticResponse.wrap_route(
Expand Down
6 changes: 4 additions & 2 deletions robot-server/robot_server/modules/router.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Modules routes."""
from fastapi import APIRouter, Depends, status
from typing import Annotated, List, Dict

from fastapi import Depends, status
from server_utils.fastapi_utils.light_router import LightRouter

from opentrons.hardware_control import HardwareControlAPI
from opentrons.hardware_control.modules import module_calibration
from opentrons.protocol_engine.types import Vec3f
Expand All @@ -21,7 +23,7 @@
from .module_identifier import ModuleIdentifier
from .module_data_mapper import ModuleDataMapper

modules_router = APIRouter()
modules_router = LightRouter()


@PydanticResponse.wrap_route(
Expand Down
4 changes: 2 additions & 2 deletions robot-server/robot_server/protocols/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from opentrons.util.performance_helpers import TrackingFunctions

from fastapi import (
APIRouter,
Depends,
File,
HTTPException,
Expand All @@ -26,6 +25,7 @@
)
from fastapi.responses import PlainTextResponse
from pydantic import BaseModel, Field
from server_utils.fastapi_utils.light_router import LightRouter

from opentrons.protocol_reader import (
ProtocolReader,
Expand Down Expand Up @@ -151,7 +151,7 @@ class ProtocolLinks(BaseModel):
)


protocols_router = APIRouter()
protocols_router = LightRouter()


@PydanticResponse.wrap_route(
Expand Down
6 changes: 4 additions & 2 deletions robot-server/robot_server/robot/control/router.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Router for /robot/control endpoints."""
from fastapi import APIRouter, status, Depends
from typing import Annotated, TYPE_CHECKING

from fastapi import status, Depends
from server_utils.fastapi_utils.light_router import LightRouter

from opentrons_shared_data.robot.types import RobotType
from opentrons_shared_data.robot.types import RobotTypeEnum
from robot_server.hardware import get_robot_type
Expand All @@ -22,7 +24,7 @@
if TYPE_CHECKING:
from opentrons.hardware_control.ot3api import OT3API # noqa: F401

control_router = APIRouter()
control_router = LightRouter()


async def _get_estop_status_response(
Expand Down
4 changes: 2 additions & 2 deletions robot-server/robot_server/robot/router.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Router for /robot endpoints."""
from fastapi import APIRouter
from server_utils.fastapi_utils.light_router import LightRouter

from .control.router import control_router

robot_router = APIRouter()
robot_router = LightRouter()

robot_router.include_router(router=control_router)
15 changes: 12 additions & 3 deletions robot-server/robot_server/router.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Application routes."""
from fastapi import APIRouter, Depends, status
from fastapi import Depends, status
from server_utils.fastapi_utils.light_router import LightRouter


from .constants import V1_TAG
from .errors.error_responses import LegacyErrorResponse
Expand All @@ -26,24 +28,31 @@
from .subsystems.router import subsystems_router
from .system.router import system_router

router = APIRouter()
router = LightRouter()

# Legacy routes
router.include_router(
router=legacy_routes,
tags=[V1_TAG],
# todo(mm, 2024-12-19): This `responses` setting is preventing us from
# porting legacy_routes from fastapi.APIRouter to our LightRouter.
# Either teach LightRouter how to handle `responses` or stop doing
# a custom 422 response on these endpoints.
responses={
status.HTTP_422_UNPROCESSABLE_ENTITY: {
"model": LegacyErrorResponse,
}
},
)

router.include_router(
router=health_router,
tags=["Health", V1_TAG],
dependencies=[Depends(check_version_header)],
responses={
# todo(mm, 2024-12-19): This `responses` setting is preventing us from
# porting health_router from fastapi.APIRouter to our LightRouter.
# Either teach LightRouter how to handle `responses` or stop doing
# a custom 422 response on these endpoints.
status.HTTP_422_UNPROCESSABLE_ENTITY: {
"model": LegacyErrorResponse,
}
Expand Down
4 changes: 2 additions & 2 deletions robot-server/robot_server/runs/router/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Runs router."""
from fastapi import APIRouter
from server_utils.fastapi_utils.light_router import LightRouter

from .base_router import base_router
from .commands_router import commands_router
from .actions_router import actions_router
from .labware_router import labware_router
from .error_recovery_policy_router import error_recovery_policy_router

runs_router = APIRouter()
runs_router = LightRouter()

runs_router.include_router(base_router)
runs_router.include_router(commands_router)
Expand Down
6 changes: 4 additions & 2 deletions robot-server/robot_server/runs/router/actions_router.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Router for /runs actions endpoints."""
import logging

from fastapi import APIRouter, Depends, status
from datetime import datetime
from typing import Annotated, Literal, Union

from fastapi import Depends, status
from server_utils.fastapi_utils.light_router import LightRouter

from robot_server.errors.error_responses import ErrorDetails, ErrorBody
from robot_server.service.dependencies import get_current_time, get_unique_id
from robot_server.service.json_api import RequestModel, SimpleBody, PydanticResponse
Expand Down Expand Up @@ -37,7 +39,7 @@
)

log = logging.getLogger(__name__)
actions_router = APIRouter()
actions_router = LightRouter()


class RunActionNotAllowed(ErrorDetails):
Expand Down
Loading

0 comments on commit cf1c3bd

Please sign in to comment.