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

Filtered User Response

easy—No data yet

Problem statement

Every user record in the register carries a password hash and three other values nobody outside the service may see, and the endpoints hand the whole record back. Declare the public shape and answer through it, so the three endpoints report the same six fields and the hidden four never leave the service, not in a response and not in the published description.

Sample Data

The register in app/store.py holds five users under the ids 1_, _2, 3_, _5 and 8_, so _4 and 99 belong to nobody. One of them is not active. Here is one stored record, in full:

{
  "id": 3,
  "username": "alan.turing",
  "email": "alan.turing@directory.io",
  "full_name": "Alan Turing",
  "role": "member",
  "is_active": false,
  "password_hash": "pbkdf2$sha256$120000$2f6b95cd",
  "failed_login_count": 7,
  "last_login_ip": "10.0.0.31",
  "internal_notes": "account parked while the transfer clears"
}

API Endpoints to Implement

PublicUser

The public shape, in app/models/public_user.py, holding exactly six fields and no others:

  • id, a whole number.
  • username, text.
  • email, text.
  • full_name, text.
  • role, text.
  • is_active, a boolean.

password_hash, failed_login_count, last_login_ip and internal_notes are the hidden four. They may not appear in any response, and they may not appear anywhere in the published description either.

GET /users

Answers 200 with every user of the register, in id order, each through the public shape. list_users in app/services/users.py returns them already ordered.

GET /users/{user_id}

  • user_id comes from the URL as a whole number.
  • Answers 200 with that user through the public shape, looked up with find_user.
  • A user id the register does not hold is answered with 404 and the body {"detail": "User 4 was not found"}, naming the id that was asked for.

POST /users

  • Takes the body as a UserCreate and hands it to create_user, which assigns the next id, sets the role and the hash, and stores the record.
  • Answers 201, and 201 only, with the created user through the public shape. The password that was sent, and the hash derived from it, are both absent from the answer.
  • Because UserCreate declares only what a caller may supply, a body that also carries an id, a role, an is_active or a password_hash is stored with those values ignored.

Rejected requests

A user_id in the URL that is not a whole number is answered with 422 and one detail entry carrying loc of ["path", "user_id"], the input that was received, and the type and msg reported for the rule. A registration missing a required value, or carrying one of the wrong kind, is answered the same way with the loc inside the body.

The public shape must be declared once and used as the response shape of all three operations rather than assembled by hand, since the service publishes it at GET /openapi.json, where the three operations point at one named schema, that schema names the six public fields with their kinds, and 201 is the only code the registration declares.

Sample Requests & Responses

GET /users/3

Response (200 OK):

{
  "id": 3,
  "username": "alan.turing",
  "email": "alan.turing@directory.io",
  "full_name": "Alan Turing",
  "role": "member",
  "is_active": false
}

POST /users

Request:

{
  "username": "hedy.lamarr",
  "email": "hedy.lamarr@directory.io",
  "full_name": "Hedy Lamarr",
  "password": "frequency-hopping"
}

Response (201 Created):

{
  "id": 9,
  "username": "hedy.lamarr",
  "email": "hedy.lamarr@directory.io",
  "full_name": "Hedy Lamarr",
  "role": "member",
  "is_active": true
}

GET /users/4

Response (404 Not Found):

{
  "detail": "User 4 was not found"
}

More in Response Models and Status Codes

See the module
Correct Creation CodesThis problem covers answering each request with the code its outcome deserves, so a caller can tell a newly created thing from a plain success and a removal from a thing that was never there.—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.