|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Async Client: connect to OpenDecree using asyncio. |
| 3 | +
|
| 4 | +Demonstrates the AsyncConfigClient — same API as ConfigClient but fully |
| 5 | +async. Uses `async with` for lifecycle and `await` for all operations. |
| 6 | +
|
| 7 | +Run: |
| 8 | + python main.py |
| 9 | +
|
| 10 | +Requires a running decree server with seeded data (see ../README.md). |
| 11 | +""" |
| 12 | + |
| 13 | +import asyncio |
| 14 | +from datetime import timedelta |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +from opendecree import AsyncConfigClient |
| 18 | + |
| 19 | + |
| 20 | +async def main() -> None: |
| 21 | + tenant_id = get_tenant_id() |
| 22 | + |
| 23 | + # Async context manager — closes the gRPC channel on exit. |
| 24 | + async with AsyncConfigClient("localhost:9090", subject="async-example") as client: |
| 25 | + # All operations are awaitable. |
| 26 | + name = await client.get(tenant_id, "app.name") |
| 27 | + print(f"app.name: {name}") |
| 28 | + |
| 29 | + debug = await client.get(tenant_id, "app.debug", bool) |
| 30 | + print(f"app.debug: {debug}") |
| 31 | + |
| 32 | + rate_limit = await client.get(tenant_id, "server.rate_limit", int) |
| 33 | + print(f"server.rate_limit: {rate_limit}") |
| 34 | + |
| 35 | + timeout = await client.get(tenant_id, "server.timeout", timedelta) |
| 36 | + print(f"server.timeout: {timeout}") |
| 37 | + |
| 38 | + fee_rate = await client.get(tenant_id, "payments.fee_rate", float) |
| 39 | + print(f"payments.fee_rate: {fee_rate}") |
| 40 | + |
| 41 | + # Concurrent reads with asyncio.gather — faster than sequential. |
| 42 | + print("\nConcurrent reads:") |
| 43 | + name, debug, rate_limit = await asyncio.gather( |
| 44 | + client.get(tenant_id, "app.name"), |
| 45 | + client.get(tenant_id, "app.debug", bool), |
| 46 | + client.get(tenant_id, "server.rate_limit", int), |
| 47 | + ) |
| 48 | + print(f" app.name: {name}") |
| 49 | + print(f" app.debug: {debug}") |
| 50 | + print(f" server.rate_limit: {rate_limit}") |
| 51 | + |
| 52 | + # Atomic multi-write. |
| 53 | + await client.set_many( |
| 54 | + tenant_id, |
| 55 | + {"app.debug": "true", "server.rate_limit": "200"}, |
| 56 | + description="async example update", |
| 57 | + ) |
| 58 | + print("\nUpdated app.debug=true, server.rate_limit=200") |
| 59 | + |
| 60 | + debug = await client.get(tenant_id, "app.debug", bool) |
| 61 | + rate_limit = await client.get(tenant_id, "server.rate_limit", int) |
| 62 | + print(f" app.debug: {debug}") |
| 63 | + print(f" server.rate_limit: {rate_limit}") |
| 64 | + |
| 65 | + |
| 66 | +def get_tenant_id() -> str: |
| 67 | + import os |
| 68 | + |
| 69 | + if v := os.environ.get("TENANT_ID"): |
| 70 | + return v |
| 71 | + tenant_file = Path(__file__).parent.parent / ".tenant-id" |
| 72 | + if tenant_file.exists(): |
| 73 | + return tenant_file.read_text().strip() |
| 74 | + raise SystemExit("Set TENANT_ID env var or run 'make setup' from the examples directory") |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + asyncio.run(main()) |
0 commit comments