5
This module provides the capabilities for the Requests hooks system.
10
The response generated from a Request.
13
from __future__ import annotations
15
from collections.abc import Callable, Iterable
16
from typing import TYPE_CHECKING, Any
19
from . import _types as _t
20
from .models import Response
22
HOOKS: list[str] = ["response"]
25
def default_hooks() -> dict[str, list[_t.HookType]]:
26
return {event: [] for event in HOOKS}
29
# TODO: response is the only one
34
hooks: _t.HooksInputType | None,
38
"""Dispatches a hook dictionary on a given piece of data."""
39
hooks_dict = hooks or {}
40
hook_list: Iterable[_t.HookType] | _t.HookType | None = hooks_dict.get(key)
42
if isinstance(hook_list, Callable):
43
hook_list = [hook_list]
44
for hook in hook_list:
45
_hook_data = hook(hook_data, **kwargs)
46
if _hook_data is not None:
47
hook_data = _hook_data