Chilkat Online Tools

Foxpro / Squadcast API V3 / Create or Update Tagging Rules

Back to Collection Items

LOCAL loHttp
LOCAL lnSuccess
LOCAL loJson
LOCAL loResp
LOCAL loSbResponseBody
LOCAL loJResp
LOCAL lnRespStatusCode
LOCAL lcExpression
LOCAL lcValue
LOCAL lcColor
LOCAL lcId
LOCAL lcCreated_at
LOCAL lcUpdated_at
LOCAL lcDeleted_at
LOCAL lcService_id
LOCAL lcOrganization_id
LOCAL i
LOCAL lnCount_i

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

loHttp = CreateObject('Chilkat_9_5_0.Http')

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

loJson = CreateObject('Chilkat_9_5_0.JsonObject')
loJson.UpdateString("rules[0].expression","<string>")
loJson.UpdateString("rules[0].tags.key.value","<string>")
loJson.UpdateString("rules[0].tags.key.color","<string>")
loJson.UpdateString("rules[1].expression","<string>")
loJson.UpdateString("rules[1].tags.key.value","<string>")
loJson.UpdateString("rules[1].tags.key.color","<string>")

loHttp.SetRequestHeader("Authorization","")
loHttp.SetRequestHeader("Content-Type","application/json")

loResp = loHttp.PostJson3("https://api.squadcast.com/v3/services/:serviceID/tagging-rules","application/json",loJson)
IF (loHttp.LastMethodSuccess = 0) THEN
    ? loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loJson
    CANCEL
ENDIF

loSbResponseBody = CreateObject('Chilkat_9_5_0.StringBuilder')
loResp.GetBodySb(loSbResponseBody)

loJResp = CreateObject('Chilkat_9_5_0.JsonObject')
loJResp.LoadSb(loSbResponseBody)
loJResp.EmitCompact = 0

? "Response Body:"
? loJResp.Emit()

lnRespStatusCode = loResp.StatusCode
? "Response Status Code = " + STR(lnRespStatusCode)
IF (lnRespStatusCode >= 400) THEN
    ? "Response Header:"
    ? loResp.Header
    ? "Failed."
    RELEASE loResp
    RELEASE loHttp
    RELEASE loJson
    RELEASE loSbResponseBody
    RELEASE loJResp
    CANCEL
ENDIF

RELEASE loResp

* 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

lcId = loJResp.StringOf("data.id")
lcCreated_at = loJResp.StringOf("data.created_at")
lcUpdated_at = loJResp.StringOf("data.updated_at")
lcDeleted_at = loJResp.StringOf("data.deleted_at")
lcService_id = loJResp.StringOf("data.service_id")
lcOrganization_id = loJResp.StringOf("data.organization_id")
i = 0
lnCount_i = loJResp.SizeOfArray("data.rules")
DO WHILE i < lnCount_i
    loJResp.I = i
    lcExpression = loJResp.StringOf("data.rules[i].expression")
    lcValue = loJResp.StringOf("data.rules[i].tags.severity.value")
    lcColor = loJResp.StringOf("data.rules[i].tags.severity.color")
    i = i + 1
ENDDO

RELEASE loHttp
RELEASE loJson
RELEASE loSbResponseBody
RELEASE loJResp

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