Python APIs and JSON
$24.99
Buy on Amazon View on Apple BooksPrefer it in print? See every format on Amazon →
Price on Apple Books. Amazon’s price may differ.
- Published
Description
A script that fetches figures from a web service works on the day it is written and fails on a day nobody chose: a token that expired overnight, a list that grew past one page, a reply that turned out to be a web page, a limit nobody mentioned. This guide is about those days. It shows how to write the Python side of the conversation with a web service so that it fails in ways you can read, repeat safely and fix.
Every example runs against a small practice server that you start on your own computer, built from the standard library and listed in full, so each request, header and failure can be reproduced exactly. Nothing needs installing and no call leaves your machine. The book starts with the raw text of a request and a reply, then builds one client module at a time, from a single call function to a finished client with tests, retries, a token provider and an audit script that ends with a clear message and an exit code.
Inside you will find:
• Status codes as decisions: what a client does for each of the codes that matter
• The JSON module in depth: numbers, repeated names, encodings and how to read a parse error
• A call function that keeps four kinds of failure apart: error status, connection, timeout and unreadable body
• Bearer tokens, expiring tokens with one safe refresh, and a scrubber that keeps secrets out of logs
• Page numbers, link headers and cursors, with a check that proves you collected every record, and a job that can resume
• Timeouts, deadlines, Retry-After, backoff with jitter and a pacer, with the worst-case wait computed
• A validator that turns messy records into typed ones, quarantines the rest and reports drift
• Forms, JSON bodies, merge patches, uploads and idempotency keys for the reply that gets lost
• Sixteen tests that need no waiting, using scripted failures and a replaceable clock
• A seven-day plan for turning the workshop into a client for a service you use
Written for Python 3.10 and later; every listing was run and its output pasted. Use only services you are entitled to use, with credentials issued to you and within their stated limits. This is an independent guide, written as general information. If a script will touch a system that holds other people's data, money or health records, involve the people who run that system first.