Conversation
There was a problem hiding this comment.
Code Review
This pull request renames the namespace variable to instance_name across Terraform modules and the admin CLI, while maintaining backward compatibility. It also introduces a new sdmx command group to query custom SDMX v3 observation APIs and enhances tf_utils.py to support fetching Terraform outputs directly from GCS remote state. Feedback on these changes focuses on improving the manual ADC loading logic for Windows compatibility and standard precedence, initializing the GCS client with the target project ID for robustness, and refining error handling when parsing non-OK HTTP responses.
be6c2c6 to
72f8975
Compare
|
Do we want this to be under "admin"? Or should it be like datcom-cli api sdmx? or just datcom-cli sdmx? These docs are a bit outdated, but capture the concept of things that are end-usery vs purely admin: Can we create a cli where we "connect" to an instance and then use cli to issue api calls? Like In that case, can/should we move the datacommons-client into this repo? With all that in mind, do we still want to add |
- Run ruff format on files flagged by CI's 'ruff format --check' step. - Extract non-OK response parsing into SdmxClient._extract_error_message and fall back to the HTTP reason phrase, so blank bodies, empty JSON objects and JSON nulls no longer produce empty or 'None' messages. - Add parametrized regression tests covering error message extraction.
Querying observations is an end-user operation, not an administrative one, so the SDMX commands move out of `datacommons admin` into a new `datacommons client` group: datacommons client sdmx-data ... datacommons client sdmx-availability ... An endpoint is now selected in one of two ways. `--url` accepts a bare host or full URL and defaults to the public Data Commons API, with an API key read from `--api-key` or `DATACOMMONS_API_KEY`. A DCP instance deployed behind IAM cannot be reached by URL alone, so it is selected with `--project-id` and `--instance-name`, which resolve its service URL from remote Terraform state and sign requests with Google Cloud credentials. The two deployment flavors serve the SDMX API under different path prefixes (`/sdmx/v3` on the public API, `/core/api/sdmx/v3` on a DCP instance). The client picks the likely prefix for the endpoint and retries once on a 404, then reuses the discovered prefix, so neither users nor callers need to configure it. The client code also moves from `datacommons-admin` to `datacommons-cli`, keeping end-user functionality out of the admin package.
cce8efe to
25a1445
Compare
Adds SDMX 3.0 query commands to the Data Commons CLI under a new
clientcommand group, alongside a reusableSdmxClient.Why a
clientgroupPer review feedback, querying observations is an end-user operation rather than an administrative one, so these commands no longer live under
datacommons admin:The implementation also moves from the
datacommons-adminpackage todatacommons-cli, keeping end-user functionality out of the admin package.datacommons admin sdmxis removed.Connecting to an endpoint
An endpoint is selected in one of two ways, depending on whether its URL is publicly reachable:
--url--project-id+--instance-namePublic API (default). Commands target
https://api.datacommons.organd authenticate with an API key from--api-keyor theDATACOMMONS_API_KEYenvironment variable.--urlretargets to any reachable endpoint and accepts a bare host or a full URL (api.datacommons.org,https://api.datacommons.org,http://localhost:8080).Private DCP instance. A DCP instance on Cloud Run is private by default and sits behind IAM, so it cannot be reached by URL alone.
--project-idand--instance-nameresolve its service URL from remote Terraform state in GCS and sign requests with your Google Cloud credentials.The two modes are mutually exclusive, and the flags are validated at parse time so conflicting input fails before any network or state access.
Handling the two API path layouts
The two deployment flavors serve the SDMX API under different prefixes:
/sdmx/v3/…/core/api/sdmx/v3/…Rather than exposing this as a flag, the client picks the likely prefix for the endpoint and retries once on a 404, then reuses the discovered prefix for the rest of the session. A valid SDMX query never returns 404 (an unknown variable returns 200 with an empty result), so the status unambiguously signals the other layout. In the common case this costs no extra request.
Key Changes
clientcommand group (packages/datacommons-cli/datacommons_cli/client/):client_cli.py: theclientgroup and its two commands. Shared query options are factored into a single decorator, endpoint resolution is lazy so--helpnever touches the network, and informational output goes tostderrsostdoutstays a clean CSV/JSON stream.connection.py: endpoint resolution, URL normalization, API key and Google ID token auth, and the guidance shown when an endpoint rejects credentials.sdmx_client.py: the SDMX HTTP client, query construction, API-root discovery, and error extraction.sdmx-data: observations matching a variable and dimension filters, as SDMX-CSV.sdmx-availability <component_id>: available values and constraints for a dimension or attribute, as SDMX-JSON Structure.-v/--variable,-f/--filter(repeatable,key=value),-o/--output,--log/--no-log,--multi-entity/--no-multi-entity,--accept.401/403responses append guidance specific to the connection mode, pointing at--api-key/DATACOMMONS_API_KEYfor the public API orgcloud auth application-default loginfor an instance.tf_utils:get_datacommons_service_urlaccepts an explicitTerraformStateConfigso callers outside a Click context can resolve a named instance.datacommons-clinow declaresrequestsandgoogle-authdirectly instead of relying on them transitively.packages/datacommons-cli/README.mdaround the new group, with a command-group overview, endpoint selection guide, and refreshed cheatsheet.Sample CLI Usage
Sample Responses
sdmx-data(SDMX-CSV)sdmx-availability(SDMX-JSON Structure){ "meta": { "schema": "https://json.sdmx.org/2.0.0/sdmx-json-structure-schema.json", "id": "DF_OBS_AVAILABILITY", "prepared": "2026-09-18T23:43:05Z", "sender": { "id": "DC" } }, "data": { "dataConstraints": [ { "id": "DF_OBS_AVAILABILITY", "agencyID": "DC", "version": "1.0.0", "name": "Available DF_OBS data", "role": "Actual", "cubeRegions": [ { "include": true, "keyValues": [ { "id": "provenance", "include": true, "values": [ { "value": "dc/base/CensusACS5YearSurvey" }, { "value": "dc/base/WikidataPopulation" } ] } ] } ] } ] } }Programmatic Usage
Verification
api.datacommons.org: both commands, the default endpoint and--urlin bare-host and full-URL forms,--api-keyandDATACOMMONS_API_KEY, multi-value filters,-ofile output, and the401guidance when no key is supplied.packages/datacommons-cli/tests/client/covering URL normalization, flag validation, both connection modes, query construction, header handling, error extraction, root discovery and caching, and CLI behavior. Full suite: 231 passing.tests/integration/suites/03_serving_api/test_sdmx.pyupdated to the new commands andConnection-based client.