Chilkat Online Tools

SubmitJob Swift3 Example

AWS Batch

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 = "batch"
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    rest.setAuthAws(authAws)

    // URL: https://batch.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = rest.connect("batch.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("arrayProperties.size", value: 123)
    json.update("containerOverrides.command[0]", value: "string")
    json.update("containerOverrides.environment[0].name", value: "string")
    json.update("containerOverrides.environment[0].value", value: "string")
    json.update("containerOverrides.instanceType", value: "string")
    json.updateInt("containerOverrides.memory", value: 123)
    json.update("containerOverrides.resourceRequirements[0].type", value: "string")
    json.update("containerOverrides.resourceRequirements[0].value", value: "string")
    json.updateInt("containerOverrides.vcpus", value: 123)
    json.update("dependsOn[0].jobId", value: "string")
    json.update("dependsOn[0].type", value: "string")
    json.update("jobDefinition", value: "string")
    json.update("jobName", value: "string")
    json.update("jobQueue", value: "string")
    json.update("nodeOverrides.nodePropertyOverrides[0].containerOverrides.command[0]", value: "string")
    json.update("nodeOverrides.nodePropertyOverrides[0].containerOverrides.environment[0].name", value: "string")
    json.update("nodeOverrides.nodePropertyOverrides[0].containerOverrides.environment[0].value", value: "string")
    json.update("nodeOverrides.nodePropertyOverrides[0].containerOverrides.instanceType", value: "string")
    json.updateInt("nodeOverrides.nodePropertyOverrides[0].containerOverrides.memory", value: 123)
    json.update("nodeOverrides.nodePropertyOverrides[0].containerOverrides.resourceRequirements[0].type", value: "string")
    json.update("nodeOverrides.nodePropertyOverrides[0].containerOverrides.resourceRequirements[0].value", value: "string")
    json.updateInt("nodeOverrides.nodePropertyOverrides[0].containerOverrides.vcpus", value: 123)
    json.update("nodeOverrides.nodePropertyOverrides[0].targetNodes", value: "string")
    json.updateInt("nodeOverrides.numNodes", value: 123)
    json.update("parameters.string", value: "string")
    json.updateInt("propagateTags", value: 123)
    json.updateInt("retryStrategy.attempts", value: 123)
    json.update("retryStrategy.evaluateOnExit[0].action", value: "string")
    json.update("retryStrategy.evaluateOnExit[0].onExitCode", value: "string")
    json.update("retryStrategy.evaluateOnExit[0].onReason", value: "string")
    json.update("retryStrategy.evaluateOnExit[0].onStatusReason", value: "string")
    json.updateInt("schedulingPriorityOverride", value: 123)
    json.update("shareIdentifier", value: "string")
    json.update("tags.string", value: "string")
    json.updateInt("timeout.attemptDurationSeconds", value: 123)

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

    // {
    //   "arrayProperties": {
    //     "size": number
    //   },
    //   "containerOverrides": {
    //     "command": [
    //       "string"
    //     ],
    //     "environment": [
    //       {
    //         "name": "string",
    //         "value": "string"
    //       }
    //     ],
    //     "instanceType": "string",
    //     "memory": number,
    //     "resourceRequirements": [
    //       {
    //         "type": "string",
    //         "value": "string"
    //       }
    //     ],
    //     "vcpus": number
    //   },
    //   "dependsOn": [
    //     {
    //       "jobId": "string",
    //       "type": "string"
    //     }
    //   ],
    //   "jobDefinition": "string",
    //   "jobName": "string",
    //   "jobQueue": "string",
    //   "nodeOverrides": {
    //     "nodePropertyOverrides": [
    //       {
    //         "containerOverrides": {
    //           "command": [
    //             "string"
    //           ],
    //           "environment": [
    //             {
    //               "name": "string",
    //               "value": "string"
    //             }
    //           ],
    //           "instanceType": "string",
    //           "memory": number,
    //           "resourceRequirements": [
    //             {
    //               "type": "string",
    //               "value": "string"
    //             }
    //           ],
    //           "vcpus": number
    //         },
    //         "targetNodes": "string"
    //       }
    //     ],
    //     "numNodes": number
    //   },
    //   "parameters": {
    //     "string": "string"
    //   },
    //   "propagateTags": boolean,
    //   "retryStrategy": {
    //     "attempts": number,
    //     "evaluateOnExit": [
    //       {
    //         "action": "string",
    //         "onExitCode": "string",
    //         "onReason": "string",
    //         "onStatusReason": "string"
    //       }
    //     ]
    //   },
    //   "schedulingPriorityOverride": number,
    //   "shareIdentifier": "string",
    //   "tags": {
    //     "string": "string"
    //   },
    //   "timeout": {
    //     "attemptDurationSeconds": number
    //   }
    // }

    rest.addHeader("Content-Type", value: "application/x-amz-json-1.1")
    rest.addHeader("X-Amz-Target", value: "SubmitJob")

    let sbRequestBody = CkoStringBuilder()!
    json.emitSb(sbRequestBody)
    let sbResponseBody = CkoStringBuilder()!
    success = rest.fullRequestSb("POST", uriPath: "/v1/submitjob", 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 jobArn: String? = jResp.string(of: "jobArn")
    var jobId: String? = jResp.string(of: "jobId")
    var jobName: String? = jResp.string(of: "jobName")

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

    // {
    //   "jobArn": "string",
    //   "jobId": "string",
    //   "jobName": "string"
    // }

}