Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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

    queryParams.i = CkJsonObject::ckCreate()
    If queryParams.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateInt(queryParams,"limit",50)
    CkJsonObject::ckUpdateInt(queryParams,"page",1)

    CkHttp::ckSetRequestHeader(http,"X-FAIRE-ACCESS-TOKEN","<access_token>")

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpParams(http,"GET","https://www.faire.com/api/v1/products",queryParams,resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(queryParams)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(queryParams)
        CkHttpResponse::ckDispose(resp)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    ; 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

    id.s
    brand_id.s
    short_description.s
    description.s
    wholesale_price_cents.i
    retail_price_cents.i
    sale_state.s
    active.i
    deleted.i
    name.s
    unit_multiplier.i
    taxonomy_typeId.s
    taxonomy_typeName.s
    created_at.s
    updated_at.s
    j.i
    count_j.i

    page.i = CkJsonObject::ckIntOf(jResp,"page")
    limit.i = CkJsonObject::ckIntOf(jResp,"limit")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"products")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        id = CkJsonObject::ckStringOf(jResp,"products[i].id")
        brand_id = CkJsonObject::ckStringOf(jResp,"products[i].brand_id")
        short_description = CkJsonObject::ckStringOf(jResp,"products[i].short_description")
        description = CkJsonObject::ckStringOf(jResp,"products[i].description")
        wholesale_price_cents = CkJsonObject::ckIntOf(jResp,"products[i].wholesale_price_cents")
        retail_price_cents = CkJsonObject::ckIntOf(jResp,"products[i].retail_price_cents")
        sale_state = CkJsonObject::ckStringOf(jResp,"products[i].sale_state")
        active = CkJsonObject::ckBoolOf(jResp,"products[i].active")
        deleted = CkJsonObject::ckBoolOf(jResp,"products[i].deleted")
        name = CkJsonObject::ckStringOf(jResp,"products[i].name")
        unit_multiplier = CkJsonObject::ckIntOf(jResp,"products[i].unit_multiplier")
        taxonomy_typeId = CkJsonObject::ckStringOf(jResp,"products[i].taxonomy_type.id")
        taxonomy_typeName = CkJsonObject::ckStringOf(jResp,"products[i].taxonomy_type.name")
        created_at = CkJsonObject::ckStringOf(jResp,"products[i].created_at")
        updated_at = CkJsonObject::ckStringOf(jResp,"products[i].updated_at")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jResp,"products[i].options")
        While j < count_j
            CkJsonObject::setCkJ(jResp, j)
            j = j + 1
        Wend
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(queryParams)
    CkHttpResponse::ckDispose(resp)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure