Skip to content

Send API v1's requests to the simulation service through its generated client - #4355

Merged
anth-volk merged 4 commits into
masterfrom
v2plan/d7-1-generated-sim-client
Nov 27, 2026
Merged

anth-volk merged 4 commits into
masterfrom
v2plan/d7-1-generated-sim-client

Conversation

@anth-volk

Copy link
Copy Markdown
Collaborator

Fixes #4354

Summary

  • API v1 now depends on policyengine-api-simulation-client==0.20261127.430, the simulation service's client generated from its OpenAPI document. Every release from 0.20261125.416 on has all four routes below and every request field API v1 sends; 0.20261127.430 is the newest.

  • New policyengine_api/libs/simulation_service_operations.py holds the four senders, which replace the hand-written ones in simulation_household_calculations.py, simulation_reports.py and simulation_report_reruns.py:

    Route Sender Generated operation Generated request model
    POST /household-calculations post_household_calculation submit_household_calculation HouseholdCalculationRequest
    POST /simulations post_simulation create_simulation EconomySimulationRequest
    POST /reports post_report create_report EconomyReportRequest, BudgetWindowReportRequest or HouseholdReportRequest, by the body's report_type
    POST /reports/{report_id}/runs post_report_rerun rerun_report ReportRerunRequest
  • Each sender builds the generated model from the body API v1 already builds (from_dict), has the generated operation build the request (method, path, JSON body, content type), checks that the JSON body is exactly API v1's body (it raises ValueError, before sending, if the model would leave out, add or change a field, since a model keeps only the fields it defines), sends it on the existing simulation service HTTP client (base URL, gateway auth, request and observability ID headers, transport, instrumentation), and returns the status and body to the existing code that reads them.

  • The senders call each operation's request builder, _get_kwargs, not its sync_detailed. sync_detailed also parses the answer and raises on a body that isn't JSON or isn't the documented shape, and on a status http.HTTPStatus doesn't name; create_report and rerun_report document only 201. API v1 reads those answers itself (a created record whose answer can't be read, a front end's 503 in the service's place, a 202), so sync_detailed would change the answers. _get_kwargs is private to the generated package; the exact pin keeps it, and the tests show the requests it builds.

  • The requests and the answers API v1 gives its callers are unchanged. Only the JSON key order (for example an economy report's include_cliffs before spm) and header order differ.

  • Later routes: the module docstring says how to add one (its generated operation and model, send_request_with_body or send_operation_request).

  • Not changed: the v1 economy routes' SimulationEntrypointClient and the model catalog publication's GET /versions (fetch_simulation_versions) still send by hand; the module docstring names them.

Inline suppression added: # noqa: E501 on from policyengine_api_simulation_client.api.default.submit_household_calculation import (, whose module path alone takes the line to 89 characters and can't be split.

Testing

  • tests/unit/libs/test_simulation_generated_client.py, 49 tests, committed failing first:
    • per route and per kind of report, with every optional field, explicit nulls and absent fields: the request sent is the one the generated operation builds from its model (byte for byte), the body the generated model wrote, and the same JSON body API v1 builds by hand;
    • per route, the answers the generated parsers raise on (a body that isn't JSON or isn't the documented shape, status 599, a front end's 503, and 202 for reports and re-runs) give the same errors and messages as before;
    • per route, a body the generated model would change (a field it lacks, an SPM option it lacks, a household report side's field it lacks) raises ValueError and sends nothing.
  • The existing tests of these modules are unchanged, and they still read the routes' paths from the same constants.
  • make quality (ruff, ruff format, lint-imports, mypy, scripts/run_quality_guards.py), python -m scripts.rules validate, uv lock --check, and scripts/export_migration_contracts.py (no diff).
  • No environment variable is added or read anew. The Cloud Run image installs the client from uv.lock (uv sync --frozen --no-dev).

🤖 Generated with Claude Code

anth-volk and others added 4 commits November 28, 2026 00:20
Install policyengine-api-simulation-client 0.20261127.430, the newest
release, whose request models hold every field API v1 sends, for the tests
only. For each route API v1 calls (POST /household-calculations,
/simulations, /reports and /reports/{report_id}/runs), the new tests check
that the request API v1 sends is the one the generated operation builds from
the generated request model, and the same request API v1 builds by hand;
and that API v1 still reads the answers the generated operations' parsers
raise on.

They fail: API v1 still posts each body by hand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
API v1 now depends on policyengine-api-simulation-client 0.20261127.430.
The new policyengine_api.libs.simulation_service_operations module holds
the four senders: post_household_calculation, post_simulation, post_report
and post_report_rerun. Each builds the route's generated request model from
the body API v1 builds, has the route's generated operation
(submit_household_calculation, create_simulation, create_report,
rerun_report) build the request, sends it on API v1's simulation service
HTTP client, and returns the status and body for the existing code to read.

The operations' request builders are used instead of their sync_detailed,
which parses the answer and raises on a body that isn't JSON or isn't the
documented shape, and on a status http.HTTPStatus doesn't name; API v1
reads those answers itself. The requests and answers are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A generated request model's from_dict keeps only the fields the model
defines, so a field API v1 builds that the pinned release lacks, at the
top level or inside the SPM settings or a household report's side, would
be left out of the request without an error. The new test expects each
sender to refuse such a body with a ValueError naming the field, before
sending anything. It fails: each sender sends the body without the field.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ents

Each sender now passes the body API v1 built to send_request_with_body,
which compares it with the JSON body the generated operation built and
raises ValueError, naming each top-level field that differs, before
sending anything. A field the pinned release's model lacks can no longer
be left out of a request without an error.

The comments on the routes' path constants say that tests check requests
against them, and that each generated operation holds its own copy. The
dependency's comment and the new module's docstring say which requests
go through the generated client and which API v1 still sends by hand,
and that the generated package treats _get_kwargs as private.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.88%. Comparing base (11c3711) to head (e4acfab).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4355      +/-   ##
==========================================
- Coverage   93.89%   93.88%   -0.02%     
==========================================
  Files         432      433       +1     
  Lines       24194    24216      +22     
  Branches     3752     3754       +2     
==========================================
+ Hits        22718    22734      +16     
- Misses        846      853       +7     
+ Partials      630      629       -1     
Flag Coverage Δ
integration 59.23% <0.00%> (-0.06%) ⬇️
unit 91.13% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@anth-volk
anth-volk marked this pull request as ready for review November 27, 2026 21:41
@anth-volk
anth-volk merged commit aa563f9 into master Nov 27, 2026
23 checks passed
@anth-volk
anth-volk deleted the v2plan/d7-1-generated-sim-client branch November 27, 2026 21:41

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Send API v1's requests to the simulation service through its generated client

1 participant