Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions examples/v1/alerts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Examples for the Alerts resource.

Covers creating and archiving threshold alerts.

Usage:
export METRONOME_BEARER_TOKEN="your-api-token"
python examples/v1/alerts.py
"""

from metronome import Metronome

client = Metronome()


def create_alert():
"""Create a threshold alert that fires when a condition is met."""
response = client.v1.alerts.create(
alert_type="spend_threshold_reached",
name="High Spend Alert",
threshold=10000.0,
# customer_id="13117714-3f05-48e5-a6e9-a66093f13b4d", # Specific customer (omit for all)
# evaluate_on_create=True, # Evaluate immediately for existing customers
)
print(f"Created alert: {response.data}")


def create_usage_alert():
"""Create an alert that fires when usage reaches a threshold."""
response = client.v1.alerts.create(
alert_type="usage_threshold_reached",
name="API Usage Alert",
threshold=1000000.0,
billable_metric_id="b3f3e5a4-1b2c-4d5e-9f0a-1b2c3d4e5f6a",
)
print(f"Created usage alert: {response.data}")


def create_low_credit_alert():
"""Create an alert that fires when credit balance drops below a threshold."""
response = client.v1.alerts.create(
alert_type="low_credit_balance_reached",
name="Low Credit Balance",
threshold=500.0,
)
print(f"Created credit alert: {response.data}")


def archive_alert():
"""Archive an alert."""
response = client.v1.alerts.archive(
id="a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d",
)
print(f"Archived alert: {response.data}")


if __name__ == "__main__":
# Uncomment the example you want to run:
# create_alert()
# create_usage_alert()
# create_low_credit_alert()
# archive_alert()
pass
27 changes: 27 additions & 0 deletions examples/v1/audit_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Examples for the Audit Logs resource.

Covers listing audit log entries.

Usage:
export METRONOME_BEARER_TOKEN="your-api-token"
python examples/v1/audit_logs.py
"""

from metronome import Metronome

client = Metronome()


def list_audit_logs():
"""List audit log entries with auto-pagination."""
for log in client.v1.audit_logs.list(
# starting_on="2025-01-01T00:00:00Z", # Filter by date range
# ending_before="2025-02-01T00:00:00Z",
):
print(f"Audit log: {log}")


if __name__ == "__main__":
# Uncomment the example you want to run:
# list_audit_logs()
pass
66 changes: 66 additions & 0 deletions examples/v1/billable_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Examples for the Billable Metrics resource.

Covers creating, retrieving, listing, and archiving billable metrics.

Usage:
export METRONOME_BEARER_TOKEN="your-api-token"
python examples/v1/billable_metrics.py
"""

from metronome import Metronome

client = Metronome()


def create_billable_metric():
"""Create a new billable metric using aggregation."""
response = client.v1.billable_metrics.create(
name="CPU Seconds",
aggregation_type="SUM",
aggregation_key="cpu_seconds",
event_type_filter={"in_values": ["heartbeat"]},
# group_keys=[["region"]], # Group usage by region
# property_filters=[{"name": "cluster_id", "exists": True}],
)
print(f"Created billable metric: {response.data}")


def create_billable_metric_with_sql():
"""Create a billable metric using a SQL query."""
response = client.v1.billable_metrics.create(
name="Active Users",
sql="SELECT COUNT(DISTINCT user_id) as value FROM events WHERE event_type = 'login'",
)
print(f"Created billable metric: {response.data}")


def retrieve_billable_metric():
"""Retrieve a specific billable metric by ID."""
response = client.v1.billable_metrics.retrieve(
billable_metric_id="b3f3e5a4-1b2c-4d5e-9f0a-1b2c3d4e5f6a",
)
print(f"Billable metric: {response.data}")


def list_billable_metrics():
"""List all billable metrics with auto-pagination."""
for metric in client.v1.billable_metrics.list():
print(f"Metric: {metric}")


def archive_billable_metric():
"""Archive a billable metric."""
response = client.v1.billable_metrics.archive(
id="b3f3e5a4-1b2c-4d5e-9f0a-1b2c3d4e5f6a",
)
print(f"Archived: {response.data}")


if __name__ == "__main__":
# Uncomment the example you want to run:
# create_billable_metric()
# create_billable_metric_with_sql()
# retrieve_billable_metric()
# list_billable_metrics()
# archive_billable_metric()
pass
44 changes: 44 additions & 0 deletions examples/v1/contract_named_schedules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Examples for the Contract Named Schedules resource.

Covers retrieving and updating named schedules on contracts.

Usage:
export METRONOME_BEARER_TOKEN="your-api-token"
python examples/v1/contract_named_schedules.py
"""

from datetime import datetime

from metronome import Metronome

client = Metronome()


def retrieve_named_schedule():
"""Retrieve a named schedule for a contract."""
response = client.v1.contracts.named_schedules.retrieve(
contract_id="d7abd0cd-4ae9-4db7-8676-e986a4ebd8dc",
customer_id="13117714-3f05-48e5-a6e9-a66093f13b4d",
schedule_name="my-schedule",
)
print(f"Named schedule: {response.data}")


def update_named_schedule():
"""Update a named schedule on a contract."""
client.v1.contracts.named_schedules.update(
contract_id="d7abd0cd-4ae9-4db7-8676-e986a4ebd8dc",
customer_id="13117714-3f05-48e5-a6e9-a66093f13b4d",
schedule_name="my-schedule",
starting_at=datetime.fromisoformat("2025-01-01T00:00:00"),
value={"custom_field": "custom_value"},
# ending_before=datetime.fromisoformat("2026-01-01T00:00:00"),
)
print("Contract named schedule updated")


if __name__ == "__main__":
# Uncomment the example you want to run:
# retrieve_named_schedule()
# update_named_schedule()
pass
67 changes: 67 additions & 0 deletions examples/v1/contract_products.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Examples for the Contract Products resource.

Covers creating, retrieving, updating, listing, and archiving contract products.

Usage:
export METRONOME_BEARER_TOKEN="your-api-token"
python examples/v1/contract_products.py
"""

from datetime import datetime

from metronome import Metronome

client = Metronome()


def create_product():
"""Create a new contract product."""
response = client.v1.contracts.products.create(
name="API Calls",
type="USAGE",
# type can be: USAGE, FIXED, COMPOSITE, SUBSCRIPTION, PROFESSIONAL_SERVICE, PRO_SERVICE
)
print(f"Created product: {response.data}")


def retrieve_product():
"""Retrieve a specific product by ID."""
response = client.v1.contracts.products.retrieve(
id="b3f3e5a4-1b2c-4d5e-9f0a-1b2c3d4e5f6a",
)
print(f"Product: {response.data}")


def update_product():
"""Update a product (e.g., change name, add tags)."""
response = client.v1.contracts.products.update(
product_id="b3f3e5a4-1b2c-4d5e-9f0a-1b2c3d4e5f6a",
starting_at=datetime.fromisoformat("2025-01-01T00:00:00"),
# name="Updated API Calls",
# tags=["compute", "api"],
)
print(f"Updated product: {response.data}")


def list_products():
"""List all contract products with auto-pagination."""
for product in client.v1.contracts.products.list():
print(f"Product: {product}")


def archive_product():
"""Archive a contract product."""
response = client.v1.contracts.products.archive(
product_id="b3f3e5a4-1b2c-4d5e-9f0a-1b2c3d4e5f6a",
)
print(f"Archived: {response.data}")


if __name__ == "__main__":
# Uncomment the example you want to run:
# create_product()
# retrieve_product()
# update_product()
# list_products()
# archive_product()
pass
Loading
Loading