Chilkat Online Tools

SearchInsights Swift3 Example

Amazon DevOps Guru

func chilkatTest() {
    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let rest = CkoRest()!
    var success: Bool

    let authAws = CkoAuthAws()!
    authAws.accessKey = "AWS_ACCESS_KEY"
    authAws.secretKey = "AWS_SECRET_KEY"

    // Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
    authAws.region = "us-west-2"
    authAws.serviceName = "devops-guru"
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    rest.setAuthAws(authAws)

    // URL: https://devops-guru.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = rest.connect("devops-guru.us-west-2.amazonaws.com", port: 443, tls: true, autoReconnect: true)
    if success != true {
        print("ConnectFailReason: \(rest.connectFailReason.intValue)")
        print("\(rest.lastErrorText!)")
        return
    }

    // The following code creates the JSON request body.
    // The JSON created by this code is shown below.

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    let json = CkoJsonObject()!
    json.update("Filters.ResourceCollection.CloudFormation.StackNames[0]", value: "string")
    json.update("Filters.ResourceCollection.Tags[0].AppBoundaryKey", value: "string")
    json.update("Filters.ResourceCollection.Tags[0].TagValues[0]", value: "string")
    json.update("Filters.ServiceCollection.ServiceNames[0]", value: "string")
    json.update("Filters.Severities[0]", value: "string")
    json.update("Filters.Statuses[0]", value: "string")
    json.updateInt("MaxResults", value: 123)
    json.update("NextToken", value: "string")
    json.updateInt("StartTimeRange.FromTime", value: 123)
    json.updateInt("StartTimeRange.ToTime", value: 123)
    json.update("Type", value: "string")

    // The JSON request body created by the above code:

    // {
    //   "Filters": {
    //     "ResourceCollection": {
    //       "CloudFormation": {
    //         "StackNames": [
    //           "string"
    //         ]
    //       },
    //       "Tags": [
    //         {
    //           "AppBoundaryKey": "string",
    //           "TagValues": [
    //             "string"
    //           ]
    //         }
    //       ]
    //     },
    //     "ServiceCollection": {
    //       "ServiceNames": [
    //         "string"
    //       ]
    //     },
    //     "Severities": [
    //       "string"
    //     ],
    //     "Statuses": [
    //       "string"
    //     ]
    //   },
    //   "MaxResults": number,
    //   "NextToken": "string",
    //   "StartTimeRange": {
    //     "FromTime": number,
    //     "ToTime": number
    //   },
    //   "Type": "string"
    // }

    rest.addHeader("Content-Type", value: "application/x-amz-json-1.1")
    rest.addHeader("X-Amz-Target", value: "SearchInsights")

    let sbRequestBody = CkoStringBuilder()!
    json.emitSb(sbRequestBody)
    let sbResponseBody = CkoStringBuilder()!
    success = rest.fullRequestSb("POST", uriPath: "/insights/search", requestBody: sbRequestBody, responseBody: sbResponseBody)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    var respStatusCode: Int = rest.responseStatusCode.intValue
    print("response status code = \(respStatusCode)")
    if respStatusCode != 200 {
        print("Response Header:")
        print("\(rest.responseHeader!)")
        print("Response Body:")
        print("\(sbResponseBody.getAsString()!)")
        return
    }

    let jResp = CkoJsonObject()!
    jResp.loadSb(sbResponseBody)

    // The following code parses the JSON response.
    // A sample JSON response is shown below the sample code.

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var Id: String?
    var EndTime: Int
    var StartTime: Int
    var Name: String?
    var PredictionTimeRangeEndTime: Int
    var PredictionTimeRangeStartTime: Int
    var Severity: String?
    var Status: String?
    var j: Int
    var count_j: Int
    var strVal: String?
    var AppBoundaryKey: String?
    var k: Int
    var count_k: Int
    var InsightTimeRangeEndTime: Int
    var InsightTimeRangeStartTime: Int

    var NextToken: String? = jResp.string(of: "NextToken")
    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "ProactiveInsights").intValue
    while i < count_i {
        jResp.i = i
        Id = jResp.string(of: "ProactiveInsights[i].Id")
        EndTime = jResp.int(of: "ProactiveInsights[i].InsightTimeRange.EndTime").intValue
        StartTime = jResp.int(of: "ProactiveInsights[i].InsightTimeRange.StartTime").intValue
        Name = jResp.string(of: "ProactiveInsights[i].Name")
        PredictionTimeRangeEndTime = jResp.int(of: "ProactiveInsights[i].PredictionTimeRange.EndTime").intValue
        PredictionTimeRangeStartTime = jResp.int(of: "ProactiveInsights[i].PredictionTimeRange.StartTime").intValue
        Severity = jResp.string(of: "ProactiveInsights[i].Severity")
        Status = jResp.string(of: "ProactiveInsights[i].Status")
        j = 0
        count_j = jResp.size(ofArray: "ProactiveInsights[i].AssociatedResourceArns").intValue
        while j < count_j {
            jResp.j = j
            strVal = jResp.string(of: "ProactiveInsights[i].AssociatedResourceArns[j]")
            j = j + 1
        }

        j = 0
        count_j = jResp.size(ofArray: "ProactiveInsights[i].ResourceCollection.CloudFormation.StackNames").intValue
        while j < count_j {
            jResp.j = j
            strVal = jResp.string(of: "ProactiveInsights[i].ResourceCollection.CloudFormation.StackNames[j]")
            j = j + 1
        }

        j = 0
        count_j = jResp.size(ofArray: "ProactiveInsights[i].ResourceCollection.Tags").intValue
        while j < count_j {
            jResp.j = j
            AppBoundaryKey = jResp.string(of: "ProactiveInsights[i].ResourceCollection.Tags[j].AppBoundaryKey")
            k = 0
            count_k = jResp.size(ofArray: "ProactiveInsights[i].ResourceCollection.Tags[j].TagValues").intValue
            while k < count_k {
                jResp.k = k
                strVal = jResp.string(of: "ProactiveInsights[i].ResourceCollection.Tags[j].TagValues[k]")
                k = k + 1
            }

            j = j + 1
        }

        j = 0
        count_j = jResp.size(ofArray: "ProactiveInsights[i].ServiceCollection.ServiceNames").intValue
        while j < count_j {
            jResp.j = j
            strVal = jResp.string(of: "ProactiveInsights[i].ServiceCollection.ServiceNames[j]")
            j = j + 1
        }

        i = i + 1
    }

    i = 0
    count_i = jResp.size(ofArray: "ReactiveInsights").intValue
    while i < count_i {
        jResp.i = i
        Id = jResp.string(of: "ReactiveInsights[i].Id")
        InsightTimeRangeEndTime = jResp.int(of: "ReactiveInsights[i].InsightTimeRange.EndTime").intValue
        InsightTimeRangeStartTime = jResp.int(of: "ReactiveInsights[i].InsightTimeRange.StartTime").intValue
        Name = jResp.string(of: "ReactiveInsights[i].Name")
        Severity = jResp.string(of: "ReactiveInsights[i].Severity")
        Status = jResp.string(of: "ReactiveInsights[i].Status")
        j = 0
        count_j = jResp.size(ofArray: "ReactiveInsights[i].AssociatedResourceArns").intValue
        while j < count_j {
            jResp.j = j
            strVal = jResp.string(of: "ReactiveInsights[i].AssociatedResourceArns[j]")
            j = j + 1
        }

        j = 0
        count_j = jResp.size(ofArray: "ReactiveInsights[i].ResourceCollection.CloudFormation.StackNames").intValue
        while j < count_j {
            jResp.j = j
            strVal = jResp.string(of: "ReactiveInsights[i].ResourceCollection.CloudFormation.StackNames[j]")
            j = j + 1
        }

        j = 0
        count_j = jResp.size(ofArray: "ReactiveInsights[i].ResourceCollection.Tags").intValue
        while j < count_j {
            jResp.j = j
            AppBoundaryKey = jResp.string(of: "ReactiveInsights[i].ResourceCollection.Tags[j].AppBoundaryKey")
            k = 0
            count_k = jResp.size(ofArray: "ReactiveInsights[i].ResourceCollection.Tags[j].TagValues").intValue
            while k < count_k {
                jResp.k = k
                strVal = jResp.string(of: "ReactiveInsights[i].ResourceCollection.Tags[j].TagValues[k]")
                k = k + 1
            }

            j = j + 1
        }

        j = 0
        count_j = jResp.size(ofArray: "ReactiveInsights[i].ServiceCollection.ServiceNames").intValue
        while j < count_j {
            jResp.j = j
            strVal = jResp.string(of: "ReactiveInsights[i].ServiceCollection.ServiceNames[j]")
            j = j + 1
        }

        i = i + 1
    }

    // A sample JSON response body parsed by the above code:

    // {
    //   "NextToken": "string",
    //   "ProactiveInsights": [
    //     {
    //       "AssociatedResourceArns": [
    //         "string"
    //       ],
    //       "Id": "string",
    //       "InsightTimeRange": {
    //         "EndTime": number,
    //         "StartTime": number
    //       },
    //       "Name": "string",
    //       "PredictionTimeRange": {
    //         "EndTime": number,
    //         "StartTime": number
    //       },
    //       "ResourceCollection": {
    //         "CloudFormation": {
    //           "StackNames": [
    //             "string"
    //           ]
    //         },
    //         "Tags": [
    //           {
    //             "AppBoundaryKey": "string",
    //             "TagValues": [
    //               "string"
    //             ]
    //           }
    //         ]
    //       },
    //       "ServiceCollection": {
    //         "ServiceNames": [
    //           "string"
    //         ]
    //       },
    //       "Severity": "string",
    //       "Status": "string"
    //     }
    //   ],
    //   "ReactiveInsights": [
    //     {
    //       "AssociatedResourceArns": [
    //         "string"
    //       ],
    //       "Id": "string",
    //       "InsightTimeRange": {
    //         "EndTime": number,
    //         "StartTime": number
    //       },
    //       "Name": "string",
    //       "ResourceCollection": {
    //         "CloudFormation": {
    //           "StackNames": [
    //             "string"
    //           ]
    //         },
    //         "Tags": [
    //           {
    //             "AppBoundaryKey": "string",
    //             "TagValues": [
    //               "string"
    //             ]
    //           }
    //         ]
    //       },
    //       "ServiceCollection": {
    //         "ServiceNames": [
    //           "string"
    //         ]
    //       },
    //       "Severity": "string",
    //       "Status": "string"
    //     }
    //   ]
    // }

}