payload_loader module is the single owner of the payload pipeline. One JSON payload moves through this order: session memo, persistent cache, CDN fetch, validation, then memo and cache write-back. Session and the async fetch layer route their fetches through this module.
Overview
The module contains one protocol, two transport implementations, one loader class, and one factory function:HttpTransport— the HTTP seam, defined as atyping.Protocol.NiquestsTransport— the production transport, built on the shared niquests session.InMemoryTransport— a test transport that serves payloads from an in-memory mapping.PayloadLoader— the two-method interface (getfor one path,get_manyfor async fan-out) that hides the whole pipeline.get_url_loader()— a thread-safe factory for the shared loader used for absolute-URL payloads.
Session constructs one PayloadLoader per session and injects its own memo, cache callables, and overridable fetch delegates (Session._fetch_from_cdn and Session._fetch_from_cdn_fast). CDN source ordering and fallback live in tif1.cdn.CDNManager, not in this module. The loader supplies only the per-URL fetch callable built on the transport.
Validation failures always raise. There is no patched-callable escape hatch.
HttpTransport
HttpTransport is a runtime-checkable typing.Protocol. Implementations fetch one URL and return the parsed JSON object.
DataNotFoundError— the resource does not exist (HTTP 404).NetworkError— the request failed at the transport level.InvalidDataError— the response body was not a JSON object.
NiquestsTransport
NiquestsTransport is the production transport. It sends every request through the pooled niquests session owned by tif1.http_session.
Methods
get_json
url with the pooled niquests session and parse the JSON body.
str
required
Absolute URL to fetch.
float | None
default:"None"
Request timeout in seconds. When
None, the transport reads the timeout value from the configuration. The configuration default is 30.- A transport-level failure raises
NetworkErrorwith the URL and no status code. - HTTP 404 raises
DataNotFoundErrorwith the URL. - Any other HTTP error status raises
NetworkErrorwith the URL and the status code. - A response body that decodes to a non-dict JSON value raises
InvalidDataError.
InMemoryTransport
InMemoryTransport serves payloads from an in-memory mapping. Use it in tests to run the full pipeline without network access. Every requested URL is recorded in the calls attribute.
Constructor
Mapping[str, Any] | None
default:"None"
Mapping of URL fragment to payload. A fragment can be a path, a full URL, or any substring of a URL. A value can be a dict, any other payload value, an
Exception class or instance to raise, or a callable. A callable takes the URL and returns any of those value types.Any
default:"sentinel"
Fallback entry used when no mapping matches. When unset, an unmatched URL raises
DataNotFoundError.Methods
add
get_json
url against the in-memory mapping and record url in calls. Raises DataNotFoundError when no mapping matched and no default was provided. Raises the matched exception when the matched entry specifies one.
PayloadLoader
PayloadLoader owns the pipeline order: memo, cache, CDN fetch, validation, write-back. The loader is parameterized by session coordinates (year, gp, session) that build cache keys and CDN URLs. All storage and the fetch step are injectable as callables.
Constructor
int | None
default:"None"
Season year.
None is valid only for absolute-URL loaders.str | None
default:"None"
Grand Prix identifier, URL-encoded.
str | None
default:"None"
Session identifier, URL-encoded.
HttpTransport | None
default:"None"
HTTP seam implementation. Defaults to
NiquestsTransport().CDNManager | None
default:"None"
CDN fallback manager. Defaults to the process-global
tif1.cdn.get_cdn_manager() instance, resolved at call time. Also settable through the cdn_manager property.SessionMemo | None
default:"None"
Per-session memo tier used by the default memo callables.
bool
default:"True"
Gate for the default persistent-cache callables.
Callable[..., Any] | None
default:"None"
Override for the fetch step, with signature
fetch(path, *, fast: bool). Defaults to PayloadLoader.fetch_from_cdn.Callable[[str], dict[str, Any] | None] | None
default:"None"
Override for the memo read,
path -> dict or None.Callable[[str, dict[str, Any]], None] | None
default:"None"
Override for the memo write,
(path, dict).Callable[[str], Any | None] | None
default:"None"
Override for the persistent-cache read,
cache key -> data or None.Callable[[str, dict[str, Any]], None] | None
default:"None"
Override for the persistent-cache write,
(cache key, dict).Methods
get
str
required
Session-relative payload path, for example
"drivers.json".bool
default:"True"
Run payload validation before returning. Validation is path-based and pydantic-backed; it is gated by the
validate_data, validate_lap_times, and validate_telemetry configuration values.bool
default:"True"
Read the persistent cache before a network fetch.
bool
default:"True"
Write fetched payloads to the persistent cache.
bool
default:"False"
Skip per-source retry and backoff delays on the CDN fetch.
DataNotFoundError when the payload does not exist. Raises InvalidDataError when the payload is not a JSON object or failed validation. Raises NetworkError when all CDN sources failed. Validation failures always raise.
A payload found in the memo or cache returns without a network request. A cached dict is also written to the memo.
fetch_from_cdn
CDNManager.try_sources; this method only builds the per-URL fetch callable on the configured transport. When fast is False, the fetch callable is wrapped in retry_with_backoff using the max_retries, retry_backoff_factor, and retry_jitter configuration values. When fast is True, the zero-retry path is used.
Raises DataNotFoundError when the payload does not exist (HTTP 404) and NetworkError when every CDN source failed.
get_url
timeout default comes from the configuration.
get_many
tif1.async_fetch.fetch_multiple_async. Every keyword argument defaults to the configured value when None. The returned list has one entry per requested path; a failed fetch produces None at its position.
cache_key
"{year}/{gp}/{session}/{path}".
cdn_manager
tif1.cdn.get_cdn_manager().
get_url_loader
PayloadLoader for absolute-URL payloads. The loader is created on first call under a lock, so the function is safe to call from multiple threads. tif1.events uses this loader to fetch f1schedule year payloads from their CDN URLs.
Examples
Offline pipeline run with InMemoryTransport
This example runs the fullget pipeline without network access. The enable_cache=False argument keeps the run off the persistent cache. Validation is disabled because the fixture payload is not a real drivers.json document.
Absolute-URL fetch
This example requires network access.Related APIs
- Core API:
Sessionand its overridable_fetch_from_cdndelegates - CDN API:
CDNManagersource ordering and fallback - Cache API:
SessionMemoand the persistent SQLite cache - Async Fetch API:
fetch_multiple_asyncused byget_many - Config API: timeout and retry configuration values
- Validation API: the pydantic validators used by the pipeline