Chilkat Online Tools

C / Squadcast API V3 / Create or Update Routing Rules

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkJsonObject json;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    const char *expression;
    const char *Entity_type;
    const char *Entity_id;
    const char *Id;
    const char *Created_at;
    const char *Updated_at;
    const char *Deleted_at;
    const char *Service_id;
    const char *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 = 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.

    // {
    //   "rules": [
    //     {
    //       "expression": "<string>",
    //       "route_to": {
    //         "entity_type": "<string>",
    //         "entity_id": "<string>"
    //       }
    //     },
    //     {
    //       "expression": "<string>",
    //       "route_to": {
    //         "entity_type": "<string>",
    //         "entity_id": "<string>"
    //       }
    //     }
    //   ]
    // }

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"rules[0].expression","<string>");
    CkJsonObject_UpdateString(json,"rules[0].route_to.entity_type","<string>");
    CkJsonObject_UpdateString(json,"rules[0].route_to.entity_id","<string>");
    CkJsonObject_UpdateString(json,"rules[1].expression","<string>");
    CkJsonObject_UpdateString(json,"rules[1].route_to.entity_type","<string>");
    CkJsonObject_UpdateString(json,"rules[1].route_to.entity_id","<string>");

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

    resp = CkHttp_PostJson3(http,"https://api.squadcast.com/v3/services/:serviceID/routing-rules","application/json",json);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        return;
    }

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

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);
    CkJsonObject_putEmitCompact(jResp,FALSE);

    printf("Response Body:\n");
    printf("%s\n",CkJsonObject_emit(jResp));

    respStatusCode = CkHttpResponse_getStatusCode(resp);
    printf("Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        printf("Response Header:\n");
        printf("%s\n",CkHttpResponse_header(resp));
        printf("Failed.\n");
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    CkHttpResponse_Dispose(resp);

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

    // {
    //   "data": {
    //     "id": "5d8b23427c870edd41453c25",
    //     "created_at": "2019-09-25T08:20:18.884Z",
    //     "updated_at": "2019-09-25T08:20:18.884Z",
    //     "deleted_at": null,
    //     "service_id": "5d81d9687000fb6b9def7e35",
    //     "organization_id": "5d81d9187000fb6b9def7e32",
    //     "rules": [
    //       {
    //         "expression": "re(current.labels.alertname, \"^CPUThrottlingHigh.*\") && re(past.labels.alertname, \"^CPUThrottlingHigh.*\")",
    //         "route_to": {
    //           "entity_type": "escalationpolicy",
    //           "entity_id": "5d81d9407000fb6b9def7e33"
    //         }
    //       },
    //       {
    //         "expression": "re(current.labels.alertname, \"^CPUThrottlingHigh.*\") && re(past.labels.alertname, \"^CPUThrottlingHigh.*\")",
    //         "route_to": {
    //           "entity_type": "squad",
    //           "entity_id": "5d8b5976578cc3b845cb3608"
    //         }
    //       },
    //       {
    //         "expression": "re(current.labels.alertname, \"^CPUThrottlingHigh.*\") && re(past.labels.alertname, \"^CPUThrottlingHigh.*\")",
    //         "route_to": {
    //           "entity_type": "user",
    //           "entity_id": "5d81d9187000fb6b9def7e31"
    //         }
    //       }
    //     ]
    //   }
    // }

    // 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 = CkJsonObject_stringOf(jResp,"data.id");
    Created_at = CkJsonObject_stringOf(jResp,"data.created_at");
    Updated_at = CkJsonObject_stringOf(jResp,"data.updated_at");
    Deleted_at = CkJsonObject_stringOf(jResp,"data.deleted_at");
    Service_id = CkJsonObject_stringOf(jResp,"data.service_id");
    Organization_id = CkJsonObject_stringOf(jResp,"data.organization_id");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"data.rules");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        expression = CkJsonObject_stringOf(jResp,"data.rules[i].expression");
        Entity_type = CkJsonObject_stringOf(jResp,"data.rules[i].route_to.entity_type");
        Entity_id = CkJsonObject_stringOf(jResp,"data.rules[i].route_to.entity_id");
        i = i + 1;
    }



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

    }

Curl Command

curl -X POST
	-H "Authorization: "
	-H "Content-Type: application/json"
	-d '{
    "rules": [
        {
            "expression": "<string>",
            "route_to": {
                "entity_type": "<string>",
                "entity_id": "<string>"
            }
        },
        {
            "expression": "<string>",
            "route_to": {
                "entity_type": "<string>",
                "entity_id": "<string>"
            }
        }
    ]
}'
https://api.squadcast.com/v3/services/:serviceID/routing-rules

Postman Collection Item JSON

{
  "name": "Create or Update Routing 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            \"route_to\": {\n                \"entity_type\": \"<string>\",\n                \"entity_id\": \"<string>\"\n            }\n        },\n        {\n            \"expression\": \"<string>\",\n            \"route_to\": {\n                \"entity_type\": \"<string>\",\n                \"entity_id\": \"<string>\"\n            }\n        }\n    ]\n}"
    },
    "url": {
      "raw": "{{baseUrl}}/services/:serviceID/routing-rules",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "services",
        ":serviceID",
        "routing-rules"
      ],
      "variable": [
        {
          "key": "serviceID",
          "value": "<string>",
          "type": "string",
          "description": "(Required) "
        }
      ]
    },
    "description": "This endpoint creates or updates the routing 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": "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            \"route_to\": {\n                \"entity_type\": \"escalationpolicy\",\n                \"entity_id\": \"5d81d9407000fb6b9def7e33\"\n            }\n        },\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"route_to\": {\n                \"entity_type\": \"squad\",\n                \"entity_id\": \"5d8b5976578cc3b845cb3608\"\n            }\n        },\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"route_to\": {\n                \"entity_type\": \"user\",\n                \"entity_id\": \"5d81d9187000fb6b9def7e31\"\n            }\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services/:serviceID/routing-rules",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services",
            ":serviceID",
            "routing-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}"
    },
    {
      "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            \"route_to\": {\n                \"entity_type\": \"escalationpolicy\",\n                \"entity_id\": \"5d81d9407000fb6b9def7e33\"\n            }\n        },\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"route_to\": {\n                \"entity_type\": \"squad\",\n                \"entity_id\": \"5d8b5976578cc3b845cb3608\"\n            }\n        },\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"route_to\": {\n                \"entity_type\": \"user\",\n                \"entity_id\": \"5d81d9187000fb6b9def7e31\"\n            }\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services/:serviceID/routing-rules",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services",
            ":serviceID",
            "routing-rules"
          ],
          "variable": [
            {
              "key": "serviceID"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"data\": {\n  \"id\": \"5d8b23427c870edd41453c25\",\n  \"created_at\": \"2019-09-25T08:20:18.884Z\",\n  \"updated_at\": \"2019-09-25T08:20:18.884Z\",\n  \"deleted_at\": null,\n  \"service_id\": \"5d81d9687000fb6b9def7e35\",\n  \"organization_id\": \"5d81d9187000fb6b9def7e32\",\n  \"rules\": [\n   {\n    \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n    \"route_to\": {\n     \"entity_type\": \"escalationpolicy\",\n     \"entity_id\": \"5d81d9407000fb6b9def7e33\"\n    }\n   },\n   {\n    \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n    \"route_to\": {\n     \"entity_type\": \"squad\",\n     \"entity_id\": \"5d8b5976578cc3b845cb3608\"\n    }\n   },\n   {\n    \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n    \"route_to\": {\n     \"entity_type\": \"user\",\n     \"entity_id\": \"5d81d9187000fb6b9def7e31\"\n    }\n   }\n  ]\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            \"route_to\": {\n                \"entity_type\": \"escalationpolicy\",\n                \"entity_id\": \"5d81d9407000fb6b9def7e33\"\n            }\n        },\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"route_to\": {\n                \"entity_type\": \"squad\",\n                \"entity_id\": \"5d8b5976578cc3b845cb3608\"\n            }\n        },\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"route_to\": {\n                \"entity_type\": \"user\",\n                \"entity_id\": \"5d81d9187000fb6b9def7e31\"\n            }\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services/:serviceID/routing-rules",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services",
            ":serviceID",
            "routing-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": "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            \"route_to\": {\n                \"entity_type\": \"escalationpolicy\",\n                \"entity_id\": \"5d81d9407000fb6b9def7e33\"\n            }\n        },\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"route_to\": {\n                \"entity_type\": \"squad\",\n                \"entity_id\": \"5d8b5976578cc3b845cb3608\"\n            }\n        },\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"route_to\": {\n                \"entity_type\": \"user\",\n                \"entity_id\": \"5d81d9187000fb6b9def7e31\"\n            }\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services/:serviceID/routing-rules",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services",
            ":serviceID",
            "routing-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            \"route_to\": {\n                \"entity_type\": \"escalationpolicy\",\n                \"entity_id\": \"5d81d9407000fb6b9def7e33\"\n            }\n        },\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"route_to\": {\n                \"entity_type\": \"squad\",\n                \"entity_id\": \"5d8b5976578cc3b845cb3608\"\n            }\n        },\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n            \"route_to\": {\n                \"entity_type\": \"user\",\n                \"entity_id\": \"5d81d9187000fb6b9def7e31\"\n            }\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services/:serviceID/routing-rules",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services",
            ":serviceID",
            "routing-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}"
    }
  ]
}