Visual intuition: API call architecture¶
Caption: High-level API gateway routing to services with auth, rate-limiting, cache, and shared DB.
Alt: Flowchart showing client -> gateway -> auth/rate limit -> services -> shared database and cache.
REST and GraphQL APIs — practical patterns + safe browser demos¶
Business framing: APIs connect your models and services to users and other systems. This notebook focuses on robust request handling, authentication patterns, rate limits, and safe in-browser examples that simulate API behavior.
Learning objectives¶
Design simple, testable API clients and small server-side handlers.
Handle failures, retries, and rate limits in a predictable way.
Use deterministic, Pyodide-safe examples to show concurrency and backoff strategies.
Pyodide-safe simulated API client + retry/backoff pattern¶
Concurrency: parallel simulated requests (IO-bound)¶
MCQ¶
Q: When a third-party API occasionally returns HTTP 500, what is the simplest robust client strategy?
A) Fail immediately and surface error to user
B) Retry with exponential backoff and a capped number of attempts
C) Ignore errors and return stale data
(Answer: B)
Exercises¶
Modify
get_with_retryto implement exponential backoff and jitter.Add a
rate_limitwrapper that enforces N calls per second for a wrapped client function.(Stretch) Implement a small
circuit_breakerthat opens after several failures and resets after a cooldown.
Notes: Examples are deterministic and safe for in-browser execution. I preserved the notebook’s original code cells and any existing content.
REST and GraphQL APIs¶
# Your code here