Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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

  1. Modify get_with_retry to implement exponential backoff and jitter.

  2. Add a rate_limit wrapper that enforces N calls per second for a wrapped client function.

  3. (Stretch) Implement a small circuit_breaker that 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