2026-02-05 15:57:52 +08:00
|
|
|
"""Per-request context shared across layers.
|
|
|
|
|
|
|
|
|
|
We use `contextvars` so the transport layer (URL builder) can pass small bits of
|
|
|
|
|
state to the handler layer without changing existing return types.
|
|
|
|
|
|
|
|
|
|
This is intentionally minimal; only add fields that are safe and cheap to carry
|
|
|
|
|
per request.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import contextvars
|
2026-03-05 02:07:34 +08:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from src.services.provider.fingerprint import FingerprintProfile
|
2026-02-05 15:57:52 +08:00
|
|
|
|
|
|
|
|
_selected_base_url: contextvars.ContextVar[str | None] = contextvars.ContextVar(
|
|
|
|
|
"provider_selected_base_url",
|
|
|
|
|
default=None,
|
|
|
|
|
)
|
2026-03-05 02:07:34 +08:00
|
|
|
_current_fingerprint: contextvars.ContextVar[FingerprintProfile | None] = contextvars.ContextVar(
|
|
|
|
|
"provider_current_fingerprint",
|
|
|
|
|
default=None,
|
|
|
|
|
)
|
2026-02-05 15:57:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_selected_base_url(url: str | None) -> None:
|
|
|
|
|
_selected_base_url.set(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_selected_base_url() -> str | None:
|
|
|
|
|
return _selected_base_url.get()
|
|
|
|
|
|
|
|
|
|
|
2026-03-05 02:07:34 +08:00
|
|
|
def set_current_fingerprint(fp: FingerprintProfile | None) -> None:
|
|
|
|
|
_current_fingerprint.set(fp)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_current_fingerprint() -> FingerprintProfile | None:
|
|
|
|
|
return _current_fingerprint.get()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
"get_current_fingerprint",
|
|
|
|
|
"get_selected_base_url",
|
|
|
|
|
"set_current_fingerprint",
|
|
|
|
|
"set_selected_base_url",
|
|
|
|
|
]
|