Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/v2-generated-simulation-client.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API v1 sends its requests to the simulation service (household calculations, simulations, reports and report re-runs) through the generated `policyengine-api-simulation-client`, pinned to 0.20261127.430; the requests, and the answers API v1 gives its callers, are unchanged.
9 changes: 6 additions & 3 deletions docs/migration/stage-11-v2-households.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,12 @@ succeeded job whose result can't be read is 500.
The call to the simulation service goes through
`policyengine_api/libs/simulation_household_calculations.py`, at
`SIMULATION_ENTRYPOINT_URL` with the gateway credentials of every other
simulation call. `post_household_calculation` is the only code that sends
the request, so moving to the generated simulation client changes that
function and the dependency.
simulation call. `post_household_calculation`
(`policyengine_api/libs/simulation_service_operations.py`) is the only code
that sends the request: it builds it with the simulation service's generated
client, `policyengine-api-simulation-client`, from its
`submit_household_calculation` operation and `HouseholdCalculationRequest`
model.

The live staging job runs, after the other live tests: a submit and read
cycle, six calculations submitted at once (each accepted within
Expand Down
40 changes: 11 additions & 29 deletions policyengine_api/libs/simulation_household_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@
which the service never received after a connection never made or a gateway
token that couldn't be fetched.

Wiring the generated simulation client: ``post_household_calculation`` is the
only code that sends the request, and it returns the status and body, which
the rest of this module reads. Once a ``policyengine-api-simulation-client``
release has the household calculation route, depend on that release and make
the function call the generated operation, as its docstring says.
The request goes through the simulation service's generated client:
``post_household_calculation``
(``policyengine_api.libs.simulation_service_operations``) builds it with the
generated ``submit_household_calculation`` operation and
``HouseholdCalculationRequest`` model, sends it, and returns the status and
body, which the rest of this module reads.
"""

from __future__ import annotations
Expand Down Expand Up @@ -120,6 +121,9 @@
status_and_code,
submit_again,
)
from policyengine_api.libs.simulation_service_operations import (
post_household_calculation,
)
from policyengine_api.request_context import start_observability_id
from policyengine_api.services.v2.household_calculations.types import (
HouseholdCalculationAccepted,
Expand All @@ -141,6 +145,8 @@
HouseholdCalculationInput,
)

# The route's path, which tests check requests against. The generated
# operation holds its own copy, which it sends the request to.
HOUSEHOLD_CALCULATIONS_PATH = "/household-calculations"
UNREADABLE_ANSWER = "The simulation service's answer could not be read."
# Statuses after which no job of the submission runs: the service's
Expand Down Expand Up @@ -219,30 +225,6 @@ def household_calculation_request(
return body


def post_household_calculation(
http_client: httpx.Client, body: dict[str, Any]
) -> SimulationAnswer:
"""Send one submission to the simulation service and return its answer.

Raise httpx.HTTPError when no answer arrives, and GatewayAuthError when
the gateway token cannot be fetched. ``http_client`` has the service's
base URL. To send through the generated client instead, this body
becomes, with ``Client`` from ``policyengine_api_simulation_client``,
``submit_household_calculation`` from its ``api.default`` package and
``HouseholdCalculationRequest`` from its ``models``, the names the
simulation service's OpenAPI document gives the route::

client = Client(base_url=str(http_client.base_url))
client.set_httpx_client(http_client)
response = submit_household_calculation.sync_detailed(
client=client, body=HouseholdCalculationRequest.from_dict(body)
)
return SimulationAnswer(int(response.status_code), response.content)
"""
response = http_client.post(HOUSEHOLD_CALCULATIONS_PATH, json=body)
return SimulationAnswer(status_code=response.status_code, content=response.content)


def _invalid_request_reason(refusal: Refusal, code: str | None = None) -> str:
"""Return the service's reason, or each refused field and why.

Expand Down
25 changes: 8 additions & 17 deletions policyengine_api/libs/simulation_report_reruns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
``POST /reports/{report_id}/runs``, through the same HTTP client as the
service's other v2 routes (``policyengine_api.libs.simulation_reports``),
with ``"include_cliffs": true`` when the re-run asks for cliffs. The
request goes through the generated client's ``rerun_report`` operation
(``post_report_rerun`` in
``policyengine_api.libs.simulation_service_operations``). The
service creates the stored report's new run on its default worker's
versions and starts it, keeping every earlier run. Without ``force``, each
side reuses a run of its own simulation that succeeded on those versions.
Expand Down Expand Up @@ -131,7 +134,11 @@ class _RerunRefusal(Refusal):


def report_runs_path(report_id: UUID) -> str:
"""Return the simulation service's path for the report's runs."""
"""Return the simulation service's path for the report's runs.

Tests check requests against it. The generated ``rerun_report``
operation builds its own copy, which it sends the request to.
"""
return f"/reports/{report_id}/runs"


Expand All @@ -148,22 +155,6 @@ def report_rerun_request(rerun: ReportRerunInput) -> dict[str, Any]:
return body


def post_report_rerun(
http_client: httpx.Client, report_id: UUID, body: dict[str, Any]
) -> SimulationAnswer:
"""Send one re-run to the simulation service and return its answer.

Raise httpx.HTTPError when no answer arrives, and GatewayAuthError when
the gateway token cannot be fetched. ``http_client`` has the service's
base URL. With the generated simulation client, this body becomes the
route's operation (``rerun_report.sync_detailed`` with ``report_id`` and
``ReportRerunRequest.from_dict(body)``), as
``policyengine_api.libs.simulation_reports.post_report`` describes.
"""
response = http_client.post(report_runs_path(report_id), json=body)
return SimulationAnswer(status_code=response.status_code, content=response.content)


def _refusal(answer: SimulationAnswer) -> _RerunRefusal | None:
try:
return _RerunRefusal.model_validate_json(answer.content)
Expand Down
60 changes: 16 additions & 44 deletions policyengine_api/libs/simulation_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@
``POST /reports/{report_id}/runs``; how it reads that route's answers is in
``policyengine_api.libs.simulation_report_reruns``.

Wiring the generated simulation client: ``post_simulation``,
``post_report`` and ``post_report_rerun`` are the only code that sends a
request, and each returns the status and body, which the rest of these
modules read. Once a
``policyengine-api-simulation-client`` release has the three routes, depend
on that release and make each function call the generated operation, as its
docstring says.
Each request goes through the simulation service's generated client:
``post_simulation``, ``post_report`` and ``post_report_rerun``
(``policyengine_api.libs.simulation_service_operations``) build it with the
route's generated operation (``create_simulation``, ``create_report`` and
``rerun_report``) and request model (``EconomySimulationRequest``;
``EconomyReportRequest``, ``BudgetWindowReportRequest`` or
``HouseholdReportRequest``; ``ReportRerunRequest``), send it, and return the
status and body, which the rest of these modules read.
"""

from __future__ import annotations
Expand All @@ -116,10 +117,7 @@

from pydantic import ValidationError

from policyengine_api.libs.simulation_report_reruns import (
post_report_rerun,
start_report_rerun,
)
from policyengine_api.libs.simulation_report_reruns import start_report_rerun
from policyengine_api.libs.simulation_service_answers import (
CALLER_ROW_REFUSAL_CODES,
CREATED_STATUSES,
Expand All @@ -139,6 +137,11 @@
status_and_code,
submit_again,
)
from policyengine_api.libs.simulation_service_operations import (
post_report,
post_report_rerun,
post_simulation,
)
from policyengine_api.services.v2.simulation_service.types import (
SimulationServiceAnswerUnreadableError,
SimulationServiceInternalError,
Expand All @@ -165,6 +168,8 @@
StartedReportRun,
)

# The routes' paths, which tests check requests against. Each generated
# operation holds its own copy, which it sends the request to.
SIMULATIONS_PATH = "/simulations"
REPORTS_PATH = "/reports"

Expand Down Expand Up @@ -307,39 +312,6 @@ def household_report_request(report: HouseholdReportCreationInput) -> dict[str,
}


def post_simulation(
http_client: httpx.Client, body: dict[str, Any]
) -> SimulationAnswer:
"""Send one simulation to the simulation service and return its answer.

Raise httpx.HTTPError when no answer arrives, and GatewayAuthError when
the gateway token cannot be fetched. ``http_client`` has the service's
base URL. To send through the generated client instead, this body
becomes (the operation and model names are those the simulation
service's OpenAPI document gives the route)::

client = Client(base_url=str(http_client.base_url))
client.set_httpx_client(http_client)
response = create_simulation.sync_detailed(
client=client, body=SimulationRequest.from_dict(body)
)
return SimulationAnswer(int(response.status_code), response.content)
"""
response = http_client.post(SIMULATIONS_PATH, json=body)
return SimulationAnswer(status_code=response.status_code, content=response.content)


def post_report(http_client: httpx.Client, body: dict[str, Any]) -> SimulationAnswer:
"""Send one report to the simulation service and return its answer.

As ``post_simulation``; with the generated client, the operation is the
report route's (``create_report.sync_detailed`` with
``ReportRequest.from_dict(body)``).
"""
response = http_client.post(REPORTS_PATH, json=body)
return SimulationAnswer(status_code=response.status_code, content=response.content)


def _refusal(answer: SimulationAnswer) -> _Refusal | None:
try:
return _Refusal.model_validate_json(answer.content)
Expand Down
Loading
Loading