ReplicateKey Swift3 Example
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 = "kms"
// SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
rest.setAuthAws(authAws)
// URL: https://kms.us-west-2.amazonaws.com/
// Use the same region as specified above.
success = rest.connect("kms.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.updateInt("BypassPolicyLockoutSafetyCheck", value: 123)
json.update("Description", value: "string")
json.update("KeyId", value: "string")
json.update("Policy", value: "string")
json.update("ReplicaRegion", value: "string")
json.update("Tags[0].TagKey", value: "string")
json.update("Tags[0].TagValue", value: "string")
// The JSON request body created by the above code:
// {
// "BypassPolicyLockoutSafetyCheck": boolean,
// "Description": "string",
// "KeyId": "string",
// "Policy": "string",
// "ReplicaRegion": "string",
// "Tags": [
// {
// "TagKey": "string",
// "TagValue": "string"
// }
// ]
// }
rest.addHeader("Content-Type", value: "application/x-amz-json-1.1")
rest.addHeader("X-Amz-Target", value: "TrentService.ReplicateKey")
let sbRequestBody = CkoStringBuilder()!
json.emitSb(sbRequestBody)
let sbResponseBody = CkoStringBuilder()!
success = rest.fullRequestSb("POST", uriPath: "/", 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 strVal: String?
var TagKey: String?
var TagValue: String?
var Arn: String? = jResp.string(of: "ReplicaKeyMetadata.Arn")
var AWSAccountId: String? = jResp.string(of: "ReplicaKeyMetadata.AWSAccountId")
var CloudHsmClusterId: String? = jResp.string(of: "ReplicaKeyMetadata.CloudHsmClusterId")
var CreationDate: Int = jResp.int(of: "ReplicaKeyMetadata.CreationDate").intValue
var CustomerMasterKeySpec: String? = jResp.string(of: "ReplicaKeyMetadata.CustomerMasterKeySpec")
var CustomKeyStoreId: String? = jResp.string(of: "ReplicaKeyMetadata.CustomKeyStoreId")
var DeletionDate: Int = jResp.int(of: "ReplicaKeyMetadata.DeletionDate").intValue
var Description: String? = jResp.string(of: "ReplicaKeyMetadata.Description")
var Enabled: Int = jResp.int(of: "ReplicaKeyMetadata.Enabled").intValue
var ExpirationModel: String? = jResp.string(of: "ReplicaKeyMetadata.ExpirationModel")
var KeyId: String? = jResp.string(of: "ReplicaKeyMetadata.KeyId")
var KeyManager: String? = jResp.string(of: "ReplicaKeyMetadata.KeyManager")
var KeySpec: String? = jResp.string(of: "ReplicaKeyMetadata.KeySpec")
var KeyState: String? = jResp.string(of: "ReplicaKeyMetadata.KeyState")
var KeyUsage: String? = jResp.string(of: "ReplicaKeyMetadata.KeyUsage")
var MultiRegion: Int = jResp.int(of: "ReplicaKeyMetadata.MultiRegion").intValue
var MultiRegionKeyType: String? = jResp.string(of: "ReplicaKeyMetadata.MultiRegionConfiguration.MultiRegionKeyType")
var PrimaryKeyArn: String? = jResp.string(of: "ReplicaKeyMetadata.MultiRegionConfiguration.PrimaryKey.Arn")
var Region: String? = jResp.string(of: "ReplicaKeyMetadata.MultiRegionConfiguration.PrimaryKey.Region")
var Origin: String? = jResp.string(of: "ReplicaKeyMetadata.Origin")
var PendingDeletionWindowInDays: Int = jResp.int(of: "ReplicaKeyMetadata.PendingDeletionWindowInDays").intValue
var ValidTo: Int = jResp.int(of: "ReplicaKeyMetadata.ValidTo").intValue
var ReplicaPolicy: String? = jResp.string(of: "ReplicaPolicy")
var i: Int = 0
var count_i: Int = jResp.size(ofArray: "ReplicaKeyMetadata.EncryptionAlgorithms").intValue
while i < count_i {
jResp.i = i
strVal = jResp.string(of: "ReplicaKeyMetadata.EncryptionAlgorithms[i]")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "ReplicaKeyMetadata.MacAlgorithms").intValue
while i < count_i {
jResp.i = i
strVal = jResp.string(of: "ReplicaKeyMetadata.MacAlgorithms[i]")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "ReplicaKeyMetadata.MultiRegionConfiguration.ReplicaKeys").intValue
while i < count_i {
jResp.i = i
Arn = jResp.string(of: "ReplicaKeyMetadata.MultiRegionConfiguration.ReplicaKeys[i].Arn")
Region = jResp.string(of: "ReplicaKeyMetadata.MultiRegionConfiguration.ReplicaKeys[i].Region")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "ReplicaKeyMetadata.SigningAlgorithms").intValue
while i < count_i {
jResp.i = i
strVal = jResp.string(of: "ReplicaKeyMetadata.SigningAlgorithms[i]")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "ReplicaTags").intValue
while i < count_i {
jResp.i = i
TagKey = jResp.string(of: "ReplicaTags[i].TagKey")
TagValue = jResp.string(of: "ReplicaTags[i].TagValue")
i = i + 1
}
// A sample JSON response body parsed by the above code:
// {
// "ReplicaKeyMetadata": {
// "Arn": "string",
// "AWSAccountId": "string",
// "CloudHsmClusterId": "string",
// "CreationDate": number,
// "CustomerMasterKeySpec": "string",
// "CustomKeyStoreId": "string",
// "DeletionDate": number,
// "Description": "string",
// "Enabled": boolean,
// "EncryptionAlgorithms": [
// "string"
// ],
// "ExpirationModel": "string",
// "KeyId": "string",
// "KeyManager": "string",
// "KeySpec": "string",
// "KeyState": "string",
// "KeyUsage": "string",
// "MacAlgorithms": [
// "string"
// ],
// "MultiRegion": boolean,
// "MultiRegionConfiguration": {
// "MultiRegionKeyType": "string",
// "PrimaryKey": {
// "Arn": "string",
// "Region": "string"
// },
// "ReplicaKeys": [
// {
// "Arn": "string",
// "Region": "string"
// }
// ]
// },
// "Origin": "string",
// "PendingDeletionWindowInDays": number,
// "SigningAlgorithms": [
// "string"
// ],
// "ValidTo": number
// },
// "ReplicaPolicy": "string",
// "ReplicaTags": [
// {
// "TagKey": "string",
// "TagValue": "string"
// }
// ]
// }
}