Chilkat Online Tools

GetAnswer unicodeCpp Example

AWS Well-Architected Tool

#include <CkRestW.h>
#include <CkAuthAwsW.h>
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>

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

    CkRestW rest;
    bool success;

    CkAuthAwsW authAws;
    authAws.put_AccessKey(L"AWS_ACCESS_KEY");
    authAws.put_SecretKey(L"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.put_Region(L"us-west-2");
    authAws.put_ServiceName(L"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(L"wellarchitected.us-west-2.amazonaws.com",443,true,true);
    if (success != true) {
        wprintf(L"ConnectFailReason: %d\n",rest.get_ConnectFailReason());
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

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

    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestNoBodySb(L"GET",L"/workloads/{WorkloadId}/lensReviews/{LensAlias}/answers/{QuestionId}",sbResponseBody);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();
    wprintf(L"response status code = %d\n",respStatusCode);
    if (respStatusCode != 200) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",rest.responseHeader());
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",sbResponseBody.getAsString());
        return;
    }

    CkJsonObjectW jResp;
    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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    const wchar_t *ChoiceId = 0;
    const wchar_t *Status = 0;
    const wchar_t *Description = 0;
    const wchar_t *DisplayText = 0;
    const wchar_t *v_Url = 0;
    const wchar_t *ImprovementPlanDisplayText = 0;
    const wchar_t *Title = 0;
    const wchar_t *strVal = 0;

    const wchar_t *HelpfulResourceDisplayText = jResp.stringOf(L"Answer.HelpfulResourceDisplayText");
    const wchar_t *HelpfulResourceUrl = jResp.stringOf(L"Answer.HelpfulResourceUrl");
    const wchar_t *ImprovementPlanUrl = jResp.stringOf(L"Answer.ImprovementPlanUrl");
    int IsApplicable = jResp.IntOf(L"Answer.IsApplicable");
    const wchar_t *Notes = jResp.stringOf(L"Answer.Notes");
    const wchar_t *PillarId = jResp.stringOf(L"Answer.PillarId");
    const wchar_t *QuestionDescription = jResp.stringOf(L"Answer.QuestionDescription");
    const wchar_t *QuestionId = jResp.stringOf(L"Answer.QuestionId");
    const wchar_t *QuestionTitle = jResp.stringOf(L"Answer.QuestionTitle");
    const wchar_t *Reason = jResp.stringOf(L"Answer.Reason");
    const wchar_t *Risk = jResp.stringOf(L"Answer.Risk");
    const wchar_t *LensAlias = jResp.stringOf(L"LensAlias");
    const wchar_t *LensArn = jResp.stringOf(L"LensArn");
    int MilestoneNumber = jResp.IntOf(L"MilestoneNumber");
    const wchar_t *WorkloadId = jResp.stringOf(L"WorkloadId");
    int i = 0;
    int count_i = jResp.SizeOfArray(L"Answer.ChoiceAnswers");
    while (i < count_i) {
        jResp.put_I(i);
        ChoiceId = jResp.stringOf(L"Answer.ChoiceAnswers[i].ChoiceId");
        Notes = jResp.stringOf(L"Answer.ChoiceAnswers[i].Notes");
        Reason = jResp.stringOf(L"Answer.ChoiceAnswers[i].Reason");
        Status = jResp.stringOf(L"Answer.ChoiceAnswers[i].Status");
        i = i + 1;
    }

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

    i = 0;
    count_i = jResp.SizeOfArray(L"Answer.SelectedChoices");
    while (i < count_i) {
        jResp.put_I(i);
        strVal = jResp.stringOf(L"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"
    // }
    }