Designing the API

You have been on the other side of this. The APIs & Datasets course had you consume real APIs and record what was missing: undocumented paging, counts that lied, errors returned as HTML, no way to tell an error from data.

Design the one you wished you had received. Wrap results in an object so there is room for a count. Use status codes properly. Return errors as JSON with the same shape every time. Name the fields you expose rather than serializing whole records.

Authorization belongs in the query, not after it. From the Django course: @login_required alone means any signed-in person can act on anyone's data by changing a number in the URL.

Filter by the owner so the wrong thing cannot be fetched at all. There is then no check to forget, and it fails as a 404 rather than confirming that the record exists but belongs to somebody else.

◈ Graded Challenge

Write visible_to(projects, user) returning the titles a given viewer may see, sorted alphabetically. projects is a list of dictionaries with title, owner and visible. user is a dictionary with name and is_staff, or None for a signed-out visitor. Everyone may see visible projects. Only the owner or staff may see an invisible one.

Name it exactly: visible_to(projects, user)

Python3 visible + 4 hidden
loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Failing Usefully →
← Back