Chilkat Online Tools

PutWebhook Swift3 Example

AWS CodePipeline

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

    // URL: https://codepipeline.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = rest.connect("codepipeline.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("tags[0].key", value: "string")
    json.update("tags[0].value", value: "string")
    json.update("webhook.authentication", value: "string")
    json.update("webhook.authenticationConfiguration.AllowedIPRange", value: "string")
    json.update("webhook.authenticationConfiguration.SecretToken", value: "string")
    json.update("webhook.filters[0].jsonPath", value: "string")
    json.update("webhook.filters[0].matchEquals", value: "string")
    json.update("webhook.name", value: "string")
    json.update("webhook.targetAction", value: "string")
    json.update("webhook.targetPipeline", value: "string")

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

    // {
    //   "tags": [
    //     {
    //       "key": "string",
    //       "value": "string"
    //     }
    //   ],
    //   "webhook": {
    //     "authentication": "string",
    //     "authenticationConfiguration": {
    //       "AllowedIPRange": "string",
    //       "SecretToken": "string"
    //     },
    //     "filters": [
    //       {
    //         "jsonPath": "string",
    //         "matchEquals": "string"
    //       }
    //     ],
    //     "name": "string",
    //     "targetAction": "string",
    //     "targetPipeline": "string"
    //   }
    // }

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

    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 jsonPath: String?
    var matchEquals: String?
    var key: String?
    var value: String?

    var Arn: String? = jResp.string(of: "webhook.arn")
    var Authentication: String? = jResp.string(of: "webhook.definition.authentication")
    var AllowedIPRange: String? = jResp.string(of: "webhook.definition.authenticationConfiguration.AllowedIPRange")
    var SecretToken: String? = jResp.string(of: "webhook.definition.authenticationConfiguration.SecretToken")
    var Name: String? = jResp.string(of: "webhook.definition.name")
    var TargetAction: String? = jResp.string(of: "webhook.definition.targetAction")
    var TargetPipeline: String? = jResp.string(of: "webhook.definition.targetPipeline")
    var ErrorCode: String? = jResp.string(of: "webhook.errorCode")
    var ErrorMessage: String? = jResp.string(of: "webhook.errorMessage")
    var LastTriggered: Int = jResp.int(of: "webhook.lastTriggered").intValue
    var v_Url: String? = jResp.string(of: "webhook.url")
    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "webhook.definition.filters").intValue
    while i < count_i {
        jResp.i = i
        jsonPath = jResp.string(of: "webhook.definition.filters[i].jsonPath")
        matchEquals = jResp.string(of: "webhook.definition.filters[i].matchEquals")
        i = i + 1
    }

    i = 0
    count_i = jResp.size(ofArray: "webhook.tags").intValue
    while i < count_i {
        jResp.i = i
        key = jResp.string(of: "webhook.tags[i].key")
        value = jResp.string(of: "webhook.tags[i].value")
        i = i + 1
    }

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

    // {
    //   "webhook": {
    //     "arn": "string",
    //     "definition": {
    //       "authentication": "string",
    //       "authenticationConfiguration": {
    //         "AllowedIPRange": "string",
    //         "SecretToken": "string"
    //       },
    //       "filters": [
    //         {
    //           "jsonPath": "string",
    //           "matchEquals": "string"
    //         }
    //       ],
    //       "name": "string",
    //       "targetAction": "string",
    //       "targetPipeline": "string"
    //     },
    //     "errorCode": "string",
    //     "errorMessage": "string",
    //     "lastTriggered": number,
    //     "tags": [
    //       {
    //         "key": "string",
    //         "value": "string"
    //       }
    //     ],
    //     "url": "string"
    //   }
    // }

}