diff --git a/schunk_egu_egk_gripper_dummy/schunk_egu_egk_gripper_dummy/main.py b/schunk_egu_egk_gripper_dummy/schunk_egu_egk_gripper_dummy/main.py index ea8f176..857964f 100644 --- a/schunk_egu_egk_gripper_dummy/schunk_egu_egk_gripper_dummy/main.py +++ b/schunk_egu_egk_gripper_dummy/schunk_egu_egk_gripper_dummy/main.py @@ -1,6 +1,6 @@ from src.dummy import Dummy -from fastapi import FastAPI, Request +from fastapi import FastAPI, Request, Form from fastapi.middleware.cors import CORSMiddleware from typing import Optional from pydantic import BaseModel @@ -20,15 +20,22 @@ ) -class Message(BaseModel): - message: str - optional: Optional[str] = None +class Update(BaseModel): + inst: str + value: str + elem: Optional[int] = None + callback: Optional[str] = None -@server.put("/") -async def put(msg: Message): - print(msg) - return True +@server.post("/adi/update.json") +async def post( + inst: str = Form(...), + value: str = Form(...), + elem: Optional[int] = Form(None), + callback: Optional[str] = Form(None), +): + msg = Update(inst=inst, value=value, elem=elem, callback=callback) + return dummy.post(msg) @server.get("/adi/{path}") diff --git a/schunk_egu_egk_gripper_dummy/src/dummy.py b/schunk_egu_egk_gripper_dummy/src/dummy.py index 3536681..42216d6 100644 --- a/schunk_egu_egk_gripper_dummy/src/dummy.py +++ b/schunk_egu_egk_gripper_dummy/src/dummy.py @@ -46,6 +46,9 @@ def _run(self) -> None: time.sleep(1) print("Done") + def post(self, msg: dict) -> dict: + return {"result": 0} + def get_info(self, query: dict[str, str]) -> dict: return {"dataformat": 0} # 0: Little endian, 1: Big endian diff --git a/schunk_egu_egk_gripper_dummy/tests/test_dummy.py b/schunk_egu_egk_gripper_dummy/tests/test_dummy.py index 747b44a..e37b4e8 100644 --- a/schunk_egu_egk_gripper_dummy/tests/test_dummy.py +++ b/schunk_egu_egk_gripper_dummy/tests/test_dummy.py @@ -87,3 +87,11 @@ def test_dummy_survives_invalid_data_requests(): query = {"inst": "0x0040", "count": "2"} expected = [] assert dummy.get_data(query) == expected + + +def test_dummy_responds_correctly_to_post_requests(): + dummy = Dummy() + inst = "0x0048" + data = {"inst": inst, "value": "01"} + expected = {"result": 0} + assert dummy.post(data) == expected diff --git a/schunk_egu_egk_gripper_dummy/tests/test_webserver.py b/schunk_egu_egk_gripper_dummy/tests/test_webserver.py index 2c05bfe..91444c1 100644 --- a/schunk_egu_egk_gripper_dummy/tests/test_webserver.py +++ b/schunk_egu_egk_gripper_dummy/tests/test_webserver.py @@ -15,3 +15,9 @@ def test_enum_route_is_available(): def test_data_route_is_available(): client = TestClient(server) assert client.get("/adi/data.json").is_success + + +def test_update_route_is_available(): + client = TestClient(server) + data = {"inst": 0, "value": "0"} + assert client.post("/adi/update.json", data=data).is_success