Sample code for 30+ languages & platforms
Swift

Faire - Get All Products

See more Faire Examples

Retrieves a list of products, ordered ascending by updated_at. By default, it only returns non-deleted products.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!

    // Implements the following CURL command:

    // curl -X GET -H "X-FAIRE-ACCESS-TOKEN: <access_token>" -d "limit=50" -d "page=1" https://www.faire.com/api/v1/products

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    let queryParams = CkoJsonObject()!
    queryParams.updateInt(jsonPath: "limit", value: 50)
    queryParams.updateInt(jsonPath: "page", value: 1)

    http.setRequestHeader(name: "X-FAIRE-ACCESS-TOKEN", value: "<access_token>")

    let resp = CkoHttpResponse()!
    success = http.httpParams(verb: "GET", url: "https://www.faire.com/api/v1/products", json: queryParams, response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    let sbResponseBody = CkoStringBuilder()!
    resp.getBodySb(sb: sbResponseBody)

    let jResp = CkoJsonObject()!
    jResp.loadSb(sb: sbResponseBody)
    jResp.emitCompact = false

    print("Response Body:")
    print("\(jResp.emit()!)")

    var respStatusCode: Int = resp.statusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp.header!)")
        print("Failed.")
        return
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "page": 1,
    //   "limit": 50,
    //   "products": [
    //     {
    //       "id": "p_123",
    //       "brand_id": "b_abc",
    //       "short_description": "Our candles smell fantastic. Want to know how good? Read our description!",
    //       "description": "Glad you decided to read our description! We have significantly more characters to describe to you just how good our candles smell.",
    //       "wholesale_price_cents": 500,
    //       "retail_price_cents": 1000,
    //       "sale_state": "FOR_SALE",
    //       "active": true,
    //       "deleted": false,
    //       "name": "Faire's fantastic candle",
    //       "unit_multiplier": 8,
    //       "taxonomy_type": {
    //         "id": "tt_23nl3bzl00",
    //         "name": "Votive Candle"
    //       },
    //       "options": [
    //       ],
    //       "created_at": "20190314T000915.000Z",
    //       "updated_at": "20190315T000915.000Z"
    //     }
    //   ]
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var id: String?
    var brand_id: String?
    var short_description: String?
    var description: String?
    var wholesale_price_cents: Int
    var retail_price_cents: Int
    var sale_state: String?
    var active: Bool
    var deleted: Bool
    var name: String?
    var unit_multiplier: Int
    var taxonomy_typeId: String?
    var taxonomy_typeName: String?
    var created_at: String?
    var updated_at: String?
    var j: Int
    var count_j: Int

    var page: Int = jResp.int(of: "page").intValue
    var limit: Int = jResp.int(of: "limit").intValue
    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "products").intValue
    while i < count_i {
        jResp.i = i
        id = jResp.string(of: "products[i].id")
        brand_id = jResp.string(of: "products[i].brand_id")
        short_description = jResp.string(of: "products[i].short_description")
        description = jResp.string(of: "products[i].description")
        wholesale_price_cents = jResp.int(of: "products[i].wholesale_price_cents").intValue
        retail_price_cents = jResp.int(of: "products[i].retail_price_cents").intValue
        sale_state = jResp.string(of: "products[i].sale_state")
        active = jResp.bool(of: "products[i].active")
        deleted = jResp.bool(of: "products[i].deleted")
        name = jResp.string(of: "products[i].name")
        unit_multiplier = jResp.int(of: "products[i].unit_multiplier").intValue
        taxonomy_typeId = jResp.string(of: "products[i].taxonomy_type.id")
        taxonomy_typeName = jResp.string(of: "products[i].taxonomy_type.name")
        created_at = jResp.string(of: "products[i].created_at")
        updated_at = jResp.string(of: "products[i].updated_at")
        j = 0
        count_j = jResp.size(ofArray: "products[i].options").intValue
        while j < count_j {
            jResp.j = j
            j = j + 1
        }

        i = i + 1
    }


}