Problem statement
The three alert endpoints take nothing and answer nothing. Wire them up so each reports the code its outcome deserves: a created alert with 201, a removal with 204 and an empty body, a missing alert with 404, and each of those codes declared rather than left at the default.
Sample Data
The register in app/store.py holds five alerts under the ids 1_, _2, 3_, _5 and 8_, so _4 and 99 belong to nobody. The next alert created takes the id 9. Here is one alert record:
{
"id": 1,
"name": "Cpu Saturation",
"metric": "cpu_percent",
"threshold": 90.0,
"enabled": true
}
API Endpoints to Implement
The three handlers live in app/api/routes/alerts.py. Their helpers, add_alert, find_alert and remove_alert, are in app/services/alerts.py.
POST /alerts
- Takes the body as an AlertCreate, the draft holding name, metric, threshold and enabled, and hands it to add_alert.
- Answers 201, not the default success code, with the stored alert: the four drafted values plus the id it was given.
GET /alerts/{alert_id}
- alert_id comes from the URL as a whole number.
- Answers 200 with that alert, looked up with find_alert.
- An alert id the register does not hold is answered with 404 and the body {"detail": "Alert 4 was not found"}, naming the id that was asked for.
DELETE /alerts/{alert_id}
- Removes that alert with remove_alert and answers 204, not the default success code, with a body that is empty rather than null.
- An alert id the register does not hold is answered with the same 404 as the read, so deleting the same alert twice reports the second attempt as not found.
Rejected requests
An alert_id in the URL that is not a whole number, and a draft missing one of its four values, are each answered with 422 and one detail entry per broken rule, carrying loc of ["path", "alert_id"] or ["body", "field name"], the input that was received, and the type and msg reported for the rule.
The codes must be declared on the operations rather than only returned at run time, since the service publishes them at GET /openapi.json, where the creation reports 201 and the removal 204 with neither reporting 200, the removal declares no response content at all, the alert id is published as a required whole number in the path, and the creation requires a JSON body.
Sample Requests & Responses
POST /alerts
Request:
{
"name": "Queue Backlog",
"metric": "queue_depth",
"threshold": 500.0,
"enabled": true
}
Response (201 Created):
{
"id": 9,
"name": "Queue Backlog",
"metric": "queue_depth",
"threshold": 500.0,
"enabled": true
}
GET /alerts/4
Response (404 Not Found):
{
"detail": "Alert 4 was not found"
}
DELETE /alerts/2
Response (204 No Content), with no body at all.
