repositori / bagas / requests

requests public ready

https://github.com/psf/requests commit 3c7baaa 48 berkas · 374 potongan 2026-11-21T15:17:50Z
48berkas
36berkas python
374potongan
python 36 markdown 5 yaml 2 text 2 toml 1 css 1 html 1
src/requests/hooks.pypython 48 baris ditampilkan
1
"""
2
requests.hooks
3
~~~~~~~~~~~~~~
4
5
This module provides the capabilities for the Requests hooks system.
6
7
Available hooks:
8
9
``response``:
10
The response generated from a Request.
11
"""
12
13
from __future__ import annotations
14
15
from collections.abc import Callable, Iterable
16
from typing import TYPE_CHECKING, Any
17
18
if TYPE_CHECKING:
19
from . import _types as _t
20
from .models import Response
21
22
HOOKS: list[str] = ["response"]
23
24
25
def default_hooks() -> dict[str, list[_t.HookType]]:
26
return {event: [] for event in HOOKS}
27
28
29
# TODO: response is the only one
30
31
32
def dispatch_hook(
33
key: str,
34
hooks: _t.HooksInputType | None,
35
hook_data: Response,
36
**kwargs: Any,
37
) -> Response:
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)
41
if hook_list:
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
48
return hook_data