Chilkat Online Tools

unicodeC / Squadcast API V3 / Create or Update Tagging Rules

Back to Collection Items

#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *expression;
    const wchar_t *Value;
    const wchar_t *Color;
    const wchar_t *Id;
    const wchar_t *Created_at;
    const wchar_t *Updated_at;
    const wchar_t *Deleted_at;
    const wchar_t *Service_id;
    const wchar_t *Organization_id;
    int i;
    int count_i;

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

    http = CkHttpW_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.

    // {
    //   "rules": [
    //     {
    //       "expression": "<string>",
    //       "tags": {
    //         "key": {
    //           "value": "<string>",
    //           "color": "<string>"
    //         }
    //       }
    //     },
    //     {
    //       "expression": "<string>",
    //       "tags": {
    //         "key": {
    //           "value": "<string>",
    //           "color": "<string>"
    //         }
    //       }
    //     }
    //   ]
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"rules[0].expression",L"<string>");
    CkJsonObjectW_UpdateString(json,L"rules[0].tags.key.value",L"<string>");
    CkJsonObjectW_UpdateString(json,L"rules[0].tags.key.color",L"<string>");
    CkJsonObjectW_UpdateString(json,L"rules[1].expression",L"<string>");
    CkJsonObjectW_UpdateString(json,L"rules[1].tags.key.value",L"<string>");
    CkJsonObjectW_UpdateString(json,L"rules[1].tags.key.color",L"<string>");

    CkHttpW_SetRequestHeader(http,L"Authorization",L"");
    CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");

    resp = CkHttpW_PostJson3(http,L"https://api.squadcast.com/v3/services/:serviceID/tagging-rules",L"application/json",json);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpResponseW_Dispose(resp);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "data": {
    //     "id": "5d8c7b6905f5b9cfbfd30816",
    //     "created_at": "2019-09-26T08:48:41.869Z",
    //     "updated_at": "2019-09-26T08:48:41.869Z",
    //     "deleted_at": null,
    //     "service_id": "5d8c79cb3f00de06d5fed406",
    //     "organization_id": "5d81d9187000fb6b9def7e32",
    //     "rules": [
    //       {
    //         "expression": "re(current.labels.alertname, \"^CPUThrottlingHigh.*\") && re(past.labels.alertname, \"^CPUThrottlingHigh.*\")",
    //         "tags": {
    //           "severity": {
    //             "value": "high",
    //             "color": "#ff0000"
    //           }
    //         }
    //       }
    //     ]
    //   }
    // }

    // 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.

    Id = CkJsonObjectW_stringOf(jResp,L"data.id");
    Created_at = CkJsonObjectW_stringOf(jResp,L"data.created_at");
    Updated_at = CkJsonObjectW_stringOf(jResp,L"data.updated_at");
    Deleted_at = CkJsonObjectW_stringOf(jResp,L"data.deleted_at");
    Service_id = CkJsonObjectW_stringOf(jResp,L"data.service_id");
    Organization_id = CkJsonObjectW_stringOf(jResp,L"data.organization_id");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"data.rules");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        expression = CkJsonObjectW_stringOf(jResp,L"data.rules[i].expression");
        Value = CkJsonObjectW_stringOf(jResp,L"data.rules[i].tags.severity.value");
        Color = CkJsonObjectW_stringOf(jResp,L"data.rules[i].tags.severity.color");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }

Curl Command

curl -X POST
	-H "Authorization: "
	-H "Content-Type: application/json"
	-d '{
    "rules": [
        {
            "expression": "<string>",
            "tags": {
                "key": {
                    "value": "<string>",
                    "color": "<string>"
                }
            }
        },
        {
            "expression": "<string>",
            "tags": {
                "key": {
                    "value": "<string>",
                    "color": "<string>"
                }
            }
        }
    ]
}'
https://api.squadcast.com/v3/services/:serviceID/tagging-rules

Postman Collection Item JSON

{
  "name": "Create or Update Tagging Rules",
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
      {
        "key": "Authorization",
        "value": ""
      },
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"rules\": [\n        {\n            \"expression\": \"<string>\",\n            \"tags\": {\n                \"key\": {\n                    \"value\": \"<string>\",\n                    \"color\": \"<string>\"\n                }\n            }\n        },\n        {\n            \"expression\": \"<string>\",\n            \"tags\": {\n                \"key\": {\n                    \"value\": \"<string>\",\n                    \"color\": \"<string>\"\n                }\n            }\n        }\n    ]\n}"
    },
    "url": {
      "raw": "{{baseUrl}}/services/:serviceID/tagging-rules",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "services",
        ":serviceID",
        "tagging-rules"
      ],
      "variable": [
        {
          "key": "serviceID",
          "value": "<string>",
          "type": "string",
          "description": "(Required) service id"
        }
      ]
    },
    "description": "This endpoint creates or updates the tagging rules for the given service.\nIf default rules not found it creates otherwise it updates the new rules.\nRequires `access_token` as a `Bearer {{token}}` in the `Authorization` header with `write` scope."
  },
  "response": [
    {
      "name": "Created",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"rules\": [\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"tags\": {\n                \"severity\": {\n                    \"value\": \"high\",\n                    \"color\": \"#ff0000\"\n                }\n            }\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services/:serviceID/tagging-rules",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services",
            ":serviceID",
            "tagging-rules"
          ],
          "variable": [
            {
              "key": "serviceID"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"data\": {\n  \"id\": \"5d8c7b6905f5b9cfbfd30816\",\n  \"created_at\": \"2019-09-26T08:48:41.869Z\",\n  \"updated_at\": \"2019-09-26T08:48:41.869Z\",\n  \"deleted_at\": null,\n  \"service_id\": \"5d8c79cb3f00de06d5fed406\",\n  \"organization_id\": \"5d81d9187000fb6b9def7e32\",\n  \"rules\": [\n   {\n    \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n    \"tags\": {\n     \"severity\": {\n      \"value\": \"high\",\n      \"color\": \"#ff0000\"\n     }\n    }\n   }\n  ]\n }\n}"
    },
    {
      "name": "Unauthorized",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"rules\": [\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"tags\": {\n                \"severity\": {\n                    \"value\": \"high\",\n                    \"color\": \"#ff0000\"\n                }\n            }\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services/:serviceID/tagging-rules",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services",
            ":serviceID",
            "tagging-rules"
          ],
          "variable": [
            {
              "key": "serviceID"
            }
          ]
        }
      },
      "status": "Unauthorized",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"meta\": {\n  \"status\": \"<integer>\",\n  \"error_message\": \"<string>\"\n }\n}"
    },
    {
      "name": "Internal Server Error",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"rules\": [\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"tags\": {\n                \"severity\": {\n                    \"value\": \"high\",\n                    \"color\": \"#ff0000\"\n                }\n            }\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services/:serviceID/tagging-rules",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services",
            ":serviceID",
            "tagging-rules"
          ],
          "variable": [
            {
              "key": "serviceID"
            }
          ]
        }
      },
      "status": "Internal Server Error",
      "code": 500,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"meta\": {\n  \"status\": \"<integer>\",\n  \"error_message\": \"<string>\"\n }\n}"
    },
    {
      "name": "Forbidden",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"rules\": [\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"tags\": {\n                \"severity\": {\n                    \"value\": \"high\",\n                    \"color\": \"#ff0000\"\n                }\n            }\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services/:serviceID/tagging-rules",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services",
            ":serviceID",
            "tagging-rules"
          ],
          "variable": [
            {
              "key": "serviceID"
            }
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"meta\": {\n  \"status\": \"<integer>\",\n  \"error_message\": \"<string>\"\n }\n}"
    },
    {
      "name": "Bad Request",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"rules\": [\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"tags\": {\n                \"severity\": {\n                    \"value\": \"high\",\n                    \"color\": \"#ff0000\"\n                }\n            }\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services/:serviceID/tagging-rules",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services",
            ":serviceID",
            "tagging-rules"
          ],
          "variable": [
            {
              "key": "serviceID"
            }
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"meta\": {\n  \"status\": \"<integer>\",\n  \"error_message\": \"<string>\"\n }\n}"
    }
  ]
}