-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_usage.py
More file actions
98 lines (80 loc) · 2.72 KB
/
basic_usage.py
File metadata and controls
98 lines (80 loc) · 2.72 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
Basic usage example for the Lumen Python SDK.
This example demonstrates the core functionality of the SDK including:
- Checking subscription status
- Tracking events
- Checking feature entitlements
- Managing seats
"""
import asyncio
import os
from lumen import (
add_seat,
get_features,
get_subscription_status,
get_usage,
is_feature_entitled,
send_event,
)
async def main() -> None:
"""Run basic usage examples."""
api_key = os.environ.get("LUMEN_API_KEY")
if not api_key:
print("Please set LUMEN_API_KEY environment variable")
return
user_id = "demo_user_123"
print("=== Lumen Python SDK - Basic Usage Examples ===\n")
print("1. Checking subscription status...")
status = await get_subscription_status(user_id=user_id)
if "error" in status:
print(f" Error: {status['error']}")
else:
print(f" Has active subscription: {status.get('hasActiveSubscription')}")
print(f" Customer ID: {status.get('customer', {}).get('id')}")
print("\n2. Sending usage event...")
response = await send_event(
name="api_call",
value=1,
user_id=user_id
)
if response:
print(f" Event sent successfully (status: {response.status_code})")
else:
print(" Failed to send event")
print("\n3. Getting usage and entitlements...")
usage = await get_usage(user_id=user_id)
if "error" in usage:
print(f" Error: {usage['error']}")
else:
entitlements = usage.get("entitlements", [])
print(f" Found {len(entitlements)} entitlements:")
for ent in entitlements[:3]:
feature = ent.get("feature", {})
print(f" - {feature.get('name')}: {ent.get('usage')}/{ent.get('limit')}")
print("\n4. Getting features as key-value pairs...")
features = await get_features(user_id=user_id)
if features:
print(" Features:")
for slug, entitled in list(features.items())[:5]:
print(f" - {slug}: {entitled}")
else:
print(" No features found")
print("\n5. Checking specific feature entitlement...")
has_premium = await is_feature_entitled(
feature="premium_feature",
user_id=user_id
)
print(f" Has premium feature: {has_premium}")
print("\n6. Adding a seat (example - may fail if not configured)...")
seat_result = await add_seat(
new_user_id="new_user_456",
organisation_user_id=user_id,
metadata={"role": "developer"}
)
if "error" in seat_result:
print(f" Info: {seat_result['error']}")
else:
print(f" Seat added successfully")
print("\n=== Examples Complete ===")
if __name__ == "__main__":
asyncio.run(main())