http and apis — making the call9 / 9
get, status, json — the call ai makes 100 times a day
Checkpoint
One last thing before we move on. Same surface as a write step — but the lesson doesn't complete until this passes.
Final drill. Below is a function fetch that simulates an API call —
it returns a (status_code, body) tuple. Write a function safe_fetch
that:
- Calls
fetch(user_id). - If the status is
200, returnsbody["name"]. - For any other status, returns the string
"error".
Then the script will call safe_fetch(7) and safe_fetch(404) and
print both. Expected output:
maya
error
⌘↵ runs the editor.
Booting Python…
Output
[promptdojo:~]$ _