|
| 1 | +Conditional HTTP Requests & Cache Validators |
| 2 | +============================================ |
| 3 | + |
| 4 | +``http_request`` never sends ``If-None-Match`` / ``If-Modified-Since`` nor reads |
| 5 | +``Cache-Control``, so every poll re-downloads an unchanged resource. This |
| 6 | +extracts caching validators from a response, parses ``Cache-Control``, decides |
| 7 | +freshness, and conditions the next request so the server can answer ``304 Not |
| 8 | +Modified``. |
| 9 | + |
| 10 | +Pure standard library; imports no ``PySide6``. Freshness takes an explicit age |
| 11 | +(no wall clock), so the logic is fully deterministic in CI. |
| 12 | + |
| 13 | +Headless API |
| 14 | +------------ |
| 15 | + |
| 16 | +.. code-block:: python |
| 17 | +
|
| 18 | + from je_auto_control import ( |
| 19 | + store_validators, conditioned_call, is_fresh, is_not_modified, |
| 20 | + parse_cache_control, build_call, |
| 21 | + ) |
| 22 | +
|
| 23 | + response = http_request(url) |
| 24 | + validators = store_validators(response) |
| 25 | + if is_fresh(validators, age_seconds=now - stored_at): |
| 26 | + use_cached() |
| 27 | + else: |
| 28 | + revalidation = conditioned_call(build_call(url), validators) |
| 29 | + fresh = perform(revalidation) |
| 30 | + if is_not_modified(fresh): |
| 31 | + use_cached() # 304 → the stored body is still valid |
| 32 | +
|
| 33 | +``store_validators`` pulls ``etag`` / ``last_modified`` / ``date`` and the parsed |
| 34 | +``cache_control`` from a response. ``parse_cache_control`` turns the header into |
| 35 | +a directive dict (``max-age`` as an int, flags as ``True``). ``conditioned_call`` |
| 36 | +adds ``If-None-Match`` / ``If-Modified-Since`` to a ``build_call`` dict. |
| 37 | +``is_fresh`` reports whether a cached entry is still fresh for a given age |
| 38 | +(``no-store`` / ``no-cache`` are never fresh). ``is_not_modified`` detects a |
| 39 | +``304`` response. |
| 40 | + |
| 41 | +Executor commands |
| 42 | +----------------- |
| 43 | + |
| 44 | +``AC_parse_cache_control`` returns ``{directives}`` for ``headers``; |
| 45 | +``AC_store_validators`` returns ``{validators}`` for a ``response``. Both are |
| 46 | +exposed as MCP tools (``ac_parse_cache_control`` / ``ac_store_validators``) and |
| 47 | +as Script Builder commands under **Data**. |
0 commit comments