Repository navigation
Send API v1's requests to the simulation service through its generated client - #4355
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.pyholds the four senders, which replace the hand-written ones insimulation_household_calculations.py,simulation_reports.pyandsimulation_report_reruns.py:POST /household-calculationspost_household_calculationsubmit_household_calculationHouseholdCalculationRequestPOST /simulationspost_simulationcreate_simulationEconomySimulationRequestPOST /reportspost_reportcreate_reportEconomyReportRequest,BudgetWindowReportRequestorHouseholdReportRequest, by the body'sreport_typePOST /reports/{report_id}/runspost_report_rerunrerun_reportReportRerunRequestEach 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 raisesValueError, 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 itssync_detailed.sync_detailedalso parses the answer and raises on a body that isn't JSON or isn't the documented shape, and on a statushttp.HTTPStatusdoesn't name;create_reportandrerun_reportdocument 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), sosync_detailedwould change the answers._get_kwargsis 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_cliffsbeforespm) and header order differ.Later routes: the module docstring says how to add one (its generated operation and model,
send_request_with_bodyorsend_operation_request).Not changed: the v1 economy routes'
SimulationEntrypointClientand the model catalog publication'sGET /versions(fetch_simulation_versions) still send by hand; the module docstring names them.Inline suppression added:
# noqa: E501onfrom 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:ValueErrorand sends nothing.make quality(ruff, ruff format,lint-imports, mypy,scripts/run_quality_guards.py),python -m scripts.rules validate,uv lock --check, andscripts/export_migration_contracts.py(no diff).uv.lock(uv sync --frozen --no-dev).🤖 Generated with Claude Code