Chilkat Online Tools

GetImageRecipe PureBasic Example

EC2 Image Builder

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkAuthAws.pb"

Procedure ChilkatExample()

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

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    authAws.i = CkAuthAws::ckCreate()
    If authAws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
    CkAuthAws::setCkSecretKey(authAws, "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.)
    CkAuthAws::setCkRegion(authAws, "us-west-2")
    CkAuthAws::setCkServiceName(authAws, "imagebuilder")
    ; SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    CkRest::ckSetAuthAws(rest,authAws)

    ; URL: https://imagebuilder.us-west-2.amazonaws.com/
    ; Use the same region as specified above.
    success = CkRest::ckConnect(rest,"imagebuilder.us-west-2.amazonaws.com",443,1,1)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

    CkRest::ckAddHeader(rest,"Content-Type","application/x-amz-json-1.1")
    CkRest::ckAddHeader(rest,"X-Amz-Target","GetImageRecipe")

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestNoBodySb(rest,"GET","/GetImageRecipe",sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkRest::ckResponseStatusCode(rest)
    Debug "response status code = " + Str(respStatusCode)
    If respStatusCode <> 200
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug CkStringBuilder::ckGetAsString(sbResponseBody)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(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

    deviceName.s
    DeleteOnTermination.i
    Encrypted.i
    Iops.i
    KmsKeyId.s
    SnapshotId.s
    Throughput.i
    VolumeSize.i
    VolumeType.s
    noDevice.s
    virtualName.s
    componentArn.s
    j.i
    count_j.i
    name.s
    k.i
    count_k.i
    strVal.s

    UninstallAfterBuild.i = CkJsonObject::ckIntOf(jResp,"imageRecipe.additionalInstanceConfiguration.systemsManagerAgent.uninstallAfterBuild")
    UserDataOverride.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.additionalInstanceConfiguration.userDataOverride")
    Arn.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.arn")
    DateCreated.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.dateCreated")
    Description.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.description")
    Name.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.name")
    Owner.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.owner")
    ParentImage.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.parentImage")
    Platform.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.platform")
    v_String.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.tags.string")
    v_Type.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.type")
    Version.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.version")
    WorkingDirectory.s = CkJsonObject::ckStringOf(jResp,"imageRecipe.workingDirectory")
    requestId.s = CkJsonObject::ckStringOf(jResp,"requestId")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"imageRecipe.blockDeviceMappings")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        deviceName = CkJsonObject::ckStringOf(jResp,"imageRecipe.blockDeviceMappings[i].deviceName")
        DeleteOnTermination = CkJsonObject::ckIntOf(jResp,"imageRecipe.blockDeviceMappings[i].ebs.deleteOnTermination")
        Encrypted = CkJsonObject::ckIntOf(jResp,"imageRecipe.blockDeviceMappings[i].ebs.encrypted")
        Iops = CkJsonObject::ckIntOf(jResp,"imageRecipe.blockDeviceMappings[i].ebs.iops")
        KmsKeyId = CkJsonObject::ckStringOf(jResp,"imageRecipe.blockDeviceMappings[i].ebs.kmsKeyId")
        SnapshotId = CkJsonObject::ckStringOf(jResp,"imageRecipe.blockDeviceMappings[i].ebs.snapshotId")
        Throughput = CkJsonObject::ckIntOf(jResp,"imageRecipe.blockDeviceMappings[i].ebs.throughput")
        VolumeSize = CkJsonObject::ckIntOf(jResp,"imageRecipe.blockDeviceMappings[i].ebs.volumeSize")
        VolumeType = CkJsonObject::ckStringOf(jResp,"imageRecipe.blockDeviceMappings[i].ebs.volumeType")
        noDevice = CkJsonObject::ckStringOf(jResp,"imageRecipe.blockDeviceMappings[i].noDevice")
        virtualName = CkJsonObject::ckStringOf(jResp,"imageRecipe.blockDeviceMappings[i].virtualName")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jResp,"imageRecipe.components")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        componentArn = CkJsonObject::ckStringOf(jResp,"imageRecipe.components[i].componentArn")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jResp,"imageRecipe.components[i].parameters")
        While j < count_j
            CkJsonObject::setCkJ(jResp, j)
            name = CkJsonObject::ckStringOf(jResp,"imageRecipe.components[i].parameters[j].name")
            k = 0
            count_k = CkJsonObject::ckSizeOfArray(jResp,"imageRecipe.components[i].parameters[j].value")
            While k < count_k
                CkJsonObject::setCkK(jResp, k)
                strVal = CkJsonObject::ckStringOf(jResp,"imageRecipe.components[i].parameters[j].value[k]")
                k = k + 1
            Wend
            j = j + 1
        Wend
        i = i + 1
    Wend

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

    ; {
    ;   "imageRecipe": {
    ;     "additionalInstanceConfiguration": {
    ;       "systemsManagerAgent": {
    ;         "uninstallAfterBuild": boolean
    ;       },
    ;       "userDataOverride": "string"
    ;     },
    ;     "arn": "string",
    ;     "blockDeviceMappings": [
    ;       {
    ;         "deviceName": "string",
    ;         "ebs": {
    ;           "deleteOnTermination": boolean,
    ;           "encrypted": boolean,
    ;           "iops": number,
    ;           "kmsKeyId": "string",
    ;           "snapshotId": "string",
    ;           "throughput": number,
    ;           "volumeSize": number,
    ;           "volumeType": "string"
    ;         },
    ;         "noDevice": "string",
    ;         "virtualName": "string"
    ;       }
    ;     ],
    ;     "components": [
    ;       {
    ;         "componentArn": "string",
    ;         "parameters": [
    ;           {
    ;             "name": "string",
    ;             "value": [
    ;               "string"
    ;             ]
    ;           }
    ;         ]
    ;       }
    ;     ],
    ;     "dateCreated": "string",
    ;     "description": "string",
    ;     "name": "string",
    ;     "owner": "string",
    ;     "parentImage": "string",
    ;     "platform": "string",
    ;     "tags": {
    ;       "string": "string"
    ;     },
    ;     "type": "string",
    ;     "version": "string",
    ;     "workingDirectory": "string"
    ;   },
    ;   "requestId": "string"
    ; }


    CkRest::ckDispose(rest)
    CkAuthAws::ckDispose(authAws)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure