Chilkat Online Tools

delphiDll / Datadog API Collection / Create a notebook

Back to Collection Items

var
http: HCkHttp;
success: Boolean;
json: HCkJsonObject;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
id: PWideChar;
v_type: PWideChar;
DefinitionType: PWideChar;
Text: PWideChar;
Id: Integer;
v_Type: PWideChar;
Live_span: PWideChar;
Name: PWideChar;
Created_at: PWideChar;
Disabled: Boolean;
v_Email: PWideChar;
Handle: PWideChar;
Icon: PWideChar;
AuthorName: PWideChar;
Status: PWideChar;
Title: PWideChar;
Verified: Boolean;
Created: PWideChar;
Is_template: Boolean;
Take_snapshots: Boolean;
MetadataType: PWideChar;
Modified: PWideChar;
AttributesStatus: 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": {
//     "attributes": {
//       "cells": [
//         {
//           "attributes": {
//             "definition": {
//               "text": "## Some test markdown\n\nWith some example content.",
//               "type": "markdown"
//             }
//           },
//           "type": "notebook_cells"
//         },
//         {
//           "attributes": {
//             "definition": {
//               "requests": [
//                 {
//                   "display_type": "line",
//                   "q": "avg:system.load.1{*}",
//                   "style": {
//                     "line_type": "solid",
//                     "line_width": "normal",
//                     "palette": "dog_classic"
//                   }
//                 }
//               ],
//               "show_legend": true,
//               "type": "timeseries",
//               "yaxis": {
//                 "scale": "linear"
//               }
//             },
//             "graph_size": "m",
//             "split_by": {
//               "keys": [
//               ],
//               "tags": [
//               ]
//             },
//             "time": null
//           },
//           "type": "notebook_cells"
//         }
//       ],
//       "name": "Example Notebook",
//       "time": {
//         "live_span": "1h"
//       }
//     },
//     "type": "notebooks"
//   }
// }

json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'data.attributes.cells[0].attributes.definition.text','## Some test markdown' + #10 + #10 + 'With some example content.');
CkJsonObject_UpdateString(json,'data.attributes.cells[0].attributes.definition.type','markdown');
CkJsonObject_UpdateString(json,'data.attributes.cells[0].type','notebook_cells');
CkJsonObject_UpdateString(json,'data.attributes.cells[1].attributes.definition.requests[0].display_type','line');
CkJsonObject_UpdateString(json,'data.attributes.cells[1].attributes.definition.requests[0].q','avg:system.load.1{*}');
CkJsonObject_UpdateString(json,'data.attributes.cells[1].attributes.definition.requests[0].style.line_type','solid');
CkJsonObject_UpdateString(json,'data.attributes.cells[1].attributes.definition.requests[0].style.line_width','normal');
CkJsonObject_UpdateString(json,'data.attributes.cells[1].attributes.definition.requests[0].style.palette','dog_classic');
CkJsonObject_UpdateBool(json,'data.attributes.cells[1].attributes.definition.show_legend',True);
CkJsonObject_UpdateString(json,'data.attributes.cells[1].attributes.definition.type','timeseries');
CkJsonObject_UpdateString(json,'data.attributes.cells[1].attributes.definition.yaxis.scale','linear');
CkJsonObject_UpdateString(json,'data.attributes.cells[1].attributes.graph_size','m');
CkJsonObject_UpdateNewArray(json,'data.attributes.cells[1].attributes.split_by.keys');
CkJsonObject_UpdateNewArray(json,'data.attributes.cells[1].attributes.split_by.tags');
CkJsonObject_UpdateNull(json,'data.attributes.cells[1].attributes.time');
CkJsonObject_UpdateString(json,'data.attributes.cells[1].type','notebook_cells');
CkJsonObject_UpdateString(json,'data.attributes.name','Example Notebook');
CkJsonObject_UpdateString(json,'data.attributes.time.live_span','1h');
CkJsonObject_UpdateString(json,'data.type','notebooks');

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

resp := CkHttp_PostJson3(http,'https://api.app.ddog-gov.com/api/v1/notebooks','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": {
//     "id": 123456,
//     "type": "notebooks",
//     "attributes": {
//       "cells": [
//         {
//           "id": "abcd1234",
//           "type": "notebook_cells",
//           "attributes": {
//             "definition": {
//               "type": "markdown",
//               "text": "# Example Header \nexample content"
//             }
//           }
//         },
//         {
//           "id": "abcd1234",
//           "type": "notebook_cells",
//           "attributes": {
//             "definition": {
//               "type": "markdown",
//               "text": "# Example Header \nexample content"
//             }
//           }
//         }
//       ],
//       "time": {
//         "live_span": "1h"
//       },
//       "name": "Example Notebook",
//       "author": {
//         "created_at": "1944-02-06T04:58:13.667Z",
//         "disabled": false,
//         "email": "tempor exercitation",
//         "handle": "est eu aliquip officia",
//         "icon": "nostrud n",
//         "name": "dolor dolor",
//         "status": "anim Ut pariatur",
//         "title": "eu",
//         "verified": true
//       },
//       "created": "2021-02-24T23:14:15.173964+00:00",
//       "metadata": {
//         "is_template": false,
//         "take_snapshots": false,
//         "type": "investigation"
//       },
//       "modified": "2021-02-24T23:15:23.274966+00:00",
//       "status": "published"
//     }
//   }
// }

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

Id := CkJsonObject_IntOf(jResp,'data.id');
v_Type := CkJsonObject__stringOf(jResp,'data.type');
Live_span := CkJsonObject__stringOf(jResp,'data.attributes.time.live_span');
Name := CkJsonObject__stringOf(jResp,'data.attributes.name');
Created_at := CkJsonObject__stringOf(jResp,'data.attributes.author.created_at');
Disabled := CkJsonObject_BoolOf(jResp,'data.attributes.author.disabled');
v_Email := CkJsonObject__stringOf(jResp,'data.attributes.author.email');
Handle := CkJsonObject__stringOf(jResp,'data.attributes.author.handle');
Icon := CkJsonObject__stringOf(jResp,'data.attributes.author.icon');
AuthorName := CkJsonObject__stringOf(jResp,'data.attributes.author.name');
Status := CkJsonObject__stringOf(jResp,'data.attributes.author.status');
Title := CkJsonObject__stringOf(jResp,'data.attributes.author.title');
Verified := CkJsonObject_BoolOf(jResp,'data.attributes.author.verified');
Created := CkJsonObject__stringOf(jResp,'data.attributes.created');
Is_template := CkJsonObject_BoolOf(jResp,'data.attributes.metadata.is_template');
Take_snapshots := CkJsonObject_BoolOf(jResp,'data.attributes.metadata.take_snapshots');
MetadataType := CkJsonObject__stringOf(jResp,'data.attributes.metadata.type');
Modified := CkJsonObject__stringOf(jResp,'data.attributes.modified');
AttributesStatus := CkJsonObject__stringOf(jResp,'data.attributes.status');
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'data.attributes.cells');
while i < count_i do
  begin
    CkJsonObject_putI(jResp,i);
    id := CkJsonObject__stringOf(jResp,'data.attributes.cells[i].id');
    v_type := CkJsonObject__stringOf(jResp,'data.attributes.cells[i].type');
    DefinitionType := CkJsonObject__stringOf(jResp,'data.attributes.cells[i].attributes.definition.type');
    Text := CkJsonObject__stringOf(jResp,'data.attributes.cells[i].attributes.definition.text');
    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": {
    "attributes": {
      "cells": [
        {
          "attributes": {
            "definition": {
              "text": "## Some test markdown\n\nWith some example content.",
              "type": "markdown"
            }
          },
          "type": "notebook_cells"
        },
        {
          "attributes": {
            "definition": {
              "requests": [
                {
                  "display_type": "line",
                  "q": "avg:system.load.1{*}",
                  "style": {
                    "line_type": "solid",
                    "line_width": "normal",
                    "palette": "dog_classic"
                  }
                }
              ],
              "show_legend": true,
              "type": "timeseries",
              "yaxis": {
                "scale": "linear"
              }
            },
            "graph_size": "m",
            "split_by": {
              "keys": [],
              "tags": []
            },
            "time": null
          },
          "type": "notebook_cells"
        }
      ],
      "name": "Example Notebook",
      "time": {
        "live_span": "1h"
      }
    },
    "type": "notebooks"
  }
}'
https://api.app.ddog-gov.com/api/v1/notebooks

Postman Collection Item JSON

{
  "name": "Create a notebook",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"data\": {\n    \"attributes\": {\n      \"cells\": [\n        {\n          \"attributes\": {\n            \"definition\": {\n              \"text\": \"## Some test markdown\\n\\nWith some example content.\",\n              \"type\": \"markdown\"\n            }\n          },\n          \"type\": \"notebook_cells\"\n        },\n        {\n          \"attributes\": {\n            \"definition\": {\n              \"requests\": [\n                {\n                  \"display_type\": \"line\",\n                  \"q\": \"avg:system.load.1{*}\",\n                  \"style\": {\n                    \"line_type\": \"solid\",\n                    \"line_width\": \"normal\",\n                    \"palette\": \"dog_classic\"\n                  }\n                }\n              ],\n              \"show_legend\": true,\n              \"type\": \"timeseries\",\n              \"yaxis\": {\n                \"scale\": \"linear\"\n              }\n            },\n            \"graph_size\": \"m\",\n            \"split_by\": {\n              \"keys\": [],\n              \"tags\": []\n            },\n            \"time\": null\n          },\n          \"type\": \"notebook_cells\"\n        }\n      ],\n      \"name\": \"Example Notebook\",\n      \"time\": {\n        \"live_span\": \"1h\"\n      }\n    },\n    \"type\": \"notebooks\"\n  }\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v1/notebooks",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v1",
        "notebooks"
      ]
    },
    "description": "Create a notebook using the specified options."
  },
  "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    \"attributes\": {\n      \"cells\": [\n        {\n          \"attributes\": {\n            \"definition\": {\n              \"text\": \"## Some test markdown\\n\\nWith some example content.\",\n              \"type\": \"markdown\"\n            }\n          },\n          \"type\": \"notebook_cells\"\n        },\n        {\n          \"attributes\": {\n            \"definition\": {\n              \"requests\": [\n                {\n                  \"display_type\": \"line\",\n                  \"q\": \"avg:system.load.1{*}\",\n                  \"style\": {\n                    \"line_type\": \"solid\",\n                    \"line_width\": \"normal\",\n                    \"palette\": \"dog_classic\"\n                  }\n                }\n              ],\n              \"show_legend\": true,\n              \"type\": \"timeseries\",\n              \"yaxis\": {\n                \"scale\": \"linear\"\n              }\n            },\n            \"graph_size\": \"m\",\n            \"split_by\": {\n              \"keys\": [],\n              \"tags\": []\n            },\n            \"time\": null\n          },\n          \"type\": \"notebook_cells\"\n        }\n      ],\n      \"name\": \"Example Notebook\",\n      \"time\": {\n        \"live_span\": \"1h\"\n      }\n    },\n    \"type\": \"notebooks\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/notebooks",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "notebooks"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"data\": {\n    \"id\": 123456,\n    \"type\": \"notebooks\",\n    \"attributes\": {\n      \"cells\": [\n        {\n          \"id\": \"abcd1234\",\n          \"type\": \"notebook_cells\",\n          \"attributes\": {\n            \"definition\": {\n              \"type\": \"markdown\",\n              \"text\": \"# Example Header \\nexample content\"\n            }\n          }\n        },\n        {\n          \"id\": \"abcd1234\",\n          \"type\": \"notebook_cells\",\n          \"attributes\": {\n            \"definition\": {\n              \"type\": \"markdown\",\n              \"text\": \"# Example Header \\nexample content\"\n            }\n          }\n        }\n      ],\n      \"time\": {\n        \"live_span\": \"1h\"\n      },\n      \"name\": \"Example Notebook\",\n      \"author\": {\n        \"created_at\": \"1944-02-06T04:58:13.667Z\",\n        \"disabled\": false,\n        \"email\": \"tempor exercitation\",\n        \"handle\": \"est eu aliquip officia\",\n        \"icon\": \"nostrud n\",\n        \"name\": \"dolor dolor\",\n        \"status\": \"anim Ut pariatur\",\n        \"title\": \"eu\",\n        \"verified\": true\n      },\n      \"created\": \"2021-02-24T23:14:15.173964+00:00\",\n      \"metadata\": {\n        \"is_template\": false,\n        \"take_snapshots\": false,\n        \"type\": \"investigation\"\n      },\n      \"modified\": \"2021-02-24T23:15:23.274966+00:00\",\n      \"status\": \"published\"\n    }\n  }\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    \"attributes\": {\n      \"cells\": [\n        {\n          \"attributes\": {\n            \"definition\": {\n              \"text\": \"## Some test markdown\\n\\nWith some example content.\",\n              \"type\": \"markdown\"\n            }\n          },\n          \"type\": \"notebook_cells\"\n        },\n        {\n          \"attributes\": {\n            \"definition\": {\n              \"requests\": [\n                {\n                  \"display_type\": \"line\",\n                  \"q\": \"avg:system.load.1{*}\",\n                  \"style\": {\n                    \"line_type\": \"solid\",\n                    \"line_width\": \"normal\",\n                    \"palette\": \"dog_classic\"\n                  }\n                }\n              ],\n              \"show_legend\": true,\n              \"type\": \"timeseries\",\n              \"yaxis\": {\n                \"scale\": \"linear\"\n              }\n            },\n            \"graph_size\": \"m\",\n            \"split_by\": {\n              \"keys\": [],\n              \"tags\": []\n            },\n            \"time\": null\n          },\n          \"type\": \"notebook_cells\"\n        }\n      ],\n      \"name\": \"Example Notebook\",\n      \"time\": {\n        \"live_span\": \"1h\"\n      }\n    },\n    \"type\": \"notebooks\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/notebooks",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "notebooks"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    },
    {
      "name": "Authentication Error",
      "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    \"attributes\": {\n      \"cells\": [\n        {\n          \"attributes\": {\n            \"definition\": {\n              \"text\": \"## Some test markdown\\n\\nWith some example content.\",\n              \"type\": \"markdown\"\n            }\n          },\n          \"type\": \"notebook_cells\"\n        },\n        {\n          \"attributes\": {\n            \"definition\": {\n              \"requests\": [\n                {\n                  \"display_type\": \"line\",\n                  \"q\": \"avg:system.load.1{*}\",\n                  \"style\": {\n                    \"line_type\": \"solid\",\n                    \"line_width\": \"normal\",\n                    \"palette\": \"dog_classic\"\n                  }\n                }\n              ],\n              \"show_legend\": true,\n              \"type\": \"timeseries\",\n              \"yaxis\": {\n                \"scale\": \"linear\"\n              }\n            },\n            \"graph_size\": \"m\",\n            \"split_by\": {\n              \"keys\": [],\n              \"tags\": []\n            },\n            \"time\": null\n          },\n          \"type\": \"notebook_cells\"\n        }\n      ],\n      \"name\": \"Example Notebook\",\n      \"time\": {\n        \"live_span\": \"1h\"\n      }\n    },\n    \"type\": \"notebooks\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/notebooks",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "notebooks"
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\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    \"attributes\": {\n      \"cells\": [\n        {\n          \"attributes\": {\n            \"definition\": {\n              \"text\": \"## Some test markdown\\n\\nWith some example content.\",\n              \"type\": \"markdown\"\n            }\n          },\n          \"type\": \"notebook_cells\"\n        },\n        {\n          \"attributes\": {\n            \"definition\": {\n              \"requests\": [\n                {\n                  \"display_type\": \"line\",\n                  \"q\": \"avg:system.load.1{*}\",\n                  \"style\": {\n                    \"line_type\": \"solid\",\n                    \"line_width\": \"normal\",\n                    \"palette\": \"dog_classic\"\n                  }\n                }\n              ],\n              \"show_legend\": true,\n              \"type\": \"timeseries\",\n              \"yaxis\": {\n                \"scale\": \"linear\"\n              }\n            },\n            \"graph_size\": \"m\",\n            \"split_by\": {\n              \"keys\": [],\n              \"tags\": []\n            },\n            \"time\": null\n          },\n          \"type\": \"notebook_cells\"\n        }\n      ],\n      \"name\": \"Example Notebook\",\n      \"time\": {\n        \"live_span\": \"1h\"\n      }\n    },\n    \"type\": \"notebooks\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/notebooks",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "notebooks"
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    }
  ]
}