|
| 1 | +"""Basic asynchronous Replane client example. |
| 2 | +
|
| 3 | +This example demonstrates how to use the AsyncReplaneClient |
| 4 | +for async/await applications. |
| 5 | +""" |
| 6 | + |
| 7 | +import asyncio |
| 8 | + |
| 9 | +from replane import AsyncReplaneClient |
| 10 | + |
| 11 | +# Configuration - replace with your actual values |
| 12 | +BASE_URL = "https://your-replane-server.com" |
| 13 | +SDK_KEY = "sk_your_sdk_key_here" |
| 14 | + |
| 15 | + |
| 16 | +async def main(): |
| 17 | + # Using async context manager (recommended) |
| 18 | + async with AsyncReplaneClient( |
| 19 | + base_url=BASE_URL, |
| 20 | + sdk_key=SDK_KEY, |
| 21 | + # Optional: set default context for all evaluations |
| 22 | + context={"environment": "production"}, |
| 23 | + # Optional: fallback values if server is unavailable |
| 24 | + fallbacks={ |
| 25 | + "feature-enabled": False, |
| 26 | + "max-items": 10, |
| 27 | + }, |
| 28 | + # Optional: enable debug logging |
| 29 | + debug=True, |
| 30 | + ) as client: |
| 31 | + # Read a boolean feature flag (sync - reads from local cache) |
| 32 | + is_feature_enabled = client.get("feature-enabled") |
| 33 | + print(f"Feature enabled: {is_feature_enabled}") |
| 34 | + |
| 35 | + # Read a numeric config |
| 36 | + max_items = client.get("max-items") |
| 37 | + print(f"Max items: {max_items}") |
| 38 | + |
| 39 | + # Read with context for override evaluation |
| 40 | + rate_limit = client.get( |
| 41 | + "rate-limit", |
| 42 | + context={"plan": "premium", "user_id": "user-123"}, |
| 43 | + ) |
| 44 | + print(f"Rate limit: {rate_limit}") |
| 45 | + |
| 46 | + # Read with default value if config doesn't exist |
| 47 | + timeout = client.get("request-timeout", default=30) |
| 48 | + print(f"Timeout: {timeout}") |
| 49 | + |
| 50 | + # Keep running to receive real-time updates |
| 51 | + print("\nListening for config updates (Ctrl+C to stop)...") |
| 52 | + try: |
| 53 | + while True: |
| 54 | + await asyncio.sleep(5) |
| 55 | + # Re-read to see any updates |
| 56 | + current_value = client.get("feature-enabled") |
| 57 | + print(f"Current feature-enabled value: {current_value}") |
| 58 | + except KeyboardInterrupt: |
| 59 | + print("\nStopping...") |
| 60 | + |
| 61 | + |
| 62 | +async def example_manual_lifecycle(): |
| 63 | + """Example showing manual connect/close lifecycle.""" |
| 64 | + client = AsyncReplaneClient( |
| 65 | + base_url=BASE_URL, |
| 66 | + sdk_key=SDK_KEY, |
| 67 | + ) |
| 68 | + |
| 69 | + try: |
| 70 | + # Connect and wait for initial configs |
| 71 | + await client.connect(wait=True) |
| 72 | + |
| 73 | + # Now you can read configs |
| 74 | + value = client.get("my-config") |
| 75 | + print(f"Config value: {value}") |
| 76 | + |
| 77 | + finally: |
| 78 | + # Always close the client when done |
| 79 | + await client.close() |
| 80 | + |
| 81 | + |
| 82 | +async def example_with_subscriptions(): |
| 83 | + """Example showing how to subscribe to config changes.""" |
| 84 | + async with AsyncReplaneClient( |
| 85 | + base_url=BASE_URL, |
| 86 | + sdk_key=SDK_KEY, |
| 87 | + ) as client: |
| 88 | + # Subscribe to all config changes |
| 89 | + def on_any_change(name, config): |
| 90 | + print(f"Config '{name}' changed to: {config.value}") |
| 91 | + |
| 92 | + unsubscribe_all = client.subscribe(on_any_change) |
| 93 | + |
| 94 | + # Subscribe to a specific config |
| 95 | + def on_feature_change(config): |
| 96 | + print(f"Feature flag updated: {config.value}") |
| 97 | + |
| 98 | + unsubscribe_feature = client.subscribe_config("feature-enabled", on_feature_change) |
| 99 | + |
| 100 | + # Keep running to receive updates |
| 101 | + print("Listening for changes...") |
| 102 | + await asyncio.sleep(60) |
| 103 | + |
| 104 | + # Unsubscribe when done |
| 105 | + unsubscribe_all() |
| 106 | + unsubscribe_feature() |
| 107 | + |
| 108 | + |
| 109 | +async def example_async_callback(): |
| 110 | + """Example showing async callbacks for config changes.""" |
| 111 | + async with AsyncReplaneClient( |
| 112 | + base_url=BASE_URL, |
| 113 | + sdk_key=SDK_KEY, |
| 114 | + ) as client: |
| 115 | + # Async callbacks are supported |
| 116 | + async def on_change(name, config): |
| 117 | + print(f"Config '{name}' changed") |
| 118 | + # Can do async operations here |
| 119 | + await asyncio.sleep(0.1) |
| 120 | + print(f"Processed change for '{name}'") |
| 121 | + |
| 122 | + client.subscribe(on_change) |
| 123 | + |
| 124 | + await asyncio.sleep(60) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + asyncio.run(main()) |
0 commit comments