|
4 | 4 |
|
5 | 5 | from datadog_api_client import ApiClient, Configuration |
6 | 6 | from datadog_api_client.v1.api.usage_metering_api import UsageMeteringApi |
| 7 | +from datadog_api_client.v2.api.usage_metering_api import UsageMeteringApi as UsageMeteringApiV2 |
7 | 8 | from datetime import datetime |
8 | 9 | from dateutil.tz import tzutc |
9 | 10 |
|
10 | 11 | configuration = Configuration() |
11 | 12 | with ApiClient(configuration) as api_client: |
12 | | - api_instance = UsageMeteringApi(api_client) |
13 | | - response = api_instance.get_usage_summary( |
| 13 | + # Step 1: fetch the canonical field lists from the v2 discovery endpoint. |
| 14 | + v2_instance = UsageMeteringApiV2(api_client) |
| 15 | + fields_response = v2_instance.get_usage_summary_available_fields() |
| 16 | + attrs = fields_response.data.attributes |
| 17 | + response_fields = attrs.response_fields # Layer 1 keys |
| 18 | + date_fields = attrs.date_fields # Layer 2 keys |
| 19 | + date_org_fields = attrs.date_org_fields # Layer 3 keys |
| 20 | + |
| 21 | + # Step 2: call the v1 summary endpoint with org details included. |
| 22 | + v1_instance = UsageMeteringApi(api_client) |
| 23 | + resp = v1_instance.get_usage_summary( |
14 | 24 | start_month=datetime(2021, 11, 11, 11, 11, 11, 111000, tzinfo=tzutc()), |
| 25 | + include_org_details=True, |
15 | 26 | ) |
16 | 27 |
|
17 | | - print(response) |
| 28 | + # Layer 1 – top-level UsageSummaryResponse additionalProperties |
| 29 | + print("=== Layer 1: UsageSummaryResponse ===") |
| 30 | + for field in response_fields: |
| 31 | + value = resp.get(field) |
| 32 | + if value is not None: |
| 33 | + print(f" {field}: {value}") |
| 34 | + |
| 35 | + # Layer 2 – per-date UsageSummaryDate additionalProperties |
| 36 | + print("=== Layer 2: UsageSummaryDate (usage[]) ===") |
| 37 | + for date_entry in resp.get("usage") or []: |
| 38 | + date_label = date_entry.get("date", "unknown") |
| 39 | + for field in date_fields: |
| 40 | + value = date_entry.get(field) |
| 41 | + if value is not None: |
| 42 | + print(f" [{date_label}] {field}: {value}") |
| 43 | + |
| 44 | + # Layer 3 – per-org UsageSummaryDateOrg additionalProperties |
| 45 | + print(f" === Layer 3: UsageSummaryDateOrg (usage[].orgs[]) for {date_label} ===") |
| 46 | + for org in date_entry.get("orgs") or []: |
| 47 | + org_label = org.get("name", org.get("id", "unknown")) |
| 48 | + for field in date_org_fields: |
| 49 | + value = org.get(field) |
| 50 | + if value is not None: |
| 51 | + print(f" [{org_label}] {field}: {value}") |
0 commit comments