10 A new endpoint
Build it yourself. Read the tutorial is the other way to read this part.
This walk goes through rahuldave/cheesecracker on GitHub. This part starts from the code at step-09. The code before this part
If you start at this part, make your clone as Get the project says, and then run this command. It puts in your clone the files that this part starts from. A file that the project deleted before this part stays, so delete it if git check lists it:
git fetch --quiet --tags --force origin; git rm -rq --cached --ignore-unmatch -- :/ && git restore --source step-09 --staged --worktree -- :/just setupThe Cheese App needs to add two numbers. The endpoint /add/ takes x and y, and returns their sum. An integration
test calls it. Then every check of the project runs again, on this machine, in the dev image, and against a new run
image.
Apply the patch to src/api-service/api/service.py, from the top of the project:
curl -fsSL https://github.com/rahuldave/cheesecracker/compare/0701e9244ab2384538eda4d623e1d047ff3f8258...e706255d9c0fee3b6fded40376e7ac8829d1654a.diff | git apply --include=src/api-service/api/service.pyApply the patch to tests/integration/test_api.py, from the top of the project:
curl -fsSL https://github.com/rahuldave/cheesecracker/compare/0701e9244ab2384538eda4d623e1d047ff3f8258...e706255d9c0fee3b6fded40376e7ac8829d1654a.diff | git apply --include=tests/integration/test_api.pyApply both patches. Read the function add_numbers() at the end of service.py, and test_add_endpoint() at the end
of test_api.py.
just checkThe checks pass on this machine. The integration tests are now eleven.
just docker-testThe same checks pass in the dev image.
The run image still has the old code, because an image is built once and does not change. Build it again, and start its server.
just docker-build run
just docker-serve
curl -s "localhost:9000/add/?x=10&y=20"The server in Docker answers {"result":30.0}.
just test-system -qThe system tests print 5 passed against the new run image.
just docker-stopCheck your files against this part. The command prints nothing when they match, and otherwise shows what differs:
git check e706255d9c0fIf you are stuck, this command puts the files of this part in place of yours. A file that you made yourself stays:
git fetch --quiet --tags --force origin; git restore --source step-09 --staged -- :/ && git restore --source step-10 --staged --worktree -- :/What changed
src/api-service/api/service.py: the endpoint/add/. 6 lines added. The file- look at
return {"result": x + y}
- look at
tests/integration/test_api.py: the test of/add/. 6 lines added. The file- look at
response = client.get("/add/?x=10&y=20")
- look at
The patch to src/api-service/api/service.py
@@ -44,3 +44,9 @@ async def calculate_euclidean_distance(x: float = 1, y: float = 2):
"distance": result,
"message": "This is a very long line that exceeds 120 characters blah",
}
+
+
+@app.get("/add/")
+async def add_numbers(x: float = 0, y: float = 0):
+ """Add two numbers"""
+ return {"result": x + y}The patch to tests/integration/test_api.py
@@ -87,3 +87,9 @@ async def test_api_health_check():
assert response.status_code == 200
# API should respond quickly
assert response.elapsed.total_seconds() < 1 if hasattr(response, "elapsed") else True
+
+
+def test_add_endpoint():
+ response = client.get("/add/?x=10&y=20")
+ assert response.status_code == 200
+ assert response.json()["result"] == 30This part ends at the code at step-10. The code after this part