Python · Exercise 1 of 6Next
Intermediate
A client function with a fake server
Write a function get_user(client, user_id). It sends a GET request to the path /users/<user_id> (for id 7, the path /users/7) using the httpx.Client it is given.
If the call works, return the reply's JSON as a dictionary. If the server answers 404 (not found), return None. For any other error status such as 500, call raise_for_status() so it raises httpx.HTTPStatusError.
What Check tests
- returns the dictionary for an existing user
- returns None on 404
- raises HTTPStatusError on 500
- a 403 is not swallowed as None
- calls the path /users/<id>
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.