Sample code for 30+ languages & platforms
Swift

Shopify Receive Count of All Products in a Collection

See more Shopify Examples

Get a count of all products of a given collection

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let rest = CkoRest()!

    rest.setAuthBasic(username: "SHOPIFY_PRIVATE_API_KEY", password: "SHOPIFY_PRIVATE_API_KEY")

    success = rest.connect(hostname: "chilkat.myshopify.com", port: 443, tls: true, autoReconnect: true)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    let sbJson = CkoStringBuilder()!
    success = rest.fullRequestNoBodySb(httpVerb: "GET", uriPath: "/admin/products/count.json?collection_id=841564295 ", sb: sbJson)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    if rest.responseStatusCode.intValue != 200 {
        print("Received error response code: \(rest.responseStatusCode.intValue)")
        print("Response body:")
        print("\(sbJson.getAsString()!)")
        return
    }

    let json = CkoJsonObject()!
    json.loadSb(sb: sbJson)

    // The following code parses the JSON response.
    // A sample JSON response is shown below the sample code.
    var count: Int

    count = json.int(of: "count").intValue

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

    // {
    //   "count": 1
    // }

    print("Example Completed.")

}