AI-generated article. The synthetic example below was run locally. It uses mocked responses and makes no network calls.
A scheduled CSV import will sometimes get HTTP 304 instead of a fresh file. If the job treats every successful poll as a new export, a 304 can turn into an empty overwrite. The import also has to keep the old validator until the new file has been committed. Otherwise a failed write can leave the next request asking the server to skip the very file it still needs.
For one fixed export URL, store the last committed ETag alongside the accepted snapshot. Include that exact tag as If-None-Match on the next GET. Preserve quotation marks and any W/ prefix as returned by the origin. The origin can return 304 when the current representation matches the validator. A 304 has no response content. If the export depends on an account, filter, or request header, keep its snapshot and validator separate.
Define what each result does
def next_snapshot(previous, status, body, etag):
if status == 304:
if previous is None:
raise ValueError("304 without a stored snapshot")
return previous
if status != 200:
raise RuntimeError(f"Unexpected export status: {status}")
if not body.startswith(b"order_id,quantity\n"):
raise ValueError("Unexpected CSV header")
return (body, etag)
before = (b"order_id,quantity\nA1,2\n", '"v1"')
assert next_snapshot(before, 304, b"", None) is before
assert next_snapshot(before, 200, b"order_id,quantity\nA2,1\n", '"v2"')[1] == '"v2"'
assert next_snapshot(before, 200, b"order_id,quantity\n", None)[1] is None
for status, data, error in [(503, b"", RuntimeError), (200, b"bad", ValueError)]:
try:
next_snapshot(before, status, data, '"v2"')
except error:
continue
raise AssertionError(f"Expected {error.__name__}")
print("Five synthetic cases passed.")
The header check is intentionally narrow. In production, validate the expected CSV dialect, row structure, and whether a header-only export means zero records for that source. Store the bytes or the corresponding imported rows and the new validator in one durable transaction. If the origin sends a valid 200 without an ETag, accept the data and omit If-None-Match next time. If a 304 arrives with no usable local snapshot, recover with an unconditional GET. Record the poll status separately from the timestamp of the last successful import so monitoring can distinguish an unchanged export from a failed refresh.
Reference: HTTP Semantics, RFC 9110 ยง13.1.2 and ยง15.4.5 on 304.
Top comments (0)