CreateKey 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("CustomerMasterKeySpec", value: "string")
json.update("CustomKeyStoreId", value: "string")
json.update("Description", value: "string")
json.update("KeySpec", value: "string")
json.update("KeyUsage", value: "string")
json.updateInt("MultiRegion", value: 123)
json.update("Origin", value: "string")
json.update("Policy", 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,
// "CustomerMasterKeySpec": "string",
// "CustomKeyStoreId": "string",
// "Description": "string",
// "KeySpec": "string",
// "KeyUsage": "string",
// "MultiRegion": boolean,
// "Origin": "string",
// "Policy": "string",
// "Tags": [
// {
// "TagKey": "string",
// "TagValue": "string"
// }
// ]
// }
rest.addHeader("Content-Type", value: "application/x-amz-json-1.1")
rest.addHeader("X-Amz-Target", value: "TrentService.CreateKey")
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 Arn: String? = jResp.string(of: "KeyMetadata.Arn")
var AWSAccountId: String? = jResp.string(of: "KeyMetadata.AWSAccountId")
var CloudHsmClusterId: String? = jResp.string(of: "KeyMetadata.CloudHsmClusterId")
var CreationDate: Int = jResp.int(of: "KeyMetadata.CreationDate").intValue
var CustomerMasterKeySpec: String? = jResp.string(of: "KeyMetadata.CustomerMasterKeySpec")
var CustomKeyStoreId: String? = jResp.string(of: "KeyMetadata.CustomKeyStoreId")
var DeletionDate: Int = jResp.int(of: "KeyMetadata.DeletionDate").intValue
var Description: String? = jResp.string(of: "KeyMetadata.Description")
var Enabled: Int = jResp.int(of: "KeyMetadata.Enabled").intValue
var ExpirationModel: String? = jResp.string(of: "KeyMetadata.ExpirationModel")
var KeyId: String? = jResp.string(of: "KeyMetadata.KeyId")
var KeyManager: String? = jResp.string(of: "KeyMetadata.KeyManager")
var KeySpec: String? = jResp.string(of: "KeyMetadata.KeySpec")
var KeyState: String? = jResp.string(of: "KeyMetadata.KeyState")
var KeyUsage: String? = jResp.string(of: "KeyMetadata.KeyUsage")
var MultiRegion: Int = jResp.int(of: "KeyMetadata.MultiRegion").intValue
var MultiRegionKeyType: String? = jResp.string(of: "KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType")
var PrimaryKeyArn: String? = jResp.string(of: "KeyMetadata.MultiRegionConfiguration.PrimaryKey.Arn")
var Region: String? = jResp.string(of: "KeyMetadata.MultiRegionConfiguration.PrimaryKey.Region")
var Origin: String? = jResp.string(of: "KeyMetadata.Origin")
var PendingDeletionWindowInDays: Int = jResp.int(of: "KeyMetadata.PendingDeletionWindowInDays").intValue
var ValidTo: Int = jResp.int(of: "KeyMetadata.ValidTo").intValue
var i: Int = 0
var count_i: Int = jResp.size(ofArray: "KeyMetadata.EncryptionAlgorithms").intValue
while i < count_i {
jResp.i = i
strVal = jResp.string(of: "KeyMetadata.EncryptionAlgorithms[i]")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "KeyMetadata.MacAlgorithms").intValue
while i < count_i {
jResp.i = i
strVal = jResp.string(of: "KeyMetadata.MacAlgorithms[i]")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "KeyMetadata.MultiRegionConfiguration.ReplicaKeys").intValue
while i < count_i {
jResp.i = i
Arn = jResp.string(of: "KeyMetadata.MultiRegionConfiguration.ReplicaKeys[i].Arn")
Region = jResp.string(of: "KeyMetadata.MultiRegionConfiguration.ReplicaKeys[i].Region")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "KeyMetadata.SigningAlgorithms").intValue
while i < count_i {
jResp.i = i
strVal = jResp.string(of: "KeyMetadata.SigningAlgorithms[i]")
i = i + 1
}
// A sample JSON response body parsed by the above code:
// {
// "KeyMetadata": {
// "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
// }
// }
}