57.8 Mocking HTTP in Tests with responses and httpretty

Testing HTTP interactions presents a unique challenge. Unlike many parts of an application, you cannot control the external server’s behavior, its response time, or its availability. Relying on live APIs for tests leads to a fragile, slow, and non-deterministic test suite. The solution is to intercept HTTP requests at the library level and return predefined responses, a practice known as mocking. For the modern httpx library, two of the most robust tools for this task are the responses library and httpretty.

57.7 Uploading Files and Multipart Form Data

Uploading files and sending multipart form data are fundamental operations in web communication, often used for submitting forms that include both textual data and binary file content. The multipart/form-data encoding type, defined in RFC 7578, is designed for this exact purpose. It allows multiple pieces of data, each with its own content type and name, to be sent as a single HTTP request body, separated by a unique boundary string.

57.6 httpx: Async-Capable HTTP Client

The httpx library is a modern, feature-rich HTTP client for Python that supports both synchronous and asynchronous operations. It was designed to address limitations in the popular requests library, most notably the lack of native async/await support. Built to be a next-generation client, it provides a clean, intuitive API that will feel familiar to requests users while offering significant performance benefits for I/O-bound applications through its async capabilities. Its design philosophy centers on being versatile, supporting HTTP/1.1 and HTTP/2, and providing comprehensive features like connection pooling, SSL verification, proxies, cookies, and streaming.

57.5 Streaming Large Responses

When dealing with large HTTP responses—such as multi-gigabyte files, extensive log dumps, or endless streaming data feeds—downloading the entire content into memory before processing it is often impractical and can lead to excessive memory consumption, application instability, or even crashes. The solution to this problem is to stream the response content, processing it in smaller, manageable chunks as it is received from the network, rather than waiting for the complete payload.

57.4 Timeouts, Retries, and the urllib3 Adapter

When working with HTTP clients in Python, managing timeouts and implementing retry logic are critical for building robust, production-ready applications. The httpx library provides sophisticated mechanisms for handling these concerns, building upon concepts from the widely-used urllib3 library. Understanding these mechanisms is essential because network operations are inherently unreliable—connections can drop, servers can become unresponsive, and temporary glitches are common. Without proper timeout and retry configurations, your application might hang indefinitely or fail unnecessarily.

57.3 Authentication: Basic, Digest, Bearer, OAuth

Authentication is a cornerstone of interacting with modern web APIs and services. It is the process of proving your identity to a server, which then grants you permission to access specific resources. The httpx library provides robust, built-in support for the most common authentication schemes, streamlining the process of making authenticated requests. Understanding the nuances of each method is crucial for building secure and effective API clients. Basic Authentication Basic Authentication is one of the simplest and most widely supported methods. It involves sending a username and password with each request. The credentials are concatenated with a colon (username:password), base64-encoded, and placed in the Authorization header.

57.2 Sessions: Connection Pooling and Persistent Headers

When making multiple HTTP requests to the same host, creating a new connection for each request is highly inefficient. This process involves a three-way TCP handshake, potential TLS negotiation, and then a tear-down for every single operation. HTTP sessions solve this problem by maintaining a pool of persistent connections that can be reused for multiple requests, dramatically reducing latency and overhead. The httpx library provides a powerful Client object to manage these sessions, offering connection pooling, cookie persistence, and shared configuration.

57.1 requests.get, post, put, delete: The Basics

The requests library provides a set of straightforward and intuitive methods for making HTTP requests, mirroring the verbs of the HTTP protocol itself. The get(), post(), put(), and delete() functions are the primary entry points for interacting with web services. Each function returns a Response object, which contains all the information returned by the server, from the status code and headers to the actual body of the response. The GET Request The requests.get() function is used to retrieve information from a given server using a URI (Uniform Resource Identifier). By definition, a GET request should only retrieve data and should have no other effect on the data. The optional params keyword argument allows you to send a dictionary or bytes to be encoded into the query string of the URL, which is the proper way to pass data for a GET request.

— joke —

...