Skip to content

Commit

Permalink
Merge pull request #4 from sbgisen/hotfix/to_dict
Browse files Browse the repository at this point in the history
Fix Internal server Error #3
  • Loading branch information
h-wata authored Apr 8, 2024
2 parents 3c5ffdc + e6f411e commit 0d581f2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ After rebooting the device, you shall get a response by running commands such as

```bash
curl http://$KACHAKA_IP:26502/kachaka/get_robot_pose
# {"get_robot_pose":{"x":-0.27050725751488025,"y":0.06214801113488019,"theta":0.09612629786430153}}
curl http://$KACHAKA_IP:26502/kachaka/get_map_list
# {"get_map_list":[{"name":"1F","id":"XXX"},{"name":"2F","id":"YYY"}]}
```

## Local development
Expand Down
10 changes: 5 additions & 5 deletions scripts/rest_kachaka_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ async def init_channel() -> None:
await kachaka_client.update_resolver()


def to_dict(response: Any) -> Union[dict, list]: # noqa: ANN401
def convert_response(response: Any) -> Union[dict, list]: # noqa: ANN401
"""
Convert a Protobuf response to a dictionary.
Convert a Protobuf response to a dictionary or a list.
Args:
response: The Protobuf response to convert.
Expand All @@ -62,7 +62,7 @@ def to_dict(response: Any) -> Union[dict, list]: # noqa: ANN401
or isinstance(response, list)
or isinstance(response, RepeatedCompositeContainer)
):
return [to_dict(r) for r in response]
return [convert_response(r) for r in response]
return response


Expand All @@ -85,7 +85,7 @@ async def run_method_or_404(attr: str, params: dict = {}, to_dict: bool = True)
raise HTTPException(status_code=404, detail="Method not found")
method = getattr(kachaka_client, attr)
response = await method(**params)
return to_dict(response) if to_dict else response
return convert_response(response) if to_dict else response


@app.get("/kachaka/{front_or_back}_camera_image.jpeg")
Expand Down Expand Up @@ -122,7 +122,7 @@ async def get(method: str) -> dict:
Returns:
The response from the method as a dictionary.
"""
return await run_method_or_404(method)
return {method: await run_method_or_404(method)}


@app.post("/kachaka/{method:path}")
Expand Down

0 comments on commit 0d581f2

Please sign in to comment.