Skip to content

Commit

Permalink
update python implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
achim-k committed Mar 18, 2024
1 parent be45d63 commit e4a1869
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 35 deletions.
46 changes: 29 additions & 17 deletions python/src/foxglove_websocket/examples/json_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,35 @@ async def on_service_request(
await server.add_service(
{
"name": "example_set_bool",
"requestSchema": json.dumps(
{
"type": "object",
"properties": {
"data": {"type": "boolean"},
},
}
),
"responseSchema": json.dumps(
{
"type": "object",
"properties": {
"success": {"type": "boolean"},
"message": {"type": "string"},
},
}
),
"request": {
"encoding": "json",
"schemaName": "requestSchema",
"schemaEncoding": "jsonschema",
"schema": json.dumps(
{
"type": "object",
"properties": {
"data": {"type": "boolean"},
},
}
),
},
"response": {
"encoding": "json",
"schemaName": "responseSchema",
"schemaEncoding": "jsonschema",
"schema": json.dumps(
{
"type": "object",
"properties": {
"success": {"type": "boolean"},
"message": {"type": "string"},
},
}
),
},
"requestSchema": None,
"responseSchema": None,
"type": "example_set_bool",
}
)
Expand Down
9 changes: 9 additions & 0 deletions python/src/foxglove_websocket/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ async def remove_channel(self, chan_id: ChannelId):
)

async def add_service(self, service: ServiceWithoutId) -> ServiceId:
if service.request is None and service.requestSchema is None:
raise ValueError(
f"Invalid service definition: Either 'request' or 'requestSchema' must be defined"
)
if service.response is None and service.responseSchema is None:
raise ValueError(
f"Invalid service definition: Either 'response' or 'responseSchema' must be defined"
)

new_id = self._next_service_id
self._next_service_id = ServiceId(new_id + 1)
new_service = Service(id=new_id, **service)
Expand Down
17 changes: 15 additions & 2 deletions python/src/foxglove_websocket/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,24 @@ class Channel(ChannelWithoutId):
id: ChannelId


class ServiceRequestDefinition(TypedDict):
encoding: str
schemaName: str
schemaEncoding: str
schema: str


class ServiceResponseDefinition(ServiceRequestDefinition):
pass


class ServiceWithoutId(TypedDict):
name: str
type: str
requestSchema: str
responseSchema: str
request: Optional[ServiceRequestDefinition]
response: Optional[ServiceResponseDefinition]
requestSchema: Optional[str]
responseSchema: Optional[str]


class Service(ServiceWithoutId):
Expand Down
44 changes: 28 additions & 16 deletions python/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,22 +311,34 @@ async def on_service_request(
service: ServiceWithoutId = {
"name": "set_bool",
"type": "set_bool",
"requestSchema": json.dumps(
{
"type": "object",
"properties": {
"data": {"type": "boolean"},
},
}
),
"responseSchema": json.dumps(
{
"type": "object",
"properties": {
"success": {"type": "boolean"},
},
}
),
"request": {
"encoding": "json",
"schemaName": "requestSchema",
"schemaEncoding": "jsonschema",
"schema": json.dumps(
{
"type": "object",
"properties": {
"data": {"type": "boolean"},
},
}
),
},
"response": {
"encoding": "json",
"schemaName": "responseSchema",
"schemaEncoding": "jsonschema",
"schema": json.dumps(
{
"type": "object",
"properties": {
"success": {"type": "boolean"},
},
}
),
},
"requestSchema": None,
"responseSchema": None,
}
service_id = await server.add_service(service)

Expand Down

0 comments on commit e4a1869

Please sign in to comment.