Data-source adapter development
Operational adapters provide one public, fail-closed interface:
class SourceAdapter(Protocol):
source_id: str
adapter_version: str
dataset: str
def collect(self) -> SourceResult:
...
collect() is an atomic fetch-and-validate transaction. A successful result carries normalized
records, source outcome, authority, dataset ID, redacted URL, fetch time, adapter/schema version,
content SHA-256, and bounded retry metrics. Keeping fetch and validation atomic prevents callers
from accidentally publishing an unvalidated raw capture.
Required invariants
source_idis the authority's stable dataset ID and matchesresult.provenance.dataset_id.adapter_versionidentifies the adapter/schema contract and matchesresult.provenance.schema_version.datasetis the stable MinxiongHydroCast product name and matchesresult.dataset.fetched_atis a timezone-aware ISO-8601 timestamp with source provenance.content_sha256hashes the raw response bytes or a documented canonical transaction.- schema drift raises
SourceSchemaErrorand never activates a scraper fallback. - transport/authentication/rate-limit failures raise
SourceRequestError; only those failures may use an explicitly degraded fallback. - valid empty products use
outcome=emptyonly where the product contract permits it. - stale products use
outcome=staleand cannot satisfy operational readiness.
Contract test
Use a deterministic fixture transport; do not call an authority in the ordinary unit test suite.
result = adapter.collect()
validate_adapter_contract(adapter, result)
assert result.provenance.authority
assert result.provenance.fetched_at
assert result.provenance.content_sha256
Also test missing credentials, HTTP failure, malformed JSON, unknown/missing fields, unexpected
empty results, invalid units/timestamps, stale observations, and secret redaction. Scheduled live
contract checks belong in .github/workflows/cwa-live-contract.yml or an equivalently protected
workflow.
Register an adapter
- Add the strict raw-response Pydantic models and normalization code under
minxionghydrocast.ingestion. - Return a
SourceResultwith completeSourceProvenanceand retry telemetry. - Add deterministic fixture tests plus the shared contract assertion.
- Document authority, dataset, use, license/terms, retention, and redistribution status in the source register.
- Wire the adapter into collection only after failure and freshness behavior are explicit.
- Keep any live smoke command under the grouped
mhc source ...CLI.
An adapter does not by itself make a region, product, forecast, or redistribution path operationally supported.