Calling APIs with requests

Calling APIs with requests: Getting Data with requests, When a Request Fails, Saved Copies: Working Offline.

Updated

  1. Getting Data with requests (Apprentice): The requests package sends a program's request to a web API with requests.get, a User-Agent header and a timeout, and gives back a response whose status_code says how it went and whose json() turns the answer into dictionaries and lists, which for the National Weather Service takes two steps, the point's forecast address and then the forecast.
  2. When a Request Fails (Journeyman): A web request can fail three ways, no connection, an error status code or no answer in time, and a program handles each with requests' own exceptions, raise_for_status() turning a 4xx or 5xx answer into an HTTPError, a friendly message instead of a traceback, and one more try when the network may come back.
  3. Saved Copies: Working Offline (Journeyman): A program that calls a web API can save each answer to a JSON file with the date it was saved, fall back to that copy when a request fails, and take the function that fetches as a parameter, so tests can hand in a stand-in and never touch the network.