

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:
/topicsreturns status 200 and the topic names sorted alphabetically./dreams/TOPICreturns status 200 and that topic's dreams. Topics match without caring about case./dreams/TOPIC?limit=Nreturns 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
- Split off the query string with
path.partition("?"). - Handle
/topicsfirst. - For paths starting with
/dreams/, find the topic ignoring case. - Read
limitfrom the query string and slice the list. - 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 topicshandle_request('/topics', {'sea': ['Deep blue water'], 'flight': ['Soaring']})expected {'status': 200, 'body': ['flight', 'sea']}
- •/dreams/flighthandle_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 capitalshandle_request('/dreams/FLIGHT', {'flight': ['Soaring']})expected {'status': 200, 'body': ['Soaring']}
- •limit=1handle_request('/dreams/flight?limit=1', {'flight': ['Soaring', 'Falling', 'Gliding']})expected {'status': 200, 'body': ['Soaring']}
- •unknown topichandle_request('/dreams/forest', {'flight': ['Soaring']})expected {'status': 404, 'body': 'No dreams found for topic: forest'}
- •unknown pathhandle_request('/weather', {'flight': ['Soaring']})expected {'status': 400, 'body': 'Bad request'}
- •limit is not a numberhandle_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.
