Requests Library Deep Dive - 8

Continuing the study of the requests module. Today, I am diving into requests/hooks.py, a lightweight but powerful module that allows users to attach custom logic to the request/response lifecycle.

Key Concepts in requests/hooks.py

Purpose

Hooks provide a way to execute user-defined functions during specific parts of the requests/response lifecycle. This makes it possible to customize or extend the behavior of requests without modifying its core code.

Hook Events

  • The primary hook event is response, which lets you manipulate or inspect the Response object before it is returned to the user.

Structure of Hooks

default_hooks: Defines the default structure of hooks.

1
default_hooks = {'response': []}

Each key corresponds to a specific lifecycle event, and the value is a list of functions to be executed.

Dispatching Hooks

dispatch_hooks: This function executes all functions associated with a specific hook.

1
2
3
4
5
def dispatch_hook(key, hooks, hook_data, **kwargs):
    if hooks.get(key):
        for hook in hooks[key]:
            hook_data = hook(hook_data, **kwargs)
    return hook_data
  • key: The hook event (e.g., 'response').
  • hooks: A dictionary containing user-defined hooks.
  • hook_data: The data passed to the hook (e.g., a Response object).
  • kwargs: Optional additional arguments.

Using Hooks with Response

Hooks are typically used to inspect or modify a Response before it’s returned to the user. For example:

1
2
3
4
5
6
7
8
import requests

def response_hook(response, *args, **kwargs):
    print(f'Hook: Status Code - {response.status_code}')
    return response

hooks = {'response': [response_hook]}
response = requests.get('https://example.com', hooks=hooks)
  • The response_hook function runs after the request is completed but before the Response is returned.
  • You can add multiple hooks to the response event, and they will be executed in order.

comments powered by Disqus