CreateStack 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 = "appstream"
// SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
rest.setAuthAws(authAws)
// URL: https://appstream2.us-west-2.amazonaws.com/
// Use the same region as specified above.
success = rest.connect("appstream2.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.update("AccessEndpoints[0].EndpointType", value: "string")
json.update("AccessEndpoints[0].VpceId", value: "string")
json.updateInt("ApplicationSettings.Enabled", value: 123)
json.update("ApplicationSettings.SettingsGroup", value: "string")
json.update("Description", value: "string")
json.update("DisplayName", value: "string")
json.update("EmbedHostDomains[0]", value: "string")
json.update("FeedbackURL", value: "string")
json.update("Name", value: "string")
json.update("RedirectURL", value: "string")
json.update("StorageConnectors[0].ConnectorType", value: "string")
json.update("StorageConnectors[0].Domains[0]", value: "string")
json.update("StorageConnectors[0].ResourceIdentifier", value: "string")
json.update("Tags.string", value: "string")
json.update("UserSettings[0].Action", value: "string")
json.update("UserSettings[0].Permission", value: "string")
// The JSON request body created by the above code:
// {
// "AccessEndpoints": [
// {
// "EndpointType": "string",
// "VpceId": "string"
// }
// ],
// "ApplicationSettings": {
// "Enabled": boolean,
// "SettingsGroup": "string"
// },
// "Description": "string",
// "DisplayName": "string",
// "EmbedHostDomains": [
// "string"
// ],
// "FeedbackURL": "string",
// "Name": "string",
// "RedirectURL": "string",
// "StorageConnectors": [
// {
// "ConnectorType": "string",
// "Domains": [
// "string"
// ],
// "ResourceIdentifier": "string"
// }
// ],
// "Tags": {
// "string": "string"
// },
// "UserSettings": [
// {
// "Action": "string",
// "Permission": "string"
// }
// ]
// }
rest.addHeader("Content-Type", value: "application/x-amz-json-1.1")
rest.addHeader("X-Amz-Target", value: "PhotonAdminProxyService.CreateStack")
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 EndpointType: String?
var VpceId: String?
var strVal: String?
var ErrorCode: String?
var ErrorMessage: String?
var ConnectorType: String?
var ResourceIdentifier: String?
var j: Int
var count_j: Int
var Action: String?
var Permission: String?
var Enabled: Int = jResp.int(of: "Stack.ApplicationSettings.Enabled").intValue
var S3BucketName: String? = jResp.string(of: "Stack.ApplicationSettings.S3BucketName")
var SettingsGroup: String? = jResp.string(of: "Stack.ApplicationSettings.SettingsGroup")
var Arn: String? = jResp.string(of: "Stack.Arn")
var CreatedTime: Int = jResp.int(of: "Stack.CreatedTime").intValue
var Description: String? = jResp.string(of: "Stack.Description")
var DisplayName: String? = jResp.string(of: "Stack.DisplayName")
var FeedbackURL: String? = jResp.string(of: "Stack.FeedbackURL")
var Name: String? = jResp.string(of: "Stack.Name")
var RedirectURL: String? = jResp.string(of: "Stack.RedirectURL")
var i: Int = 0
var count_i: Int = jResp.size(ofArray: "Stack.AccessEndpoints").intValue
while i < count_i {
jResp.i = i
EndpointType = jResp.string(of: "Stack.AccessEndpoints[i].EndpointType")
VpceId = jResp.string(of: "Stack.AccessEndpoints[i].VpceId")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "Stack.EmbedHostDomains").intValue
while i < count_i {
jResp.i = i
strVal = jResp.string(of: "Stack.EmbedHostDomains[i]")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "Stack.StackErrors").intValue
while i < count_i {
jResp.i = i
ErrorCode = jResp.string(of: "Stack.StackErrors[i].ErrorCode")
ErrorMessage = jResp.string(of: "Stack.StackErrors[i].ErrorMessage")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "Stack.StorageConnectors").intValue
while i < count_i {
jResp.i = i
ConnectorType = jResp.string(of: "Stack.StorageConnectors[i].ConnectorType")
ResourceIdentifier = jResp.string(of: "Stack.StorageConnectors[i].ResourceIdentifier")
j = 0
count_j = jResp.size(ofArray: "Stack.StorageConnectors[i].Domains").intValue
while j < count_j {
jResp.j = j
strVal = jResp.string(of: "Stack.StorageConnectors[i].Domains[j]")
j = j + 1
}
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "Stack.UserSettings").intValue
while i < count_i {
jResp.i = i
Action = jResp.string(of: "Stack.UserSettings[i].Action")
Permission = jResp.string(of: "Stack.UserSettings[i].Permission")
i = i + 1
}
// A sample JSON response body parsed by the above code:
// {
// "Stack": {
// "AccessEndpoints": [
// {
// "EndpointType": "string",
// "VpceId": "string"
// }
// ],
// "ApplicationSettings": {
// "Enabled": boolean,
// "S3BucketName": "string",
// "SettingsGroup": "string"
// },
// "Arn": "string",
// "CreatedTime": number,
// "Description": "string",
// "DisplayName": "string",
// "EmbedHostDomains": [
// "string"
// ],
// "FeedbackURL": "string",
// "Name": "string",
// "RedirectURL": "string",
// "StackErrors": [
// {
// "ErrorCode": "string",
// "ErrorMessage": "string"
// }
// ],
// "StorageConnectors": [
// {
// "ConnectorType": "string",
// "Domains": [
// "string"
// ],
// "ResourceIdentifier": "string"
// }
// ],
// "UserSettings": [
// {
// "Action": "string",
// "Permission": "string"
// }
// ]
// }
// }
}