Skip to content

Commit

Permalink
chore(robot-server): upgrade fastapi to 0.54.1 (#5439)
Browse files Browse the repository at this point in the history
* updgrade fastapi to 0.54.1

* remove workarounds to deal with bugs in fastapi

* lint errors

* select BR2_PACKAGE_PYTHON_DOTENV package
  • Loading branch information
amitlissack authored Apr 20, 2020
1 parent 14c68c8 commit 0db633b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 96 deletions.
1 change: 1 addition & 0 deletions robot-server/Config.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ config BR2_PACKAGE_PYTHON_OPENTRONS_ROBOT_SERVER
select BR2_PACKAGE_PYTHON_FASTAPI # runtime
select BR2_PACKAGE_PYTHON_UVICORN # runtime
select BR2_PACKAGE_PYTHON_MULTIPART # runtime
select BR2_PACKAGE_PYTHON_DOTENV # runtime
help
Opentrons HTTP server. Controls an OT2 robot.

Expand Down
2 changes: 1 addition & 1 deletion robot-server/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ requests = "*"

[packages]
uvicorn = "==0.11.3"
fastapi = "==0.49.0"
fastapi = "==0.54.1"
python-multipart = "==0.0.5"
aiohttp = "==3.4.4"
python-dotenv = "*"
85 changes: 43 additions & 42 deletions robot-server/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 5 additions & 15 deletions robot-server/robot_server/service/routers/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Union, Dict
from typing import Dict

from starlette import status
from fastapi import APIRouter, Depends
Expand Down Expand Up @@ -182,16 +182,14 @@ async def get_robot_settings(
response_model=MultiPipetteSettings,
response_model_by_alias=True,
response_model_exclude_unset=True)
async def get_pipette_settings() -> Union[Dict, MultiPipetteSettings]:
async def get_pipette_settings() -> MultiPipetteSettings:
res = {}
for pipette_id in pipette_config.known_pipettes():
# Have to convert to dict using by_alias due to bug in fastapi
res[pipette_id] = _pipette_settings_from_config(
pipette_config,
pipette_id,
).dict(by_alias=True,
exclude_unset=True)

)
return res


Expand All @@ -201,16 +199,12 @@ async def get_pipette_settings() -> Union[Dict, MultiPipetteSettings]:
response_model_by_alias=True,
response_model_exclude_unset=True,
responses={status.HTTP_404_NOT_FOUND: {"model": V1BasicResponse}})
async def get_pipette_setting(pipette_id: str) -> Union[Dict, PipetteSettings]:
async def get_pipette_setting(pipette_id: str) -> PipetteSettings:
if pipette_id not in pipette_config.known_pipettes():
raise V1HandlerError(status_code=status.HTTP_404_NOT_FOUND,
message=f'{pipette_id} is not a valid pipette id')
# Have to convert to dict using by_alias due to bug in fastapi
r = _pipette_settings_from_config(
pipette_config, pipette_id
).dict(
by_alias=True,
exclude_unset=True,
)
return r

Expand All @@ -228,7 +222,7 @@ async def get_pipette_setting(pipette_id: str) -> Union[Dict, PipetteSettings]:
async def patch_pipette_setting(
pipette_id: str,
settings_update: PipetteSettingsUpdate) \
-> Union[Dict, PipetteSettings]:
-> PipetteSettings:

# Convert fields to dict of field name to value
fields = settings_update.setting_fields or {}
Expand All @@ -241,12 +235,8 @@ async def patch_pipette_setting(
raise V1HandlerError(
status_code=status.HTTP_412_PRECONDITION_FAILED,
message=str(e))
# Have to convert to dict using by_alias due to bug in fastapi
r = _pipette_settings_from_config(
pipette_config, pipette_id
).dict(
by_alias=True,
exclude_unset=True,
)
return r

Expand Down
42 changes: 4 additions & 38 deletions robot-server/tests/service/rpc/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
from threading import Event, Semaphore
from uuid import uuid4 as uuid

from fastapi.routing import APIWebSocketRoute
from starlette.testclient import WebSocketTestSession, TestClient
from fastapi import FastAPI, APIRouter
from starlette.testclient import WebSocketTestSession

from opentrons.protocol_api.execute import ExceptionInProtocolError

from robot_server.service.dependencies import get_rpc_server
from robot_server.service.rpc import rpc
from robot_server.service.routers.rpc import router


class Session(typing.NamedTuple):
Expand All @@ -25,38 +22,7 @@ class Session(typing.NamedTuple):


@pytest.fixture
def rpc_client():
"""Create an starlette TestClient to communicate with rpc"""

# We should be able to just use api_client fixture but can't due to a bug
# in fastapi.
# Fastapi does not allow overriding dependencies on websocket endpoints.
# See https://github.com/tiangolo/fastapi/issues/998
# We've submitted a PR to fix this:
# https://github.com/tiangolo/fastapi/pull/1122 but in the meantime we
# have to hack monkey patch a solution
app = FastAPI()

def add_api_websocket_route(
self, path: str, endpoint: typing.Callable, name: str = None
) -> None:
route = APIWebSocketRoute(
path,
endpoint=endpoint,
name=name,
dependency_overrides_provider=self.dependency_overrides_provider,
)
self.routes.append(route)

APIRouter.add_api_websocket_route = add_api_websocket_route # type: ignore

app.include_router(router)

return TestClient(app)


@pytest.fixture
def session(loop, rpc_client, request) -> Session:
def session(loop, api_client, request) -> Session:
"""
Create testing session. Tests using this fixture are expected
to have @pytest.mark.parametrize('root', [value]) decorator set.
Expand All @@ -74,10 +40,10 @@ async def get_server():
return _internal_server

# Override the RPC server dependency
rpc_client.app.dependency_overrides[get_rpc_server] = get_server
api_client.app.dependency_overrides[get_rpc_server] = get_server

# Connect
socket = rpc_client.websocket_connect("/")
socket = api_client.websocket_connect("/")
token = str(uuid())

def call(**kwargs):
Expand Down

0 comments on commit 0db633b

Please sign in to comment.