Skip to content

Commit fbf58af

Browse files
Notify on clientData changes.
1 parent 6996788 commit fbf58af

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

robot-server/robot_server/client_data/router.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
from robot_server.errors.error_responses import ErrorBody, ErrorDetails
1414
from robot_server.service.json_api.request import RequestModel
1515
from robot_server.service.json_api.response import SimpleBody, SimpleEmptyBody
16+
from robot_server.service.notifications.publishers.client_data_publisher import (
17+
ClientDataPublisher,
18+
get_client_data_publisher,
19+
)
1620

1721
router = fastapi.APIRouter()
1822

@@ -62,9 +66,13 @@ class ClientDataKeyDoesNotExist(ErrorDetails):
6266
async def put_client_data( # noqa: D103
6367
key: Key,
6468
request_body: RequestModel[ClientData],
65-
store: ClientDataStore = fastapi.Depends(get_client_data_store),
69+
store: Annotated[ClientDataStore, fastapi.Depends(get_client_data_store)],
70+
client_data_publisher: Annotated[
71+
ClientDataPublisher, fastapi.Depends(get_client_data_publisher)
72+
],
6673
) -> SimpleBody[ClientData]:
6774
store.put(key, request_body.data)
75+
await client_data_publisher.publish_client_data(key)
6876
return SimpleBody.construct(data=store.get(key))
6977

7078

@@ -104,7 +112,10 @@ async def get_client_data( # noqa: D103
104112
)
105113
async def delete_client_data( # noqa: D103
106114
key: Key,
107-
store: ClientDataStore = fastapi.Depends(get_client_data_store),
115+
store: Annotated[ClientDataStore, fastapi.Depends(get_client_data_store)],
116+
client_data_publisher: Annotated[
117+
ClientDataPublisher, fastapi.Depends(get_client_data_publisher)
118+
],
108119
) -> SimpleEmptyBody:
109120
try:
110121
store.delete(key)
@@ -113,6 +124,7 @@ async def delete_client_data( # noqa: D103
113124
fastapi.status.HTTP_404_NOT_FOUND
114125
) from e
115126
else:
127+
await client_data_publisher.publish_client_data(key)
116128
return SimpleEmptyBody.construct()
117129

118130

@@ -125,4 +137,8 @@ async def delete_all_client_data( # noqa: D103
125137
store: ClientDataStore = fastapi.Depends(get_client_data_store),
126138
) -> SimpleEmptyBody:
127139
store.delete_all()
140+
# FIXME(mm, 2024-07-24): We need to notify MQTT subscribers to
141+
# `robot-server/client/{key}` (for all values of `key`).
142+
# We can do it like RunsPublisher and drive the notifications from the underlying
143+
# store instead of driving notifications from these endpoint functions.
128144
return SimpleEmptyBody.construct()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import Annotated
2+
import fastapi
3+
from robot_server.service.notifications import topics
4+
from robot_server.service.notifications.notification_client import (
5+
NotificationClient,
6+
get_notification_client,
7+
)
8+
9+
10+
class ClientDataPublisher:
11+
"""Publishes clientData topics."""
12+
13+
def __init__(self, client: NotificationClient) -> None:
14+
self._client = client
15+
16+
async def publish_client_data(self, client_data_key: str) -> None:
17+
"""Publish the equivalent of `GET /clientData/{key}`."""
18+
await self._client.publish_advise_refetch_async(
19+
topics.client_data(client_data_key)
20+
)
21+
22+
23+
async def get_client_data_publisher(
24+
notification_client: Annotated[
25+
NotificationClient, fastapi.Depends(get_notification_client)
26+
],
27+
) -> ClientDataPublisher:
28+
"""Return a ClientDataPublisher for use by FastAPI endpoints."""
29+
return ClientDataPublisher(notification_client)

0 commit comments

Comments
 (0)