Skip to content

Commit edaec5e

Browse files
committed
feat: API - Auto-generating the CLI from the real API
Usage: ``` uv run tangle api Usage: tangle api COMMAND Call the Tangle API server. Commands: admin artifacts component-libraries component-library-pins components executions pipeline-runs published-components secrets users ``` TANGLE_API_URL="http://127.0.0.1:8000" TANGLE_API_TOKEN="XXX" uv run tangle api pipeline-runs set-annotation 0123 --key key1 --value value1 ``` Debug: ``` uv run tangle api pipeline-runs set-annotation 12 --key k1 --value v1 --debug PUT http://127.0.0.1:8000/api/pipeline_runs/12/annotations/k1?value=v1 ``` Complex parameters can be read from string or JSON/YAML files. Pydantic parameters are validated against the model classes.
1 parent bfb093b commit edaec5e

4 files changed

Lines changed: 780 additions & 12 deletions

File tree

tangle_cli/api/__init__.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Client access for the Tangle API CLI.
2+
3+
The generated ``tangle api ...`` commands send their HTTP requests through a
4+
single, globally-initialized :class:`~tangle_cli.api.client.Client` instance.
5+
Use :func:`get_client` to obtain it and :func:`set_client` to override it
6+
(for example, to point at a different server or to inject authentication).
7+
8+
The default client reads its configuration from the environment:
9+
10+
* ``TANGLE_API_URL`` -- base URL of the API server
11+
(default: ``http://127.0.0.1:8000``).
12+
* ``TANGLE_API_TOKEN`` -- if set, requests are sent with bearer authentication.
13+
"""
14+
15+
import os
16+
17+
from . import client
18+
19+
__all__ = [
20+
"AuthenticatedClient",
21+
"Client",
22+
"DEFAULT_BASE_URL",
23+
"get_client",
24+
"set_client",
25+
]
26+
27+
AuthenticatedClient = client.AuthenticatedClient
28+
Client = client.Client
29+
30+
DEFAULT_BASE_URL = "http://127.0.0.1:8000"
31+
32+
_client: client.Client | None = None
33+
34+
35+
def _build_default_client() -> client.Client:
36+
base_url = os.environ.get("TANGLE_API_URL", DEFAULT_BASE_URL)
37+
token = os.environ.get("TANGLE_API_TOKEN")
38+
if token:
39+
return client.AuthenticatedClient(base_url=base_url, token=token)
40+
return client.Client(base_url=base_url)
41+
42+
43+
def get_client() -> client.Client:
44+
"""Return the global API client, creating a default one if needed."""
45+
global _client
46+
if _client is None:
47+
_client = _build_default_client()
48+
return _client
49+
50+
51+
def set_client(client: client.Client) -> None:
52+
"""Replace the global API client used by the generated CLI commands."""
53+
global _client
54+
_client = client

0 commit comments

Comments
 (0)