Chilkat Online Tools

delphiDll / Datadog API Collection / Query scalar data across multiple products

Back to Collection Items

var
http: HCkHttp;
success: Boolean;
json: HCkJsonObject;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
name: PWideChar;
v_type: PWideChar;
j: Integer;
count_j: Integer;
k: Integer;
count_k: Integer;
strVal: PWideChar;
v_Type: PWideChar;
errors: PWideChar;
i: Integer;
count_i: Integer;

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

http := CkHttp_Create();

// Use this online tool to generate code from sample JSON: Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "data": {
//     "type": "scalar_request",
//     "attributes": {
//       "to": "<long>",
//       "from": "<long>",
//       "queries": [
//         {
//           "data_source": "metrics",
//           "query": "<string>",
//           "aggregator": "avg",
//           "name": "<string>"
//         },
//         {
//           "data_source": "metrics",
//           "query": "<string>",
//           "aggregator": "avg",
//           "name": "<string>"
//         }
//       ],
//       "formulas": [
//         {
//           "formula": "<string>",
//           "limit": {
//             "count": "<integer>",
//             "order": "desc"
//           }
//         },
//         {
//           "formula": "<string>",
//           "limit": {
//             "count": "<integer>",
//             "order": "desc"
//           }
//         }
//       ]
//     }
//   }
// }

json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'data.type','scalar_request');
CkJsonObject_UpdateString(json,'data.attributes.to','<long>');
CkJsonObject_UpdateString(json,'data.attributes.from','<long>');
CkJsonObject_UpdateString(json,'data.attributes.queries[0].data_source','metrics');
CkJsonObject_UpdateString(json,'data.attributes.queries[0].query','<string>');
CkJsonObject_UpdateString(json,'data.attributes.queries[0].aggregator','avg');
CkJsonObject_UpdateString(json,'data.attributes.queries[0].name','<string>');
CkJsonObject_UpdateString(json,'data.attributes.queries[1].data_source','metrics');
CkJsonObject_UpdateString(json,'data.attributes.queries[1].query','<string>');
CkJsonObject_UpdateString(json,'data.attributes.queries[1].aggregator','avg');
CkJsonObject_UpdateString(json,'data.attributes.queries[1].name','<string>');
CkJsonObject_UpdateString(json,'data.attributes.formulas[0].formula','<string>');
CkJsonObject_UpdateString(json,'data.attributes.formulas[0].limit.count','<integer>');
CkJsonObject_UpdateString(json,'data.attributes.formulas[0].limit.order','desc');
CkJsonObject_UpdateString(json,'data.attributes.formulas[1].formula','<string>');
CkJsonObject_UpdateString(json,'data.attributes.formulas[1].limit.count','<integer>');
CkJsonObject_UpdateString(json,'data.attributes.formulas[1].limit.order','desc');

CkHttp_SetRequestHeader(http,'Content-Type','application/json');
CkHttp_SetRequestHeader(http,'Accept','application/json');

resp := CkHttp_PostJson3(http,'https://api.app.ddog-gov.com/api/v2/query/scalar','application/json',json);
if (CkHttp_getLastMethodSuccess(http) = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

sbResponseBody := CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);

jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));

respStatusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkHttpResponse__header(resp));
    Memo1.Lines.Add('Failed.');
    CkHttpResponse_Dispose(resp);
    Exit;
  end;
CkHttpResponse_Dispose(resp);

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

// {
//   "data": {
//     "attributes": {
//       "columns": [
//         {
//           "name": "<string>",
//           "type": "<string>",
//           "values": [
//             [
//               "<string>",
//               "<string>"
//             ],
//             [
//               "<string>",
//               "<string>"
//             ]
//           ]
//         },
//         {
//           "name": "<string>",
//           "type": "<string>",
//           "values": [
//             [
//               "<string>",
//               "<string>"
//             ],
//             [
//               "<string>",
//               "<string>"
//             ]
//           ]
//         }
//       ]
//     },
//     "type": "scalar_response"
//   },
//   "errors": "<string>"
// }

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

v_Type := CkJsonObject__stringOf(jResp,'data.type');
errors := CkJsonObject__stringOf(jResp,'errors');
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'data.attributes.columns');
while i < count_i do
  begin
    CkJsonObject_putI(jResp,i);
    name := CkJsonObject__stringOf(jResp,'data.attributes.columns[i].name');
    v_type := CkJsonObject__stringOf(jResp,'data.attributes.columns[i].type');
    j := 0;
    count_j := CkJsonObject_SizeOfArray(jResp,'data.attributes.columns[i].values');
    while j < count_j do
      begin
        CkJsonObject_putJ(jResp,j);
        k := 0;
        count_k := CkJsonObject_SizeOfArray(jResp,'data.attributes.columns[i].values[j]');
        while k < count_k do
          begin
            CkJsonObject_putK(jResp,k);
            strVal := CkJsonObject__stringOf(jResp,'data.attributes.columns[i].values[j][k]');
            k := k + 1;
          end;

        j := j + 1;
      end;

    i := i + 1;
  end;

CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Accept: application/json"
	-d '{
  "data": {
    "type": "scalar_request",
    "attributes": {
      "to": "<long>",
      "from": "<long>",
      "queries": [
        {
          "data_source": "metrics",
          "query": "<string>",
          "aggregator": "avg",
          "name": "<string>"
        },
        {
          "data_source": "metrics",
          "query": "<string>",
          "aggregator": "avg",
          "name": "<string>"
        }
      ],
      "formulas": [
        {
          "formula": "<string>",
          "limit": {
            "count": "<integer>",
            "order": "desc"
          }
        },
        {
          "formula": "<string>",
          "limit": {
            "count": "<integer>",
            "order": "desc"
          }
        }
      ]
    }
  }
}'
https://api.app.ddog-gov.com/api/v2/query/scalar

Postman Collection Item JSON

{
  "name": "Query scalar data across multiple products",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"data\": {\n    \"type\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ]\n    }\n  }\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v2/query/scalar",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v2",
        "query",
        "scalar"
      ]
    },
    "description": "Query scalar values (as seen on Query Value, Table, and Toplist widgets).\nMultiple data sources are supported with the ability to\nprocess the data using formulas and functions."
  },
  "response": [
    {
      "name": "OK",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"type\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ]\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/scalar",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "scalar"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"data\": {\n    \"attributes\": {\n      \"columns\": [\n        {\n          \"name\": \"<string>\",\n          \"type\": \"<string>\",\n          \"values\": [\n            [\n              \"<string>\",\n              \"<string>\"\n            ],\n            [\n              \"<string>\",\n              \"<string>\"\n            ]\n          ]\n        },\n        {\n          \"name\": \"<string>\",\n          \"type\": \"<string>\",\n          \"values\": [\n            [\n              \"<string>\",\n              \"<string>\"\n            ],\n            [\n              \"<string>\",\n              \"<string>\"\n            ]\n          ]\n        }\n      ]\n    },\n    \"type\": \"scalar_response\"\n  },\n  \"errors\": \"<string>\"\n}"
    },
    {
      "name": "Bad Request",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"type\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ]\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/scalar",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "scalar"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"<string>\",\n    \"<string>\"\n  ]\n}"
    },
    {
      "name": "Unauthorized",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"type\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ]\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/scalar",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "scalar"
          ]
        }
      },
      "status": "Unauthorized",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"<string>\",\n    \"<string>\"\n  ]\n}"
    },
    {
      "name": "Forbidden",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"type\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ]\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/scalar",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "scalar"
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"<string>\",\n    \"<string>\"\n  ]\n}"
    },
    {
      "name": "Too many requests",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"type\": \"scalar_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"aggregator\": \"avg\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ]\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/scalar",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "scalar"
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"<string>\",\n    \"<string>\"\n  ]\n}"
    }
  ]
}