|
| 1 | +"""Small resource wrappers over the existing versioned HTTP API.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import math |
| 5 | +import time |
| 6 | +from typing import TYPE_CHECKING, Any |
| 7 | +from urllib.parse import quote |
| 8 | + |
| 9 | +from .errors import ExecutionTimeout |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from .client import Subfork |
| 13 | + |
| 14 | + |
| 15 | +def segment(value: str) -> str: |
| 16 | + if not value or value in {'.', '..'}: |
| 17 | + raise ValueError('Resource identifiers must be non-empty and cannot be dot segments.') |
| 18 | + return quote(value, safe='') |
| 19 | + |
| 20 | + |
| 21 | +class Resource: |
| 22 | + def __init__(self, client: Subfork): |
| 23 | + self._client = client |
| 24 | + |
| 25 | + |
| 26 | +class Nodes(Resource): |
| 27 | + def list(self, *, include_all_versions: bool = False) -> list[dict[str, Any]]: |
| 28 | + return self._client._request('GET', '/nodes', params={'include_all_versions': include_all_versions}) |
| 29 | + |
| 30 | + def get(self, node_id: str) -> dict[str, Any]: |
| 31 | + return self._client._request('GET', '/nodes/' + segment(node_id)) |
| 32 | + |
| 33 | + |
| 34 | +class Graphs(Resource): |
| 35 | + def list(self) -> list[dict[str, Any]]: |
| 36 | + return self._client._request('GET', '/graphs') |
| 37 | + |
| 38 | + def get(self, graph_id: str) -> dict[str, Any]: |
| 39 | + return self._client._request('GET', '/graphs/' + segment(graph_id)) |
| 40 | + |
| 41 | + def validate(self, definition: dict[str, Any], *, name: str | None = None) -> dict[str, Any]: |
| 42 | + return self._client._request('POST', '/graphs/validate', json={ |
| 43 | + 'name': name if name is not None else definition.get('name', 'Untitled Graph'), 'definition': definition}) |
| 44 | + |
| 45 | + def create(self, *, name: str, definition: dict[str, Any], description: str = '', tags: list[str] | None = None) -> dict[str, Any]: |
| 46 | + return self._client._request('POST', '/graphs', json={ |
| 47 | + 'name': name, 'description': description, 'definition': definition, 'tags': tags or []}) |
| 48 | + |
| 49 | + def update(self, graph_id: str, *, name: str, definition: dict[str, Any], description: str = '') -> dict[str, Any]: |
| 50 | + return self._client._request('PUT', '/graphs/' + segment(graph_id), json={ |
| 51 | + 'name': name, 'description': description, 'definition': definition}) |
| 52 | + |
| 53 | + def published(self) -> list[dict[str, Any]]: |
| 54 | + return self._client._request('GET', '/graphs/published') |
| 55 | + |
| 56 | + def versions(self, graph_id: str) -> list[dict[str, Any]]: |
| 57 | + return self._client._request('GET', '/graphs/' + segment(graph_id) + '/versions') |
| 58 | + |
| 59 | + def published_version(self, graph_id: str, version: str) -> dict[str, Any]: |
| 60 | + return self._client._request('GET', '/graphs/published/' + segment(graph_id) + '/versions/' + segment(version)) |
| 61 | + |
| 62 | + def interface(self, graph_id: str) -> dict[str, Any]: |
| 63 | + return self._client._request('GET', '/graphs/' + segment(graph_id) + '/interface-preview') |
| 64 | + |
| 65 | + def publish(self, graph_id: str, *, version: str, interface: dict[str, Any] | None = None, comment: str = '') -> dict[str, Any]: |
| 66 | + payload: dict[str, Any] = {'version': version, 'comment': comment} |
| 67 | + if interface is not None: |
| 68 | + payload['interface'] = interface |
| 69 | + return self._client._request('POST', '/graphs/' + segment(graph_id) + '/versions', json=payload) |
| 70 | + |
| 71 | + def execute(self, graph_id: str, *, version: str = 'draft', inputs: dict[str, Any] | None = None) -> dict[str, Any]: |
| 72 | + return self._client._request('POST', '/graphs/' + segment(graph_id) + '/execute', |
| 73 | + params={'version': version}, json={'inputs': inputs or {}}) |
| 74 | + |
| 75 | + |
| 76 | +class Executions(Resource): |
| 77 | + def get(self, execution_id: str) -> dict[str, Any]: |
| 78 | + return self._client._request('GET', '/executions/' + segment(execution_id)) |
| 79 | + |
| 80 | + def cancel(self, execution_id: str) -> dict[str, Any]: |
| 81 | + return self._client._request('POST', '/executions/' + segment(execution_id) + '/cancel') |
| 82 | + |
| 83 | + def wait(self, execution_id: str, *, timeout: float = 120, poll_interval: float = 2) -> dict[str, Any]: |
| 84 | + if not math.isfinite(timeout) or timeout <= 0 or not math.isfinite(poll_interval) or poll_interval <= 0: |
| 85 | + raise ValueError('timeout and poll_interval must be positive finite numbers.') |
| 86 | + deadline = time.monotonic() + timeout |
| 87 | + while True: |
| 88 | + remaining = deadline - time.monotonic() |
| 89 | + if remaining <= 0: |
| 90 | + raise ExecutionTimeout(execution_id) |
| 91 | + result = self._client._request('GET', '/executions/' + segment(execution_id), |
| 92 | + timeout=min(self._client.timeout, remaining)) |
| 93 | + if result['status'] in {'completed', 'failed', 'canceled', 'cancelled', 'outcome_unknown'}: |
| 94 | + return result |
| 95 | + time.sleep(min(poll_interval, max(0, deadline - time.monotonic()))) |
0 commit comments