Chilkat Online Tools

PureBasic / Datadog API Collection / Get hourly usage attribution

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    queryParams.i = CkJsonObject::ckCreate()
    If queryParams.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(queryParams,"start_hr","1991-02-02T07:22:34.204Z")
    CkJsonObject::ckUpdateString(queryParams,"end_hr","1991-02-02T07:22:34.204Z")
    CkJsonObject::ckUpdateString(queryParams,"usage_type","profiled_host_usage")
    CkJsonObject::ckUpdateString(queryParams,"next_record_id","tempor Ut sed velit")
    CkJsonObject::ckUpdateString(queryParams,"tag_breakdown_keys","tempor Ut sed velit")
    CkJsonObject::ckUpdateString(queryParams,"include_descendants","true")

    CkHttp::ckSetRequestHeader(http,"Accept","application/json;datetime-format=rfc3339")

    resp.i = CkHttp::ckQuickRequestParams(http,"GET","https://api.app.ddog-gov.com/api/v1/usage/hourly-attribution",queryParams)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(queryParams)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttpResponse::ckDispose(resp)

        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(queryParams)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    CkHttpResponse::ckDispose(resp)

    ; Sample JSON response:
    ; (Sample code for parsing the JSON response is shown below)

    ; {
    ;   "metadata": {
    ;     "pagination": {
    ;       "next_record_id": "proident mollit"
    ;     }
    ;   },
    ;   "usage": [
    ;     {
    ;       "hour": "1986-01-17T07:37:35.170Z",
    ;       "org_name": "in labore reprehenderit in Exce",
    ;       "public_id": "minim deserunt dolor qui",
    ;       "region": "pariatur minim Lorem",
    ;       "tag_config_source": "ut adipisicing ut",
    ;       "tags": {
    ;         "anim__1a": [
    ;           "datadog-integrations-lab",
    ;           "datadog-integrations-lab"
    ;         ]
    ;       },
    ;       "total_usage_sum": -77225004.76702869,
    ;       "updated_at": "dolore ipsum cupidatat",
    ;       "usage_type": "snmp_usage"
    ;     },
    ;     {
    ;       "hour": "2007-03-17T08:17:32.448Z",
    ;       "org_name": "sint mollit ut et",
    ;       "public_id": "amet Excepteur officia",
    ;       "region": "proident",
    ;       "tag_config_source": "cillum",
    ;       "tags": {
    ;         "Duis_2": [
    ;           "datadog-integrations-lab",
    ;           "datadog-integrations-lab"
    ;         ]
    ;       },
    ;       "total_usage_sum": 57869801.725036174,
    ;       "updated_at": "ea in",
    ;       "usage_type": "api_usage"
    ;     }
    ;   ]
    ; }

    ; Sample code for parsing the JSON response...
    ; Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    hour.s
    org_name.s
    public_id.s
    region.s
    tag_config_source.s
    total_usage_sum.s
    updated_at.s
    usage_type.s
    j.i
    count_j.i
    strVal.s

    Next_record_id.s = CkJsonObject::ckStringOf(jResp,"metadata.pagination.next_record_id")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"usage")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        hour = CkJsonObject::ckStringOf(jResp,"usage[i].hour")
        org_name = CkJsonObject::ckStringOf(jResp,"usage[i].org_name")
        public_id = CkJsonObject::ckStringOf(jResp,"usage[i].public_id")
        region = CkJsonObject::ckStringOf(jResp,"usage[i].region")
        tag_config_source = CkJsonObject::ckStringOf(jResp,"usage[i].tag_config_source")
        total_usage_sum = CkJsonObject::ckStringOf(jResp,"usage[i].total_usage_sum")
        updated_at = CkJsonObject::ckStringOf(jResp,"usage[i].updated_at")
        usage_type = CkJsonObject::ckStringOf(jResp,"usage[i].usage_type")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jResp,"usage[i].tags.anim__1a")
        While j < count_j
            CkJsonObject::setCkJ(jResp, j)
            strVal = CkJsonObject::ckStringOf(jResp,"usage[i].tags.anim__1a[j]")
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jResp,"usage[i].tags.Duis_2")
        While j < count_j
            CkJsonObject::setCkJ(jResp, j)
            strVal = CkJsonObject::ckStringOf(jResp,"usage[i].tags.Duis_2[j]")
            j = j + 1
        Wend
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(queryParams)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

Curl Command

curl -G -d "start_hr=1991-02-02T07%3A22%3A34.204Z"
	-d "end_hr=1991-02-02T07%3A22%3A34.204Z"
	-d "usage_type=profiled_host_usage"
	-d "next_record_id=tempor%20Ut%20sed%20velit"
	-d "tag_breakdown_keys=tempor%20Ut%20sed%20velit"
	-d "include_descendants=true"
	-H "Accept: application/json;datetime-format=rfc3339"
https://api.app.ddog-gov.com/api/v1/usage/hourly-attribution

Postman Collection Item JSON

{
  "name": "Get hourly usage attribution",
  "request": {
    "method": "GET",
    "header": [
      {
        "key": "Accept",
        "value": "application/json;datetime-format=rfc3339"
      }
    ],
    "url": {
      "raw": "{{baseUrl}}/api/v1/usage/hourly-attribution?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z&usage_type=profiled_host_usage&next_record_id=tempor Ut sed velit&tag_breakdown_keys=tempor Ut sed velit&include_descendants=true",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v1",
        "usage",
        "hourly-attribution"
      ],
      "query": [
        {
          "key": "start_hr",
          "value": "1991-02-02T07:22:34.204Z",
          "description": "(Required) Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage beginning at this hour."
        },
        {
          "key": "end_hr",
          "value": "1991-02-02T07:22:34.204Z",
          "description": "Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage ending\n**before** this hour."
        },
        {
          "key": "usage_type",
          "value": "profiled_host_usage",
          "description": "(Required) Usage type to retrieve."
        },
        {
          "key": "next_record_id",
          "value": "tempor Ut sed velit",
          "description": "List following results with a next_record_id provided in the previous query."
        },
        {
          "key": "tag_breakdown_keys",
          "value": "tempor Ut sed velit",
          "description": "Comma separated list of tags used to group usage. If no value is provided the usage will not be broken down by tags.\n\nTo see which tags are available, look for the value of `tag_config_source` in the API response."
        },
        {
          "key": "include_descendants",
          "value": "true",
          "description": "Include child org usage in the response. Defaults to `true`."
        }
      ]
    },
    "description": "Get hourly usage attribution. Multi-region data is available starting March 1, 2023.\n\nThis API endpoint is paginated. To make sure you receive all records, check if the value of `next_record_id` is\nset in the response. If it is, make another request and pass `next_record_id` as a parameter.\nPseudo code example:\n\n```\nresponse := GetHourlyUsageAttribution(start_month)\ncursor := response.metadata.pagination.next_record_id\nWHILE cursor != null BEGIN\n  sleep(5 seconds)  # Avoid running into rate limit\n  response := GetHourlyUsageAttribution(start_month, next_record_id=cursor)\n  cursor := response.metadata.pagination.next_record_id\nEND\n```"
  },
  "response": [
    {
      "name": "OK",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json;datetime-format=rfc3339"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/v1/usage/hourly-attribution?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z&usage_type=profiled_host_usage&next_record_id=tempor Ut sed velit&tag_breakdown_keys=tempor Ut sed velit&include_descendants=true",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "usage",
            "hourly-attribution"
          ],
          "query": [
            {
              "key": "start_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "(Required) Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage beginning at this hour."
            },
            {
              "key": "end_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage ending\n**before** this hour."
            },
            {
              "key": "usage_type",
              "value": "profiled_host_usage",
              "description": "(Required) Usage type to retrieve."
            },
            {
              "key": "next_record_id",
              "value": "tempor Ut sed velit",
              "description": "List following results with a next_record_id provided in the previous query."
            },
            {
              "key": "tag_breakdown_keys",
              "value": "tempor Ut sed velit",
              "description": "Comma separated list of tags used to group usage. If no value is provided the usage will not be broken down by tags.\n\nTo see which tags are available, look for the value of `tag_config_source` in the API response."
            },
            {
              "key": "include_descendants",
              "value": "true",
              "description": "Include child org usage in the response. Defaults to `true`."
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json;datetime-format=rfc3339"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"metadata\": {\n    \"pagination\": {\n      \"next_record_id\": \"proident mollit\"\n    }\n  },\n  \"usage\": [\n    {\n      \"hour\": \"1986-01-17T07:37:35.170Z\",\n      \"org_name\": \"in labore reprehenderit in Exce\",\n      \"public_id\": \"minim deserunt dolor qui\",\n      \"region\": \"pariatur minim Lorem\",\n      \"tag_config_source\": \"ut adipisicing ut\",\n      \"tags\": {\n        \"anim__1a\": [\n          \"datadog-integrations-lab\",\n          \"datadog-integrations-lab\"\n        ]\n      },\n      \"total_usage_sum\": -77225004.76702869,\n      \"updated_at\": \"dolore ipsum cupidatat\",\n      \"usage_type\": \"snmp_usage\"\n    },\n    {\n      \"hour\": \"2007-03-17T08:17:32.448Z\",\n      \"org_name\": \"sint mollit ut et\",\n      \"public_id\": \"amet Excepteur officia\",\n      \"region\": \"proident\",\n      \"tag_config_source\": \"cillum\",\n      \"tags\": {\n        \"Duis_2\": [\n          \"datadog-integrations-lab\",\n          \"datadog-integrations-lab\"\n        ]\n      },\n      \"total_usage_sum\": 57869801.725036174,\n      \"updated_at\": \"ea in\",\n      \"usage_type\": \"api_usage\"\n    }\n  ]\n}"
    },
    {
      "name": "Forbidden - User is not authorized",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json;datetime-format=rfc3339"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/v1/usage/hourly-attribution?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z&usage_type=profiled_host_usage&next_record_id=tempor Ut sed velit&tag_breakdown_keys=tempor Ut sed velit&include_descendants=true",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "usage",
            "hourly-attribution"
          ],
          "query": [
            {
              "key": "start_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "(Required) Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage beginning at this hour."
            },
            {
              "key": "end_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage ending\n**before** this hour."
            },
            {
              "key": "usage_type",
              "value": "profiled_host_usage",
              "description": "(Required) Usage type to retrieve."
            },
            {
              "key": "next_record_id",
              "value": "tempor Ut sed velit",
              "description": "List following results with a next_record_id provided in the previous query."
            },
            {
              "key": "tag_breakdown_keys",
              "value": "tempor Ut sed velit",
              "description": "Comma separated list of tags used to group usage. If no value is provided the usage will not be broken down by tags.\n\nTo see which tags are available, look for the value of `tag_config_source` in the API response."
            },
            {
              "key": "include_descendants",
              "value": "true",
              "description": "Include child org usage in the response. Defaults to `true`."
            }
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json;datetime-format=rfc3339"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    },
    {
      "name": "Too many requests",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json;datetime-format=rfc3339"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/v1/usage/hourly-attribution?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z&usage_type=profiled_host_usage&next_record_id=tempor Ut sed velit&tag_breakdown_keys=tempor Ut sed velit&include_descendants=true",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "usage",
            "hourly-attribution"
          ],
          "query": [
            {
              "key": "start_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "(Required) Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage beginning at this hour."
            },
            {
              "key": "end_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage ending\n**before** this hour."
            },
            {
              "key": "usage_type",
              "value": "profiled_host_usage",
              "description": "(Required) Usage type to retrieve."
            },
            {
              "key": "next_record_id",
              "value": "tempor Ut sed velit",
              "description": "List following results with a next_record_id provided in the previous query."
            },
            {
              "key": "tag_breakdown_keys",
              "value": "tempor Ut sed velit",
              "description": "Comma separated list of tags used to group usage. If no value is provided the usage will not be broken down by tags.\n\nTo see which tags are available, look for the value of `tag_config_source` in the API response."
            },
            {
              "key": "include_descendants",
              "value": "true",
              "description": "Include child org usage in the response. Defaults to `true`."
            }
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json;datetime-format=rfc3339"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    }
  ]
}