requests, JSON APIs, and robust fetching

Beginner Data Science Praktikum
Created by Pavel · 21.03.2026 at 01:05 UTC · 1 completed

You write a small script to pull weather or census JSON and everything works until the Wi-Fi hiccups or the API rate-limits you. requests is the usual companion for that story: one line to GET, then raise_for_status() so a 404 page does not get mistaken for JSON, and response.json() once you know the transport succeeded.

When the same host gets dozens of calls, a Session reuses TCP connections so you are not paying the handshake tax every time; when the world is flaky, timeouts on every call and retries for 429/5xx (via HTTPAdapter and urllib3.Retry) turn a brittle demo into something you could run overnight.

The plot twist: HTTP can say 200 OK while the body is an error payload in JSON—transport success is not business logic success—so you still validate shape before indexing and, when you care about reproducibility, snapshot raw responses.

HTTP, URLs, and the query string (?…) summarizes schemes, paths, and query parameters before you wire them into code. Tutorials: [1], timeouts and advanced usage [2].


Sources

University approvals: 0
Tasks
Question 1

After r = requests.get(url, timeout=10), why is r.raise_for_status() recommended before parsing JSON?

Hint

Consider what happens on 404 or 500.

Question 2

What is the main benefit of requests.Session() when calling the same host many times?

Hint

Think about connection setup cost.

Question 3

Implement hourly_temperature_sum(payload: str) -> float where payload is JSON text for an object like Open-Meteo's hourly block: {"temperature_2m": [1.0, 2.0, 3.0]}. Return the sum of the list (empty list → 0.0). Use json.loads.

Submit the function; expression mode passes JSON strings.

Hint

json.loads returns a dict; sum() works on lists of numbers.

Starter code is prefilled; replace TODO blocks with your solution.
2 test cases will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Data Science Praktikum
  • Difficulty: Beginner
  • Completed: 1 users
Creator
Pavel
Pavel