Skip to content

fetch_saved_scenario

fetch_saved_scenario

Service for fetch saved scenario operations.

Classes

FetchSavedScenarioRunner

Bases: BaseRunner[Dict[str, Any]]

Runner for fetching a SavedScenario from MyETM by its ID.

GET /api/v3/saved_scenarios/{saved_scenario_id}

Functions
run staticmethod
run(client, saved_scenario, **kwargs)

Fetch a single SavedScenario by ID.

Parameters:

Name Type Description Default
client BaseClient

HTTP client

required
saved_scenario ScenarioIdentifier

Object with an 'id' attribute

required

Returns:

Type Description
ServiceResult[Dict[str, Any]]

ServiceResult with SavedScenario data

Source code in src/pyetm/services/scenario_runners/fetch_saved_scenario.py
@staticmethod
def run(
    client: BaseClient, saved_scenario: ScenarioIdentifier, **kwargs: Any
) -> ServiceResult[Dict[str, Any]]:
    """
    Fetch a single SavedScenario by ID.

    Args:
        client: HTTP client
        saved_scenario: Object with an 'id' attribute

    Returns:
        ServiceResult with SavedScenario data
    """
    result = FetchSavedScenarioRunner._make_request(
        client=client, method="get", path=f"/saved_scenarios/{saved_scenario.id}"
    )

    if not result.success:
        for error in result.errors:
            if "404" in error or "not found" in error.lower():
                return ServiceResult.fail(
                    [
                        f"SavedScenario {saved_scenario.id} not found on this environment"
                    ]
                )
        return result

    if result.data is None:
        return ServiceResult.fail(["No data returned from API"])

    # Check if the API returned an error payload in a successful response
    # MyETM API sometimes returns 200 OK with {"errors": [...], "scenario": null}
    if isinstance(result.data, dict) and "errors" in result.data:
        errors = result.data.get("errors", [])
        if errors:
            # Check for "not found" errors
            for error in errors:
                if isinstance(error, str) and "not found" in error.lower():
                    return ServiceResult.fail(
                        [f"SavedScenario {saved_scenario.id} not found on this environment"]
                    )
            # Return generic failure with the error messages
            return ServiceResult.fail(errors if isinstance(errors, list) else [str(errors)])

    _, warnings = FetchSavedScenarioRunner._validate_response_keys(
        result.data,
        FetchSavedScenarioRunner.REQUIRED_KEYS,
        fill_missing=False,
    )

    return ServiceResult.ok(data=result.data, errors=warnings)