Chilkat Online Tools

GetAnswer Swift Example

AWS Well-Architected Tool

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 = "wellarchitected"
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    rest.SetAuthAws(authAws)

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

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

    let sbResponseBody = CkoStringBuilder()
    success = rest.FullRequestNoBodySb("GET", uriPath: "/workloads/{WorkloadId}/lensReviews/{LensAlias}/answers/{QuestionId}", sb: 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 ChoiceId: String?
    var Status: String?
    var Description: String?
    var DisplayText: String?
    var v_Url: String?
    var ImprovementPlanDisplayText: String?
    var Title: String?
    var strVal: String?

    var HelpfulResourceDisplayText: String? = jResp.StringOf("Answer.HelpfulResourceDisplayText")
    var HelpfulResourceUrl: String? = jResp.StringOf("Answer.HelpfulResourceUrl")
    var ImprovementPlanUrl: String? = jResp.StringOf("Answer.ImprovementPlanUrl")
    var IsApplicable: Int = jResp.IntOf("Answer.IsApplicable").intValue
    var Notes: String? = jResp.StringOf("Answer.Notes")
    var PillarId: String? = jResp.StringOf("Answer.PillarId")
    var QuestionDescription: String? = jResp.StringOf("Answer.QuestionDescription")
    var QuestionId: String? = jResp.StringOf("Answer.QuestionId")
    var QuestionTitle: String? = jResp.StringOf("Answer.QuestionTitle")
    var Reason: String? = jResp.StringOf("Answer.Reason")
    var Risk: String? = jResp.StringOf("Answer.Risk")
    var LensAlias: String? = jResp.StringOf("LensAlias")
    var LensArn: String? = jResp.StringOf("LensArn")
    var MilestoneNumber: Int = jResp.IntOf("MilestoneNumber").intValue
    var WorkloadId: String? = jResp.StringOf("WorkloadId")
    var i: Int = 0
    var count_i: Int = jResp.SizeOfArray("Answer.ChoiceAnswers").intValue
    while i < count_i {
        jResp.I = i
        ChoiceId = jResp.StringOf("Answer.ChoiceAnswers[i].ChoiceId")
        Notes = jResp.StringOf("Answer.ChoiceAnswers[i].Notes")
        Reason = jResp.StringOf("Answer.ChoiceAnswers[i].Reason")
        Status = jResp.StringOf("Answer.ChoiceAnswers[i].Status")
        i = i + 1
    }

    i = 0
    count_i = jResp.SizeOfArray("Answer.Choices").intValue
    while i < count_i {
        jResp.I = i
        ChoiceId = jResp.StringOf("Answer.Choices[i].ChoiceId")
        Description = jResp.StringOf("Answer.Choices[i].Description")
        DisplayText = jResp.StringOf("Answer.Choices[i].HelpfulResource.DisplayText")
        v_Url = jResp.StringOf("Answer.Choices[i].HelpfulResource.Url")
        ImprovementPlanDisplayText = jResp.StringOf("Answer.Choices[i].ImprovementPlan.DisplayText")
        ImprovementPlanUrl = jResp.StringOf("Answer.Choices[i].ImprovementPlan.Url")
        Title = jResp.StringOf("Answer.Choices[i].Title")
        i = i + 1
    }

    i = 0
    count_i = jResp.SizeOfArray("Answer.SelectedChoices").intValue
    while i < count_i {
        jResp.I = i
        strVal = jResp.StringOf("Answer.SelectedChoices[i]")
        i = i + 1
    }

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

    // {
    //   "Answer": {
    //     "ChoiceAnswers": [
    //       {
    //         "ChoiceId": "string",
    //         "Notes": "string",
    //         "Reason": "string",
    //         "Status": "string"
    //       }
    //     ],
    //     "Choices": [
    //       {
    //         "ChoiceId": "string",
    //         "Description": "string",
    //         "HelpfulResource": {
    //           "DisplayText": "string",
    //           "Url": "string"
    //         },
    //         "ImprovementPlan": {
    //           "DisplayText": "string",
    //           "Url": "string"
    //         },
    //         "Title": "string"
    //       }
    //     ],
    //     "HelpfulResourceDisplayText": "string",
    //     "HelpfulResourceUrl": "string",
    //     "ImprovementPlanUrl": "string",
    //     "IsApplicable": boolean,
    //     "Notes": "string",
    //     "PillarId": "string",
    //     "QuestionDescription": "string",
    //     "QuestionId": "string",
    //     "QuestionTitle": "string",
    //     "Reason": "string",
    //     "Risk": "string",
    //     "SelectedChoices": [
    //       "string"
    //     ]
    //   },
    //   "LensAlias": "string",
    //   "LensArn": "string",
    //   "MilestoneNumber": number,
    //   "WorkloadId": "string"
    // }

}