Chilkat Online Tools

DataFlex / Squadcast API V3 / Create or Update Routing Rules

Back to Collection Items

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoHttp
    Boolean iSuccess
    Variant vJson
    Handle hoJson
    Variant vResp
    Handle hoResp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Handle hoJResp
    Integer iRespStatusCode
    String sExpression
    String sEntity_type
    String sEntity_id
    String sId
    String sCreated_at
    String sUpdated_at
    String sDeleted_at
    String sService_id
    String sOrganization_id
    Integer i
    Integer iCount_i
    String sTemp1
    Boolean bTemp1

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // 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>"
    //       }
    //     }
    //   ]
    // }

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "rules[0].expression" "<string>" To iSuccess
    Get ComUpdateString Of hoJson "rules[0].route_to.entity_type" "<string>" To iSuccess
    Get ComUpdateString Of hoJson "rules[0].route_to.entity_id" "<string>" To iSuccess
    Get ComUpdateString Of hoJson "rules[1].expression" "<string>" To iSuccess
    Get ComUpdateString Of hoJson "rules[1].route_to.entity_type" "<string>" To iSuccess
    Get ComUpdateString Of hoJson "rules[1].route_to.entity_id" "<string>" To iSuccess

    Send ComSetRequestHeader To hoHttp "Authorization" ""
    Send ComSetRequestHeader To hoHttp "Content-Type" "application/json"

    Get pvComObject of hoJson to vJson
    Get ComPostJson3 Of hoHttp "https://api.squadcast.com/v3/services/:serviceID/routing-rules" "application/json" vJson To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
    Set ComEmitCompact Of hoJResp To False

    Showln "Response Body:"
    Get ComEmit Of hoJResp To sTemp1
    Showln sTemp1

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Send Destroy of hoResp
        Procedure_Return
    End

    Send Destroy of hoResp

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

    Get ComStringOf Of hoJResp "data.id" To sId
    Get ComStringOf Of hoJResp "data.created_at" To sCreated_at
    Get ComStringOf Of hoJResp "data.updated_at" To sUpdated_at
    Get ComStringOf Of hoJResp "data.deleted_at" To sDeleted_at
    Get ComStringOf Of hoJResp "data.service_id" To sService_id
    Get ComStringOf Of hoJResp "data.organization_id" To sOrganization_id
    Move 0 To i
    Get ComSizeOfArray Of hoJResp "data.rules" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJResp To i
        Get ComStringOf Of hoJResp "data.rules[i].expression" To sExpression
        Get ComStringOf Of hoJResp "data.rules[i].route_to.entity_type" To sEntity_type
        Get ComStringOf Of hoJResp "data.rules[i].route_to.entity_id" To sEntity_id
        Move (i + 1) To i
    Loop



End_Procedure

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}"
    }
  ]
}