Chilkat Online Tools

unicodeC / Anypoint Platform APIs / Update alert by id

Back to Collection Items

#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkStringBuilderW sbRequestBody;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *strVal;
    int intVal;
    int id;
    int timeCreated;
    int timeUpdated;
    const wchar_t *severity;
    const wchar_t *description;
    BOOL enabled;
    const wchar_t *ResourceType;
    const wchar_t *v_Event;
    int i;
    int count_i;

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

    http = CkHttpW_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.

    // {
    //   "name": "Low CPU usage",
    //   "severity": "WARNING",
    //   "actions": [
    //     {
    //       "type": "email",
    //       "content": "Hello,\nYou are receiving this alert because:\nThe application ${resource} is now in an ${state} state, based on the condition 'CPU ${operator} ${value}%.",
    //       "subject": "${severity}: CPU usage ${state}",
    //       "emails": [
    //         "angel.alberici+testalerts@mulesoft.com",
    //         "angel.alberici@mulesoft.com"
    //       ]
    //     }
    //   ],
    //   "condition": {
    //     "operator": "LESS_THAN",
    //     "value": 50,
    //     "periodCount": 1,
    //     "resourceType": "cloudhub-application",
    //     "type": "cpu",
    //     "periodMins": 1,
    //     "resources": [
    //       "*"
    //     ]
    //   }
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"name",L"Low CPU usage");
    CkJsonObjectW_UpdateString(json,L"severity",L"WARNING");
    CkJsonObjectW_UpdateString(json,L"actions[0].type",L"email");
    CkJsonObjectW_UpdateString(json,L"actions[0].content",L"Hello,\nYou are receiving this alert because:\nThe application ${resource} is now in an ${state} state, based on the condition 'CPU ${operator} ${value}%.");
    CkJsonObjectW_UpdateString(json,L"actions[0].subject",L"${severity}: CPU usage ${state}");
    CkJsonObjectW_UpdateString(json,L"actions[0].emails[0]",L"angel.alberici+testalerts@mulesoft.com");
    CkJsonObjectW_UpdateString(json,L"actions[0].emails[1]",L"angel.alberici@mulesoft.com");
    CkJsonObjectW_UpdateString(json,L"condition.operator",L"LESS_THAN");
    CkJsonObjectW_UpdateInt(json,L"condition.value",50);
    CkJsonObjectW_UpdateInt(json,L"condition.periodCount",1);
    CkJsonObjectW_UpdateString(json,L"condition.resourceType",L"cloudhub-application");
    CkJsonObjectW_UpdateString(json,L"condition.type",L"cpu");
    CkJsonObjectW_UpdateInt(json,L"condition.periodMins",1);
    CkJsonObjectW_UpdateString(json,L"condition.resources[0]",L"*");

    CkHttpW_SetRequestHeader(http,L"X-ANYPNT-ENV-ID",L"{{environment_id}}");
    // Adds the "Authorization: Bearer {{token}}" header.
    CkHttpW_putAuthToken(http,L"{{token}}");
    CkHttpW_SetRequestHeader(http,L"X-ANYPNT-ORG-ID",L"{{organization_id}}");

    sbRequestBody = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(json,sbRequestBody);

    resp = CkHttpW_PTextSb(http,L"PUT",L"https://anypoint.mulesoft.com/armui/api/v1/alerts/cloudhub/02213013-10f2-4bbb-b399-718c0a543e18",sbRequestBody,L"utf-8",L"application/json",FALSE,FALSE);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpResponseW_Dispose(resp);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "id": 2312,
    //   "timeCreated": 342423432142342,
    //   "timeUpdated": 342423432154342,
    //   "severity": "CRITICAL",
    //   "description": "Notify when one server of cluster 45 went down",
    //   "enabled": true,
    //   "action": {
    //     "notification": {
    //       "recipients": [
    //         "alex.tiavo",
    //         "greg.kio"
    //       ]
    //     }
    //   },
    //   "condition": {
    //     "resourceType": "CLUSTER",
    //     "resources": [
    //       45
    //     ],
    //     "event": "DISCONNECTED"
    //   }
    // }

    // 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 = CkJsonObjectW_IntOf(jResp,L"id");
    timeCreated = CkJsonObjectW_IntOf(jResp,L"timeCreated");
    timeUpdated = CkJsonObjectW_IntOf(jResp,L"timeUpdated");
    severity = CkJsonObjectW_stringOf(jResp,L"severity");
    description = CkJsonObjectW_stringOf(jResp,L"description");
    enabled = CkJsonObjectW_BoolOf(jResp,L"enabled");
    ResourceType = CkJsonObjectW_stringOf(jResp,L"condition.resourceType");
    v_Event = CkJsonObjectW_stringOf(jResp,L"condition.event");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"action.notification.recipients");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        strVal = CkJsonObjectW_stringOf(jResp,L"action.notification.recipients[i]");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"condition.resources");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        intVal = CkJsonObjectW_IntOf(jResp,L"condition.resources[i]");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }

Curl Command

curl -X PUT
	-H "X-ANYPNT-ENV-ID: {{environment_id}}"
	-H "X-ANYPNT-ORG-ID: {{organization_id}}"
	-H "Authorization: Bearer {{token}}"
	-d '{
    "name": "Low CPU usage",
    "severity": "WARNING",
    "actions": [
        {
            "type": "email",
            "content": "Hello,\nYou are receiving this alert because:\nThe application ${resource} is now in an ${state} state, based on the condition \'CPU ${operator} ${value}%.",
            "subject": "${severity}: CPU usage ${state}",
            "emails": [
                "angel.alberici+testalerts@mulesoft.com",
                "angel.alberici@mulesoft.com"
            ]
        }
    ],
    "condition": {
        "operator": "LESS_THAN",
        "value": 50,
        "periodCount": 1,
        "resourceType": "cloudhub-application",
        "type": "cpu",
        "periodMins": 1,
        "resources": [
            "*"
        ]
    }
}'
https://anypoint.mulesoft.com/armui/api/v1/alerts/cloudhub/02213013-10f2-4bbb-b399-718c0a543e18

Postman Collection Item JSON

{
  "name": "Update alert by id",
  "request": {
    "method": "PUT",
    "header": [
      {
        "description": "(Required) Identifier of the environment where the api user is currently working.",
        "key": "X-ANYPNT-ENV-ID",
        "value": "{{environment_id}}"
      },
      {
        "description": "(Required) Identifier of the sub org where the api user is currently working.",
        "key": "X-ANYPNT-ORG-ID",
        "value": "{{organization_id}}"
      },
      {
        "key": "Authorization",
        "value": "Bearer {{token}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"name\": \"Low CPU usage\",\n    \"severity\": \"WARNING\",\n    \"actions\": [\n        {\n            \"type\": \"email\",\n            \"content\": \"Hello,\\nYou are receiving this alert because:\\nThe application ${resource} is now in an ${state} state, based on the condition 'CPU ${operator} ${value}%.\",\n            \"subject\": \"${severity}: CPU usage ${state}\",\n            \"emails\": [\n                \"angel.alberici+testalerts@mulesoft.com\",\n                \"angel.alberici@mulesoft.com\"\n            ]\n        }\n    ],\n    \"condition\": {\n        \"operator\": \"LESS_THAN\",\n        \"value\": 50,\n        \"periodCount\": 1,\n        \"resourceType\": \"cloudhub-application\",\n        \"type\": \"cpu\",\n        \"periodMins\": 1,\n        \"resources\": [\n            \"*\"\n        ]\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://anypoint.mulesoft.com/armui/api/v1/alerts/cloudhub/02213013-10f2-4bbb-b399-718c0a543e18",
      "protocol": "https",
      "host": [
        "anypoint",
        "mulesoft",
        "com"
      ],
      "path": [
        "armui",
        "api",
        "v1",
        "alerts",
        "cloudhub",
        "02213013-10f2-4bbb-b399-718c0a543e18"
      ]
    }
  },
  "response": [
    {
      "name": "Untitled Example",
      "originalRequest": {
        "method": "PATCH",
        "header": [
          {
            "description": "(Required) Identifier of the environment where the api user is currently working.",
            "key": "X-ANYPNT-ENV-ID",
            "value": "pariatur magna in id esse"
          },
          {
            "description": "(Required) Identifier of the sub org where the api user is currently working.",
            "key": "X-ANYPNT-ORG-ID",
            "value": "pariatur magna in id esse"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/alerts/:alertId",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "alerts",
            ":alertId"
          ],
          "variable": [
            {
              "key": "alertId"
            }
          ]
        }
      },
      "status": "Accepted",
      "code": 202,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"id\": 2312,\n \"timeCreated\": 342423432142342,\n \"timeUpdated\": 342423432154342,\n \"severity\": \"CRITICAL\",\n \"description\": \"Notify when one server of cluster 45 went down\",\n \"enabled\": true,\n \"action\": {\n  \"notification\": {\n   \"recipients\": [\n    \"alex.tiavo\",\n    \"greg.kio\"\n   ]\n  }\n },\n \"condition\": {\n  \"resourceType\": \"CLUSTER\",\n  \"resources\": [\n   45\n  ],\n  \"event\": \"DISCONNECTED\"\n }\n}"
    }
  ]
}