-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
83 lines (63 loc) · 2.86 KB
/
Copy pathmain.py
File metadata and controls
83 lines (63 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
"""Async Client: connect to OpenDecree using asyncio.
Demonstrates the AsyncConfigClient — same API as ConfigClient but fully
async. Uses `async with` for lifecycle and `await` for all operations.
Run:
python main.py
Requires a running decree server with seeded data (see ../README.md).
"""
import asyncio
from datetime import timedelta
from pathlib import Path
from opendecree import AsyncConfigClient, FieldUpdate
async def main() -> None:
tenant_id = get_tenant_id()
# Async context manager — closes the gRPC channel on exit.
async with AsyncConfigClient("localhost:9090", subject="async-example") as client:
# All operations are awaitable.
name = await client.get(tenant_id, "app.name")
print(f"app.name: {name}")
debug = await client.get(tenant_id, "app.debug", bool)
print(f"app.debug: {debug}")
rate_limit = await client.get(tenant_id, "server.rate_limit", int)
print(f"server.rate_limit: {rate_limit}")
timeout = await client.get(tenant_id, "server.timeout", timedelta)
print(f"server.timeout: {timeout}")
fee_rate = await client.get(tenant_id, "payments.fee_rate", float)
print(f"payments.fee_rate: {fee_rate}")
# Concurrent reads with asyncio.gather — faster than sequential.
print("\nConcurrent reads:")
name, debug, rate_limit = await asyncio.gather(
client.get(tenant_id, "app.name"),
client.get(tenant_id, "app.debug", bool),
client.get(tenant_id, "server.rate_limit", int),
)
print(f" app.name: {name}")
print(f" app.debug: {debug}")
print(f" server.rate_limit: {rate_limit}")
# Atomic multi-write — each update is a FieldUpdate, not a plain dict.
# Values are always strings — the server validates against the
# field's declared type, so this works cleanly for string fields.
await client.set_many(
tenant_id,
[
FieldUpdate("app.name", "Acme Corp App (async)"),
FieldUpdate("payments.currency", "EUR"),
],
description="async example update",
)
print("\nUpdated app.name and payments.currency")
name = await client.get(tenant_id, "app.name")
currency = await client.get(tenant_id, "payments.currency")
print(f" app.name: {name}")
print(f" payments.currency: {currency}")
def get_tenant_id() -> str:
import os
if v := os.environ.get("TENANT_ID"):
return v
tenant_file = Path(__file__).parent.parent / ".tenant-id"
if tenant_file.exists():
return tenant_file.read_text().strip()
raise SystemExit("Set TENANT_ID env var or run 'make setup' from the examples directory")
if __name__ == "__main__":
asyncio.run(main())