Chilkat Online Tools

DescribeStream Unicode C Example

Amazon Kinesis Video Streams

#include <C_CkRestW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    HCkAuthAwsW authAws;
    HCkJsonObjectW json;
    HCkStringBuilderW sbRequestBody;
    HCkStringBuilderW sbResponseBody;
    int respStatusCode;
    HCkJsonObjectW jResp;
    int j;
    int count_j;
    const wchar_t *strVal;
    const wchar_t *AdjacentParentShardId;
    const wchar_t *EndingHashKey;
    const wchar_t *StartingHashKey;
    const wchar_t *ParentShardId;
    const wchar_t *EndingSequenceNumber;
    const wchar_t *StartingSequenceNumber;
    const wchar_t *ShardId;
    const wchar_t *EncryptionType;
    int HasMoreShards;
    const wchar_t *KeyId;
    int RetentionPeriodHours;
    const wchar_t *StreamARN;
    int StreamCreationTimestamp;
    const wchar_t *StreamMode;
    const wchar_t *StreamName;
    const wchar_t *StreamStatus;
    int i;
    int count_i;

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

    rest = CkRestW_Create();

    authAws = CkAuthAwsW_Create();
    CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY");
    CkAuthAwsW_putSecretKey(authAws,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.)
    CkAuthAwsW_putRegion(authAws,L"us-west-2");
    CkAuthAwsW_putServiceName(authAws,L"kinesisvideo");
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    CkRestW_SetAuthAws(rest,authAws);

    // URL: https://kinesisvideo.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = CkRestW_Connect(rest,L"kinesisvideo.us-west-2.amazonaws.com",443,TRUE,TRUE);
    if (success != TRUE) {
        wprintf(L"ConnectFailReason: %d\n",CkRestW_getConnectFailReason(rest));
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        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

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"ExclusiveStartShardId",L"string");
    CkJsonObjectW_UpdateInt(json,L"Limit",123);
    CkJsonObjectW_UpdateString(json,L"StreamName",L"string");

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

    // {
    //   "ExclusiveStartShardId": "string",
    //   "Limit": number,
    //   "StreamName": "string"
    // }

    CkRestW_AddHeader(rest,L"Content-Type",L"application/x-amz-json-1.1");
    CkRestW_AddHeader(rest,L"X-Amz-Target",L"DescribeStream");

    sbRequestBody = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(json,sbRequestBody);
    sbResponseBody = CkStringBuilderW_Create();
    success = CkRestW_FullRequestSb(rest,L"POST",L"/describeStream",sbRequestBody,sbResponseBody);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    respStatusCode = CkRestW_getResponseStatusCode(rest);
    wprintf(L"response status code = %d\n",respStatusCode);
    if (respStatusCode != 200) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkRestW_responseHeader(rest));
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponseBody));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,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.

    EncryptionType = CkJsonObjectW_stringOf(jResp,L"StreamDescription.EncryptionType");
    HasMoreShards = CkJsonObjectW_IntOf(jResp,L"StreamDescription.HasMoreShards");
    KeyId = CkJsonObjectW_stringOf(jResp,L"StreamDescription.KeyId");
    RetentionPeriodHours = CkJsonObjectW_IntOf(jResp,L"StreamDescription.RetentionPeriodHours");
    StreamARN = CkJsonObjectW_stringOf(jResp,L"StreamDescription.StreamARN");
    StreamCreationTimestamp = CkJsonObjectW_IntOf(jResp,L"StreamDescription.StreamCreationTimestamp");
    StreamMode = CkJsonObjectW_stringOf(jResp,L"StreamDescription.StreamModeDetails.StreamMode");
    StreamName = CkJsonObjectW_stringOf(jResp,L"StreamDescription.StreamName");
    StreamStatus = CkJsonObjectW_stringOf(jResp,L"StreamDescription.StreamStatus");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"StreamDescription.EnhancedMonitoring");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(jResp,L"StreamDescription.EnhancedMonitoring[i].ShardLevelMetrics");
        while (j < count_j) {
            CkJsonObjectW_putJ(jResp,j);
            strVal = CkJsonObjectW_stringOf(jResp,L"StreamDescription.EnhancedMonitoring[i].ShardLevelMetrics[j]");
            j = j + 1;
        }

        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"StreamDescription.Shards");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        AdjacentParentShardId = CkJsonObjectW_stringOf(jResp,L"StreamDescription.Shards[i].AdjacentParentShardId");
        EndingHashKey = CkJsonObjectW_stringOf(jResp,L"StreamDescription.Shards[i].HashKeyRange.EndingHashKey");
        StartingHashKey = CkJsonObjectW_stringOf(jResp,L"StreamDescription.Shards[i].HashKeyRange.StartingHashKey");
        ParentShardId = CkJsonObjectW_stringOf(jResp,L"StreamDescription.Shards[i].ParentShardId");
        EndingSequenceNumber = CkJsonObjectW_stringOf(jResp,L"StreamDescription.Shards[i].SequenceNumberRange.EndingSequenceNumber");
        StartingSequenceNumber = CkJsonObjectW_stringOf(jResp,L"StreamDescription.Shards[i].SequenceNumberRange.StartingSequenceNumber");
        ShardId = CkJsonObjectW_stringOf(jResp,L"StreamDescription.Shards[i].ShardId");
        i = i + 1;
    }

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

    // {
    //   "StreamDescription": {
    //     "EncryptionType": "string",
    //     "EnhancedMonitoring": [
    //       {
    //         "ShardLevelMetrics": [
    //           "string"
    //         ]
    //       }
    //     ],
    //     "HasMoreShards": boolean,
    //     "KeyId": "string",
    //     "RetentionPeriodHours": number,
    //     "Shards": [
    //       {
    //         "AdjacentParentShardId": "string",
    //         "HashKeyRange": {
    //           "EndingHashKey": "string",
    //           "StartingHashKey": "string"
    //         },
    //         "ParentShardId": "string",
    //         "SequenceNumberRange": {
    //           "EndingSequenceNumber": "string",
    //           "StartingSequenceNumber": "string"
    //         },
    //         "ShardId": "string"
    //       }
    //     ],
    //     "StreamARN": "string",
    //     "StreamCreationTimestamp": number,
    //     "StreamModeDetails": {
    //       "StreamMode": "string"
    //     },
    //     "StreamName": "string",
    //     "StreamStatus": "string"
    //   }
    // }


    CkRestW_Dispose(rest);
    CkAuthAwsW_Dispose(authAws);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }