Chilkat Online Tools

SQL Server / Squadcast API V3 / Create or Update Deduplication Rules

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- 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>",
    --       "time_window": "<string>"
    --     },
    --     {
    --       "expression": "<string>",
    --       "time_window": "<string>"
    --     }
    --   ]
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'rules[0].expression', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'rules[0].time_window', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'rules[1].expression', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'rules[1].time_window', '<string>'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Authorization', ''
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://api.squadcast.com/v3/services/:serviceID/deduplication-rules', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

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

    -- {
    --   "data": {
    --     "id": "5e905b9ffe3e978d6ac34cc9",
    --     "created_at": "2020-04-10T11:42:23.646Z",
    --     "updated_at": "2020-04-10T11:42:23.646Z",
    --     "deleted_at": null,
    --     "service_id": "5e8edb24668e003cb0b18ba1",
    --     "organization_id": "5d81d9187000fb6b9def7e32",
    --     "rules": [
    --       {
    --         "expression": "re(current.labels.alertname, \"^CPUThrottlingHigh.*\") && re(past.labels.alertname, \"^CPUThrottlingHigh.*\")",
    --         "time_window": 300
    --       },
    --       {
    --         "expression": "re(current.labels.alertname, \"^DeploymentReplicasNotUpdated.*\") && re(past.labels.alertname, \"^DeploymentReplicasNotUpdated.*\")",
    --         "time_window": 300
    --       }
    --     ]
    --   }
    -- }

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

    DECLARE @expression nvarchar(4000)

    DECLARE @time_window int

    DECLARE @Id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'data.id'
    DECLARE @Created_at nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Created_at OUT, 'data.created_at'
    DECLARE @Updated_at nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Updated_at OUT, 'data.updated_at'
    DECLARE @Deleted_at nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Deleted_at OUT, 'data.deleted_at'
    DECLARE @Service_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Service_id OUT, 'data.service_id'
    DECLARE @Organization_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Organization_id OUT, 'data.organization_id'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'data.rules'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @expression OUT, 'data.rules[i].expression'
        EXEC sp_OAMethod @jResp, 'IntOf', @time_window OUT, 'data.rules[i].time_window'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

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

Postman Collection Item JSON

{
  "name": "Create or Update Deduplication 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            \"time_window\": \"<string>\"\n        },\n        {\n            \"expression\": \"<string>\",\n            \"time_window\": \"<string>\"\n        }\n    ]\n}"
    },
    "url": {
      "raw": "{{baseUrl}}/services/:serviceID/deduplication-rules",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "services",
        ":serviceID",
        "deduplication-rules"
      ],
      "variable": [
        {
          "key": "serviceID",
          "value": "<string>",
          "type": "string",
          "description": "(Required) service id"
        }
      ]
    },
    "description": "This endpoint creates or updates the deduplication 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.\n"
  },
  "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            \"time_window\": 300\n        },\n        {\n            \"expression\": \"re(current.labels.alertname, \\\"^DeploymentReplicasNotUpdated.*\\\") && re(past.labels.alertname, \\\"^DeploymentReplicasNotUpdated.*\\\")\",\n            \"time_window\": 300\n        }\n    ]\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services/:serviceID/deduplication-rules",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services",
            ":serviceID",
            "deduplication-rules"
          ],
          "variable": [
            {
              "key": "serviceID"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"data\": {\n  \"id\": \"5e905b9ffe3e978d6ac34cc9\",\n  \"created_at\": \"2020-04-10T11:42:23.646Z\",\n  \"updated_at\": \"2020-04-10T11:42:23.646Z\",\n  \"deleted_at\": null,\n  \"service_id\": \"5e8edb24668e003cb0b18ba1\",\n  \"organization_id\": \"5d81d9187000fb6b9def7e32\",\n  \"rules\": [\n   {\n    \"expression\": \"re(current.labels.alertname, \\\"^CPUThrottlingHigh.*\\\") && re(past.labels.alertname, \\\"^CPUThrottlingHigh.*\\\")\",\n    \"time_window\": 300\n   },\n   {\n    \"expression\": \"re(current.labels.alertname, \\\"^DeploymentReplicasNotUpdated.*\\\") && re(past.labels.alertname, \\\"^DeploymentReplicasNotUpdated.*\\\")\",\n    \"time_window\": 300\n   }\n  ]\n }\n}"
    }
  ]
}