Chilkat Online Tools

GetPhoneNumber Swift Example

Amazon Chime

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

    // URL: https://chime.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = rest.Connect("chime.us-west-2.amazonaws.com", port: 443, tls: true, autoReconnect: true)
    if success != true {
        print("ConnectFailReason: \(rest.ConnectFailReason.intValue)")
        print("\(rest.LastErrorText)")
        return
    }

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

    let sbResponseBody = CkoStringBuilder()
    success = rest.FullRequestNoBodySb("GET", uriPath: "/phone-numbers/{phoneNumberId}", sb: 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 AssociatedTimestamp: Int
    var Name: String?
    var Value: String?

    var CallingName: String? = jResp.StringOf("PhoneNumber.CallingName")
    var CallingNameStatus: String? = jResp.StringOf("PhoneNumber.CallingNameStatus")
    var InboundCall: Int = jResp.IntOf("PhoneNumber.Capabilities.InboundCall").intValue
    var InboundMMS: Int = jResp.IntOf("PhoneNumber.Capabilities.InboundMMS").intValue
    var InboundSMS: Int = jResp.IntOf("PhoneNumber.Capabilities.InboundSMS").intValue
    var OutboundCall: Int = jResp.IntOf("PhoneNumber.Capabilities.OutboundCall").intValue
    var OutboundMMS: Int = jResp.IntOf("PhoneNumber.Capabilities.OutboundMMS").intValue
    var OutboundSMS: Int = jResp.IntOf("PhoneNumber.Capabilities.OutboundSMS").intValue
    var Country: String? = jResp.StringOf("PhoneNumber.Country")
    var CreatedTimestamp: Int = jResp.IntOf("PhoneNumber.CreatedTimestamp").intValue
    var DeletionTimestamp: Int = jResp.IntOf("PhoneNumber.DeletionTimestamp").intValue
    var E164PhoneNumber: String? = jResp.StringOf("PhoneNumber.E164PhoneNumber")
    var PhoneNumberId: String? = jResp.StringOf("PhoneNumber.PhoneNumberId")
    var ProductType: String? = jResp.StringOf("PhoneNumber.ProductType")
    var Status: String? = jResp.StringOf("PhoneNumber.Status")
    var v_Type: String? = jResp.StringOf("PhoneNumber.Type")
    var UpdatedTimestamp: Int = jResp.IntOf("PhoneNumber.UpdatedTimestamp").intValue
    var i: Int = 0
    var count_i: Int = jResp.SizeOfArray("PhoneNumber.Associations").intValue
    while i < count_i {
        jResp.I = i
        AssociatedTimestamp = jResp.IntOf("PhoneNumber.Associations[i].AssociatedTimestamp").intValue
        Name = jResp.StringOf("PhoneNumber.Associations[i].Name")
        Value = jResp.StringOf("PhoneNumber.Associations[i].Value")
        i = i + 1
    }

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

    // {
    //   "PhoneNumber": {
    //     "Associations": [
    //       {
    //         "AssociatedTimestamp": number,
    //         "Name": "string",
    //         "Value": "string"
    //       }
    //     ],
    //     "CallingName": "string",
    //     "CallingNameStatus": "string",
    //     "Capabilities": {
    //       "InboundCall": boolean,
    //       "InboundMMS": boolean,
    //       "InboundSMS": boolean,
    //       "OutboundCall": boolean,
    //       "OutboundMMS": boolean,
    //       "OutboundSMS": boolean
    //     },
    //     "Country": "string",
    //     "CreatedTimestamp": number,
    //     "DeletionTimestamp": number,
    //     "E164PhoneNumber": "string",
    //     "PhoneNumberId": "string",
    //     "ProductType": "string",
    //     "Status": "string",
    //     "Type": "string",
    //     "UpdatedTimestamp": number
    //   }
    // }

}