StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 4 · Response Models and Status Codes

Correct Creation Codes

easy—No data yet

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.

More in Response Models and Status Codes

See the module
Filtered User ResponseThis problem covers deciding which parts of a stored user are safe to hand back, so passwords and internal notes stay inside the service while callers still get the details they need.—No data yeteasySerialization TuningThis problem covers controlling how much of a record goes onto the wire, so a listing reports only what was actually filled in and a summary reports a fixed handful of fields for everything.—No data yeteasyAccounts API Output ContractThis problem covers settling what an accounts API puts on the wire, so a list stays lean, a single account comes back complete, every outcome carries the right code, and the values meant to stay inside never leave.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.