|
| 1 | +"""AsyncScenario resource class for asynchronous operations.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Dict, Optional |
| 6 | +from typing_extensions import Unpack, override |
| 7 | + |
| 8 | +from ..types import ScenarioView |
| 9 | +from ._types import BaseRequestOptions, LongRequestOptions, SDKScenarioRunParams |
| 10 | +from .._client import AsyncRunloop |
| 11 | +from .async_scenario_run import AsyncScenarioRun |
| 12 | + |
| 13 | + |
| 14 | +class AsyncScenario: |
| 15 | + """Async wrapper around a scenario resource. |
| 16 | +
|
| 17 | + Provides async methods for retrieving scenario details, updating the scenario, |
| 18 | + and starting scenario runs. |
| 19 | +
|
| 20 | + Example: |
| 21 | + >>> scenario = sdk.scenario.from_id("scn-xxx") |
| 22 | + >>> info = await scenario.get_info() |
| 23 | + >>> run = await scenario.run(run_name="test-run") |
| 24 | + >>> devbox = run.devbox |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, client: AsyncRunloop, scenario_id: str) -> None: |
| 28 | + """Initialize the wrapper. |
| 29 | +
|
| 30 | + :param client: Generated AsyncRunloop client |
| 31 | + :type client: AsyncRunloop |
| 32 | + :param scenario_id: Scenario ID returned by the API |
| 33 | + :type scenario_id: str |
| 34 | + """ |
| 35 | + self._client = client |
| 36 | + self._id = scenario_id |
| 37 | + |
| 38 | + @override |
| 39 | + def __repr__(self) -> str: |
| 40 | + return f"<AsyncScenario id={self._id!r}>" |
| 41 | + |
| 42 | + @property |
| 43 | + def id(self) -> str: |
| 44 | + """Return the scenario ID. |
| 45 | +
|
| 46 | + :return: Unique scenario ID |
| 47 | + :rtype: str |
| 48 | + """ |
| 49 | + return self._id |
| 50 | + |
| 51 | + async def get_info( |
| 52 | + self, |
| 53 | + **options: Unpack[BaseRequestOptions], |
| 54 | + ) -> ScenarioView: |
| 55 | + """Retrieve current scenario details. |
| 56 | +
|
| 57 | + :param options: Optional request configuration |
| 58 | + :return: Current scenario info |
| 59 | + :rtype: ScenarioView |
| 60 | + """ |
| 61 | + return await self._client.scenarios.retrieve( |
| 62 | + self._id, |
| 63 | + **options, |
| 64 | + ) |
| 65 | + |
| 66 | + async def update( |
| 67 | + self, |
| 68 | + *, |
| 69 | + name: Optional[str] = None, |
| 70 | + metadata: Optional[Dict[str, str]] = None, |
| 71 | + **options: Unpack[LongRequestOptions], |
| 72 | + ) -> ScenarioView: |
| 73 | + """Update the scenario. |
| 74 | +
|
| 75 | + Only provided fields will be updated. |
| 76 | +
|
| 77 | + :param name: New name for the scenario |
| 78 | + :type name: Optional[str] |
| 79 | + :param metadata: New metadata for the scenario |
| 80 | + :type metadata: Optional[Dict[str, str]] |
| 81 | + :param options: Optional long-running request configuration |
| 82 | + :return: Updated scenario info |
| 83 | + :rtype: ScenarioView |
| 84 | + """ |
| 85 | + return await self._client.scenarios.update( |
| 86 | + self._id, |
| 87 | + name=name, |
| 88 | + metadata=metadata, |
| 89 | + **options, |
| 90 | + ) |
| 91 | + |
| 92 | + async def run( |
| 93 | + self, |
| 94 | + **params: Unpack[SDKScenarioRunParams], |
| 95 | + ) -> AsyncScenarioRun: |
| 96 | + """Start a new scenario run. |
| 97 | +
|
| 98 | + Creates a new scenario run and returns a wrapper for managing it. |
| 99 | + The underlying devbox may still be starting; call await_env_ready() |
| 100 | + on the returned AsyncScenarioRun to wait for it to be ready. |
| 101 | +
|
| 102 | + :param params: See SDKScenarioRunParams for available parameters |
| 103 | + :return: Wrapper for the new scenario run |
| 104 | + :rtype: AsyncScenarioRun |
| 105 | + """ |
| 106 | + run_view = await self._client.scenarios.start_run( |
| 107 | + scenario_id=self._id, |
| 108 | + **params, |
| 109 | + ) |
| 110 | + return AsyncScenarioRun(self._client, run_view.id, run_view.devbox_id) |
| 111 | + |
| 112 | + async def run_and_await_env_ready( |
| 113 | + self, |
| 114 | + **params: Unpack[SDKScenarioRunParams], |
| 115 | + ) -> AsyncScenarioRun: |
| 116 | + """Start a new scenario run and wait for environment to be ready. |
| 117 | +
|
| 118 | + Convenience method that starts a run and waits for the devbox to be ready. |
| 119 | +
|
| 120 | + :param params: See SDKScenarioRunParams for available parameters |
| 121 | + :return: Wrapper for the scenario run with ready environment |
| 122 | + :rtype: AsyncScenarioRun |
| 123 | + """ |
| 124 | + run_view = await self._client.scenarios.start_run_and_await_env_ready( |
| 125 | + scenario_id=self._id, |
| 126 | + **params, |
| 127 | + ) |
| 128 | + return AsyncScenarioRun(self._client, run_view.id, run_view.devbox_id) |
| 129 | + |
0 commit comments