|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Basic example of using the Replicated Python SDK. |
| 4 | +This script initializes the replicated package, creates a customer and instance. |
| 5 | +""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +import asyncio |
| 9 | + |
| 10 | +from replicated import AsyncReplicatedClient |
| 11 | + |
| 12 | + |
| 13 | +async def main(): |
| 14 | + parser = argparse.ArgumentParser(description="Basic Replicated SDK example") |
| 15 | + parser.add_argument( |
| 16 | + "--base-url", |
| 17 | + default="https://replicated.app", |
| 18 | + help="Base URL for the Replicated API (default: https://replicated.app)", |
| 19 | + ) |
| 20 | + parser.add_argument( |
| 21 | + "--publishable-key", |
| 22 | + required=True, |
| 23 | + help="Your Replicated publishable key (required)", |
| 24 | + ) |
| 25 | + parser.add_argument( |
| 26 | + "--app-slug", required=True, help="Your application slug (required)" |
| 27 | + ) |
| 28 | + parser.add_argument( |
| 29 | + "--customer-email", |
| 30 | + default="user@example.com", |
| 31 | + help="Customer email address (default: user@example.com)", |
| 32 | + ) |
| 33 | + parser.add_argument("--channel", help="Channel for the customer (optional)") |
| 34 | + parser.add_argument("--customer-name", help="Customer name (optional)") |
| 35 | + parser.add_argument( |
| 36 | + "--status", |
| 37 | + choices=["missing", "unavailable", "ready", "updating", "degraded"], |
| 38 | + default="ready", |
| 39 | + help="Instance status (default: ready)", |
| 40 | + ) |
| 41 | + |
| 42 | + args = parser.parse_args() |
| 43 | + |
| 44 | + print("Initializing Replicated client...") |
| 45 | + print(f"Base URL: {args.base_url}") |
| 46 | + print(f"App Slug: {args.app_slug}") |
| 47 | + |
| 48 | + # Initialize the client |
| 49 | + async with AsyncReplicatedClient( |
| 50 | + publishable_key=args.publishable_key, |
| 51 | + app_slug=args.app_slug, |
| 52 | + base_url=args.base_url, |
| 53 | + ) as client: |
| 54 | + print("✓ Replicated client initialized successfully") |
| 55 | + |
| 56 | + # Create or get customer |
| 57 | + channel_info = f" (channel: {args.channel})" if args.channel else "" |
| 58 | + name_info = f" (name: {args.customer_name})" if args.customer_name else "" |
| 59 | + print( |
| 60 | + f"\nCreating/getting customer with email: " |
| 61 | + f"{args.customer_email}{channel_info}{name_info}" |
| 62 | + ) |
| 63 | + customer = await client.customer.get_or_create( |
| 64 | + email_address=args.customer_email, |
| 65 | + channel=args.channel, |
| 66 | + name=args.customer_name, |
| 67 | + ) |
| 68 | + print(f"✓ Customer created/retrieved - ID: {customer.customer_id}") |
| 69 | + |
| 70 | + # Get or create the associated instance |
| 71 | + instance = await customer.get_or_create_instance() |
| 72 | + print(f"Instance ID: {instance.instance_id}") |
| 73 | + print(f"✓ Instance created/retrieved - ID: {instance.instance_id}") |
| 74 | + |
| 75 | + # Get or create the associated instance |
| 76 | + instance = await customer.get_or_create_instance() |
| 77 | + print(f"Instance ID: {instance.instance_id}") |
| 78 | + |
| 79 | + # Set instance status |
| 80 | + await instance.set_status(args.status) |
| 81 | + print(f"✓ Instance status set to: {args.status}") |
| 82 | + |
| 83 | + # Send some metrics concurrently |
| 84 | + await asyncio.gather( |
| 85 | + instance.send_metric("cpu_usage", 0.83), |
| 86 | + instance.send_metric("memory_usage", 0.67), |
| 87 | + instance.send_metric("disk_usage", 0.45), |
| 88 | + ) |
| 89 | + print("Metrics sent successfully") |
| 90 | + |
| 91 | + print(f"Instance ID: {instance.instance_id}") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + asyncio.run(main()) |
0 commit comments