|
12 | 12 | from opentrons.protocol_engine import ModuleModel
|
13 | 13 |
|
14 | 14 | from robot_server.labware_offsets.models import LabwareOffsetNotFound
|
15 |
| -from robot_server.service.dependencies import get_current_time, get_unique_id |
| 15 | +from robot_server.service.dependencies import ( |
| 16 | + UniqueIDFactory, |
| 17 | + get_current_time, |
| 18 | +) |
16 | 19 | from robot_server.service.json_api.request import RequestModel
|
17 | 20 | from robot_server.service.json_api.response import (
|
18 | 21 | MultiBodyMeta,
|
|
42 | 45 | @PydanticResponse.wrap_route(
|
43 | 46 | router.post,
|
44 | 47 | path="/labwareOffsets",
|
45 |
| - summary="Store a labware offset", |
| 48 | + summary="Store labware offsets", |
46 | 49 | description=textwrap.dedent(
|
47 | 50 | """\
|
48 |
| - Store a labware offset for later retrieval through `GET /labwareOffsets`. |
| 51 | + Store labware offsets for later retrieval through `GET /labwareOffsets`. |
49 | 52 |
|
50 | 53 | On its own, this does not affect robot motion.
|
51 |
| - To do that, you must add the offset to a run, through the `/runs` endpoints. |
| 54 | + To do that, you must add the offsets to a run, through the `/runs` endpoints. |
| 55 | +
|
| 56 | + The response body's `data` will either be a single offset or a list of offsets, |
| 57 | + depending on whether you provided a single offset or a list in the request body's `data`. |
52 | 58 | """
|
53 | 59 | ),
|
54 | 60 | status_code=201,
|
55 | 61 | include_in_schema=False, # todo(mm, 2025-01-08): Include for v8.4.0.
|
56 | 62 | )
|
57 |
| -async def post_labware_offset( # noqa: D103 |
| 63 | +async def post_labware_offsets( # noqa: D103 |
58 | 64 | store: Annotated[LabwareOffsetStore, fastapi.Depends(get_labware_offset_store)],
|
59 |
| - new_offset_id: Annotated[str, fastapi.Depends(get_unique_id)], |
| 65 | + new_offset_id_factory: Annotated[UniqueIDFactory, fastapi.Depends(UniqueIDFactory)], |
60 | 66 | new_offset_created_at: Annotated[datetime, fastapi.Depends(get_current_time)],
|
61 |
| - request_body: Annotated[RequestModel[StoredLabwareOffsetCreate], fastapi.Body()], |
62 |
| -) -> PydanticResponse[SimpleBody[StoredLabwareOffset]]: |
63 |
| - new_offset = IncomingStoredLabwareOffset( |
64 |
| - id=new_offset_id, |
65 |
| - createdAt=new_offset_created_at, |
66 |
| - definitionUri=request_body.data.definitionUri, |
67 |
| - locationSequence=request_body.data.locationSequence, |
68 |
| - vector=request_body.data.vector, |
| 67 | + request_body: Annotated[ |
| 68 | + RequestModel[StoredLabwareOffsetCreate | list[StoredLabwareOffsetCreate]], |
| 69 | + fastapi.Body(), |
| 70 | + ], |
| 71 | +) -> PydanticResponse[SimpleBody[StoredLabwareOffset | list[StoredLabwareOffset]]]: |
| 72 | + new_offsets = [ |
| 73 | + IncomingStoredLabwareOffset( |
| 74 | + id=new_offset_id_factory.get(), |
| 75 | + createdAt=new_offset_created_at, |
| 76 | + definitionUri=request_body_element.definitionUri, |
| 77 | + locationSequence=request_body_element.locationSequence, |
| 78 | + vector=request_body_element.vector, |
| 79 | + ) |
| 80 | + for request_body_element in ( |
| 81 | + request_body.data |
| 82 | + if isinstance(request_body.data, list) |
| 83 | + else [request_body.data] |
| 84 | + ) |
| 85 | + ] |
| 86 | + |
| 87 | + for new_offset in new_offsets: |
| 88 | + store.add(new_offset) |
| 89 | + |
| 90 | + stored_offsets = [ |
| 91 | + StoredLabwareOffset.model_construct( |
| 92 | + id=incoming.id, |
| 93 | + createdAt=incoming.createdAt, |
| 94 | + definitionUri=incoming.definitionUri, |
| 95 | + locationSequence=incoming.locationSequence, |
| 96 | + vector=incoming.vector, |
| 97 | + ) |
| 98 | + for incoming in new_offsets |
| 99 | + ] |
| 100 | + |
| 101 | + # Return a list if the client POSTed a list, or an object if the client POSTed an object. |
| 102 | + # For some reason, mypy needs to be given the type annotation explicitly. |
| 103 | + response_data: StoredLabwareOffset | list[StoredLabwareOffset] = ( |
| 104 | + stored_offsets if isinstance(request_body.data, list) else stored_offsets[0] |
69 | 105 | )
|
70 |
| - store.add(new_offset) |
| 106 | + |
71 | 107 | return await PydanticResponse.create(
|
72 |
| - content=SimpleBody.model_construct( |
73 |
| - data=StoredLabwareOffset( |
74 |
| - id=new_offset_id, |
75 |
| - createdAt=new_offset_created_at, |
76 |
| - definitionUri=request_body.data.definitionUri, |
77 |
| - locationSequence=request_body.data.locationSequence, |
78 |
| - vector=request_body.data.vector, |
79 |
| - ) |
80 |
| - ), |
| 108 | + content=SimpleBody.model_construct(data=response_data), |
81 | 109 | status_code=201,
|
82 | 110 | )
|
83 | 111 |
|
|
0 commit comments