Android / Squadcast API V3 / Create or Update Deduplication Rules
Back to Collection Items
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttp http = new CkHttp();
boolean success;
// 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>"
// }
// ]
// }
CkJsonObject json = new CkJsonObject();
json.UpdateString("rules[0].expression","<string>");
json.UpdateString("rules[0].time_window","<string>");
json.UpdateString("rules[1].expression","<string>");
json.UpdateString("rules[1].time_window","<string>");
http.SetRequestHeader("Authorization","");
http.SetRequestHeader("Content-Type","application/json");
CkHttpResponse resp = http.PostJson3("https://api.squadcast.com/v3/services/:serviceID/deduplication-rules","application/json",json);
if (http.get_LastMethodSuccess() == false) {
Log.i(TAG, http.lastErrorText());
return;
}
CkStringBuilder sbResponseBody = new CkStringBuilder();
resp.GetBodySb(sbResponseBody);
CkJsonObject jResp = new CkJsonObject();
jResp.LoadSb(sbResponseBody);
jResp.put_EmitCompact(false);
Log.i(TAG, "Response Body:");
Log.i(TAG, jResp.emit());
int respStatusCode = resp.get_StatusCode();
Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));
if (respStatusCode >= 400) {
Log.i(TAG, "Response Header:");
Log.i(TAG, resp.header());
Log.i(TAG, "Failed.");
return;
}
// 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
String expression;
int time_window;
String Id = jResp.stringOf("data.id");
String Created_at = jResp.stringOf("data.created_at");
String Updated_at = jResp.stringOf("data.updated_at");
String Deleted_at = jResp.stringOf("data.deleted_at");
String Service_id = jResp.stringOf("data.service_id");
String Organization_id = jResp.stringOf("data.organization_id");
int i = 0;
int count_i = jResp.SizeOfArray("data.rules");
while (i < count_i) {
jResp.put_I(i);
expression = jResp.stringOf("data.rules[i].expression");
time_window = jResp.IntOf("data.rules[i].time_window");
i = i + 1;
}
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}
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}"
}
]
}