-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (41 loc) · 1.79 KB
/
main.py
File metadata and controls
60 lines (41 loc) · 1.79 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
#!/usr/bin/env python3
"""Quickstart: connect to OpenDecree and read typed configuration values.
This is the simplest possible example — context manager, typed get().
Run:
python main.py
Requires a running decree server with seeded data (see ../README.md).
"""
from datetime import timedelta
from pathlib import Path
from opendecree import ConfigClient
def main() -> None:
tenant_id = get_tenant_id()
# Context manager closes the gRPC channel automatically.
with ConfigClient("localhost:9090", subject="quickstart-example") as client:
# get() returns str by default.
name = client.get(tenant_id, "app.name")
print(f"app.name: {name}")
# Pass a type to get a typed value — no string parsing needed.
debug = client.get(tenant_id, "app.debug", bool)
print(f"app.debug: {debug}")
rate_limit = client.get(tenant_id, "server.rate_limit", int)
print(f"server.rate_limit: {rate_limit}")
timeout = client.get(tenant_id, "server.timeout", timedelta)
print(f"server.timeout: {timeout}")
fee_rate = client.get(tenant_id, "payments.fee_rate", float)
print(f"payments.fee_rate: {fee_rate}")
# set() and set_many() for writes.
client.set(tenant_id, "app.debug", "true")
print("\nSet app.debug = true")
debug = client.get(tenant_id, "app.debug", bool)
print(f"app.debug: {debug}")
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__":
main()