Skip to content
dreamcode
dreamcode
Projects
Dream API
Capstone projectPython
Reward: +600 XP
PROJECT BRIEF

Dream API

A web framework hands your code a path and waits for a response. Write handle_request(path, dream_db) for a small dream service. dream_db maps topics to lists of dream descriptions. Return a dictionary with a status code and a body:

  • /topics returns status 200 and the topic names sorted alphabetically.
  • /dreams/TOPIC returns status 200 and that topic's dreams. Topics match without caring about case.
  • /dreams/TOPIC?limit=N returns at most N dreams.
  • An unknown topic returns status 404 and the body "No dreams found for topic: TOPIC".
  • Any other path returns status 400 and the body "Bad request".

This is exactly the routing and validation a real Flask or FastAPI endpoint does.

Build it step by step
  1. Split off the query string with path.partition("?").
  2. Handle /topics first.
  3. For paths starting with /dreams/, find the topic ignoring case.
  4. Read limit from the query string and slice the list.
  5. Everything else is a 400.
Examples
handle_request('/topics', {'sea': ['Deep blue water'], 'flight': ['Soaring']})
{'status': 200, 'body': ['flight', 'sea']}
handle_request('/dreams/flight', {'flight': ['Soaring over neon clouds', 'Falling gently'], 'sea': ['Deep blue water']})
{'status': 200, 'body': ['Soaring over neon clouds', 'Falling gently']}
project.py
PYTHON
Saved as you type

Tests

0 of 7 passing
  • /topics lists topics
    handle_request('/topics', {'sea': ['Deep blue water'], 'flight': ['Soaring']})
    expected {'status': 200, 'body': ['flight', 'sea']}
  • /dreams/flight
    handle_request('/dreams/flight', {'flight': ['Soaring over neon clouds', 'Falling gently'], 'sea': ['Deep blue water']})
    expected {'status': 200, 'body': ['Soaring over neon clouds', 'Falling gently']}
  • topic in capitals
    handle_request('/dreams/FLIGHT', {'flight': ['Soaring']})
    expected {'status': 200, 'body': ['Soaring']}
  • limit=1
    handle_request('/dreams/flight?limit=1', {'flight': ['Soaring', 'Falling', 'Gliding']})
    expected {'status': 200, 'body': ['Soaring']}
  • unknown topic
    handle_request('/dreams/forest', {'flight': ['Soaring']})
    expected {'status': 404, 'body': 'No dreams found for topic: forest'}
  • unknown path
    handle_request('/weather', {'flight': ['Soaring']})
    expected {'status': 400, 'body': 'Bad request'}
  • limit is not a number
    handle_request('/dreams/flight?limit=lots', {'flight': ['Soaring']})
    expected {'status': 400, 'body': 'Bad request'}
On the line
+600 XP
Pass all 7 tests to claim it.