Chilkat Online Tools

DataFlex / ForgeRock Identity Cloud Collection / Step 4: Tail the Audit Logs for a Source Since Last Request

Back to Collection Items

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoHttp
    Boolean iSuccess
    Variant vQueryParams
    Handle hoQueryParams
    Variant vResp
    Handle hoResp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Handle hoJResp
    Integer iRespStatusCode
    String sPayload
    String sTimestamp
    String sV_type
    String sResultCount
    String sPagedResultsCookie
    String sTotalPagedResultsPolicy
    String sTotalPagedResults
    String sRemainingPagedResults
    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

    Get Create (RefClass(cComChilkatJsonObject)) To hoQueryParams
    If (Not(IsComObjectCreated(hoQueryParams))) Begin
        Send CreateComObject of hoQueryParams
    End
    Get ComUpdateString Of hoQueryParams "source" "{{auditSource}}" To iSuccess
    Get ComUpdateString Of hoQueryParams "_pagedResultsCookie" "{{pagedResultsCookie}}" To iSuccess
    Get ComUpdateString Of hoQueryParams "_prettyPrint" "false" To iSuccess

    Send ComSetRequestHeader To hoHttp "x-api-secret" "{{logApiSecret}}"
    Send ComSetRequestHeader To hoHttp "x-api-key" "{{logApiKey}}"

    Get pvComObject of hoQueryParams to vQueryParams
    Get ComQuickRequestParams Of hoHttp "GET" "https://<tenant-name>.forgeblocks.com/monitoring/logs/tail" vQueryParams 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)

    // {
    //   "result": [
    //     {
    //       "payload": "<object>",
    //       "timestamp": "<dateTime>",
    //       "type": "<string>"
    //     },
    //     {
    //       "payload": "<object>",
    //       "timestamp": "<dateTime>",
    //       "type": "<string>"
    //     }
    //   ],
    //   "resultCount": "<integer>",
    //   "pagedResultsCookie": "<string>",
    //   "totalPagedResultsPolicy": "<string>",
    //   "totalPagedResults": "<integer>",
    //   "remainingPagedResults": "<integer>"
    // }

    // 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 "resultCount" To sResultCount
    Get ComStringOf Of hoJResp "pagedResultsCookie" To sPagedResultsCookie
    Get ComStringOf Of hoJResp "totalPagedResultsPolicy" To sTotalPagedResultsPolicy
    Get ComStringOf Of hoJResp "totalPagedResults" To sTotalPagedResults
    Get ComStringOf Of hoJResp "remainingPagedResults" To sRemainingPagedResults
    Move 0 To i
    Get ComSizeOfArray Of hoJResp "result" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJResp To i
        Get ComStringOf Of hoJResp "result[i].payload" To sPayload
        Get ComStringOf Of hoJResp "result[i].timestamp" To sTimestamp
        Get ComStringOf Of hoJResp "result[i].type" To sV_type
        Move (i + 1) To i
    Loop



End_Procedure

Curl Command

curl -G -d "source=%7B%7BauditSource%7D%7D"
	-d "_pagedResultsCookie=%7B%7BpagedResultsCookie%7D%7D"
	-d "_prettyPrint=false"
	-H "x-api-key: {{logApiKey}}"
	-H "x-api-secret: {{logApiSecret}}"
https://<tenant-name>.forgeblocks.com/monitoring/logs/tail

Postman Collection Item JSON

{
  "name": "Step 4: Tail the Audit Logs for a Source Since Last Request",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "const jsonData = JSON.parse(responseBody);",
          "if(jsonData.pagedResultsCookie && jsonData.pagedResultsCookie != \"\"){",
          "    pm.globals.set(\"pagedResultsCookie\", jsonData.pagedResultsCookie);",
          "}",
          "",
          "// Tests",
          "",
          "pm.test(\"Status code is 200.\", () => {",
          "  pm.expect(pm.response.code).to.eql(200);",
          "});",
          "",
          "pm.test(\"Response contains results\", function () {",
          "    pm.expect(jsonData).to.have.any.keys('resultCount', 'result');",
          "});",
          "",
          "pm.test(\"Response contains pagedResultsCookie\", function () {",
          "    pm.expect(jsonData).to.have.any.keys('pagedResultsCookie');",
          "});",
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "GET",
    "header": [
      {
        "key": "x-api-key",
        "type": "text",
        "value": "{{logApiKey}}"
      },
      {
        "key": "x-api-secret",
        "type": "text",
        "value": "{{logApiSecret}}"
      }
    ],
    "url": {
      "raw": "{{platformUrl}}/monitoring/logs/tail?source={{auditSource}}&_pagedResultsCookie={{pagedResultsCookie}}&_prettyPrint=false",
      "host": [
        "{{platformUrl}}"
      ],
      "path": [
        "monitoring",
        "logs",
        "tail"
      ],
      "query": [
        {
          "key": "source",
          "value": "{{auditSource}}"
        },
        {
          "key": "_pagedResultsCookie",
          "value": "{{pagedResultsCookie}}"
        },
        {
          "key": "_prettyPrint",
          "value": "false"
        }
      ]
    },
    "description": "Tail log events"
  },
  "response": [
    {
      "name": "List of log events",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "{{baseUrl}}/logs/tail?source=<string>&_pagedResultsCookie=<string>&_prettyPrint=false",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "logs",
            "tail"
          ],
          "query": [
            {
              "key": "source",
              "value": "<string>"
            },
            {
              "key": "_pagedResultsCookie",
              "value": "<string>"
            },
            {
              "key": "_prettyPrint",
              "value": "false"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n\t\"result\": [\n\t\t{\n\t\t\t\"payload\": \"<object>\",\n\t\t\t\"timestamp\": \"<dateTime>\",\n\t\t\t\"type\": \"<string>\"\n\t\t},\n\t\t{\n\t\t\t\"payload\": \"<object>\",\n\t\t\t\"timestamp\": \"<dateTime>\",\n\t\t\t\"type\": \"<string>\"\n\t\t}\n\t],\n\t\"resultCount\": \"<integer>\",\n\t\"pagedResultsCookie\": \"<string>\",\n\t\"totalPagedResultsPolicy\": \"<string>\",\n\t\"totalPagedResults\": \"<integer>\",\n\t\"remainingPagedResults\": \"<integer>\"\n}"
    }
  ]
}