Command Palette

Search for a command to run...

Hectal
Phase 15: Python for APIs, Services and Tools
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

  1. returns the dictionary for an existing user
  2. returns None on 404
  3. raises HTTPStatusError on 500
  4. a 403 is not swallowed as None
  5. 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.