Chilkat Online Tools

C++ / Datadog API Collection / Get hourly usage for CSPM

Back to Collection Items

#include <CkHttp.h>
#include <CkJsonObject.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>

void ChilkatSample(void)
    {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttp http;
    bool success;

    CkJsonObject queryParams;
    queryParams.UpdateString("start_hr","1991-02-02T07:22:34.204Z");
    queryParams.UpdateString("end_hr","1991-02-02T07:22:34.204Z");

    http.SetRequestHeader("Accept","application/json;datetime-format=rfc3339");

    CkHttpResponse *resp = http.QuickRequestParams("GET","https://api.app.ddog-gov.com/api/v1/usage/cspm",queryParams);
    if (http.get_LastMethodSuccess() == false) {
        std::cout << http.lastErrorText() << "\r\n";
        return;
    }

    CkStringBuilder sbResponseBody;
    resp->GetBodySb(sbResponseBody);

    CkJsonObject jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    std::cout << "Response Body:" << "\r\n";
    std::cout << jResp.emit() << "\r\n";

    int respStatusCode = resp->get_StatusCode();
    std::cout << "Response Status Code = " << respStatusCode << "\r\n";
    if (respStatusCode >= 400) {
        std::cout << "Response Header:" << "\r\n";
        std::cout << resp->header() << "\r\n";
        std::cout << "Failed." << "\r\n";
        delete resp;
        return;
    }

    delete resp;

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

    // {
    //   "usage": [
    //     {
    //       "aas_host_count": -80766489.56845878,
    //       "aws_host_count": 17971624.141541034,
    //       "azure_host_count": -63389392.498256706,
    //       "compliance_host_count": -36217158.04911183,
    //       "container_count": 11133759.007289752,
    //       "gcp_host_count": 50901682.36994678,
    //       "host_count": -47780028.372240715,
    //       "hour": "2018-03-17T20:30:04.637Z",
    //       "org_name": "mollit",
    //       "public_id": "laborum aliqua ex irure"
    //     },
    //     {
    //       "aas_host_count": -98211553.22625725,
    //       "aws_host_count": -72710141.40577123,
    //       "azure_host_count": 57237415.532838285,
    //       "compliance_host_count": 955332.3425777704,
    //       "container_count": -47480535.50305298,
    //       "gcp_host_count": -39047011.60659756,
    //       "host_count": 61940711.68366918,
    //       "hour": "2009-11-04T13:20:03.152Z",
    //       "org_name": "mollit adipisicing velit",
    //       "public_id": "officia amet do in"
    //     }
    //   ]
    // }

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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    const char *aas_host_count = 0;
    const char *aws_host_count = 0;
    const char *azure_host_count = 0;
    const char *compliance_host_count = 0;
    const char *container_count = 0;
    const char *gcp_host_count = 0;
    const char *host_count = 0;
    const char *hour = 0;
    const char *org_name = 0;
    const char *public_id = 0;

    int i = 0;
    int count_i = jResp.SizeOfArray("usage");
    while (i < count_i) {
        jResp.put_I(i);
        aas_host_count = jResp.stringOf("usage[i].aas_host_count");
        aws_host_count = jResp.stringOf("usage[i].aws_host_count");
        azure_host_count = jResp.stringOf("usage[i].azure_host_count");
        compliance_host_count = jResp.stringOf("usage[i].compliance_host_count");
        container_count = jResp.stringOf("usage[i].container_count");
        gcp_host_count = jResp.stringOf("usage[i].gcp_host_count");
        host_count = jResp.stringOf("usage[i].host_count");
        hour = jResp.stringOf("usage[i].hour");
        org_name = jResp.stringOf("usage[i].org_name");
        public_id = jResp.stringOf("usage[i].public_id");
        i = i + 1;
    }
    }

Curl Command

curl -G -d "start_hr=1991-02-02T07%3A22%3A34.204Z"
	-d "end_hr=1991-02-02T07%3A22%3A34.204Z"
	-H "Accept: application/json;datetime-format=rfc3339"
https://api.app.ddog-gov.com/api/v1/usage/cspm

Postman Collection Item JSON

{
  "name": "Get hourly usage for CSPM",
  "request": {
    "method": "GET",
    "header": [
      {
        "key": "Accept",
        "value": "application/json;datetime-format=rfc3339"
      }
    ],
    "url": {
      "raw": "{{baseUrl}}/api/v1/usage/cspm?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v1",
        "usage",
        "cspm"
      ],
      "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."
        }
      ]
    },
    "description": "Get hourly usage for cloud security posture management (CSPM).\n**Note:** hourly usage data for all products is now available in the [Get hourly usage by product family API](https://docs.datadoghq.com/api/latest/usage-metering/#get-hourly-usage-by-product-family). Refer to [Migrating from the V1 Hourly Usage APIs to V2](https://docs.datadoghq.com/account_management/guide/hourly-usage-migration/) for the associated migration guide."
  },
  "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/cspm?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "usage",
            "cspm"
          ],
          "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."
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json;datetime-format=rfc3339"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"usage\": [\n    {\n      \"aas_host_count\": -80766489.56845878,\n      \"aws_host_count\": 17971624.141541034,\n      \"azure_host_count\": -63389392.498256706,\n      \"compliance_host_count\": -36217158.04911183,\n      \"container_count\": 11133759.007289752,\n      \"gcp_host_count\": 50901682.36994678,\n      \"host_count\": -47780028.372240715,\n      \"hour\": \"2018-03-17T20:30:04.637Z\",\n      \"org_name\": \"mollit\",\n      \"public_id\": \"laborum aliqua ex irure\"\n    },\n    {\n      \"aas_host_count\": -98211553.22625725,\n      \"aws_host_count\": -72710141.40577123,\n      \"azure_host_count\": 57237415.532838285,\n      \"compliance_host_count\": 955332.3425777704,\n      \"container_count\": -47480535.50305298,\n      \"gcp_host_count\": -39047011.60659756,\n      \"host_count\": 61940711.68366918,\n      \"hour\": \"2009-11-04T13:20:03.152Z\",\n      \"org_name\": \"mollit adipisicing velit\",\n      \"public_id\": \"officia amet do in\"\n    }\n  ]\n}"
    },
    {
      "name": "Bad Request",
      "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/cspm?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "usage",
            "cspm"
          ],
          "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."
            }
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_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": "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/cspm?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "usage",
            "cspm"
          ],
          "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."
            }
          ]
        }
      },
      "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/cspm?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "usage",
            "cspm"
          ],
          "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."
            }
          ]
        }
      },
      "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}"
    }
  ]
}