Sample code for 30+ languages & platforms
PureBasic

Faire - Update Inventory Levels

See more Faire Examples

Update the inventory levels for multiple product options in one request.

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 PATCH 
    ;     -H "Content-Type: application/json" 
    ;     -H "X-FAIRE-ACCESS-TOKEN: <access_token>"
    ;     -d '{
    ;   "inventories": [
    ;     {
    ;       "sku": "vanilla-candle",
    ;       "current_quantity": 24,
    ;       "discontinued": false,
    ;       "backordered_until": null
    ;     },
    ;     {
    ;       "sku": "cinnamon-candle",
    ;       "current_quantity": 0,
    ;       "discontinued": false,
    ;       "backordered_until": "20190314T000915.000Z"
    ;     },
    ;     {
    ;       "sku": "fall-candle",
    ;       "current_quantity": 0,
    ;       "discontinued": true,
    ;       "backordered_until": null
    ;     },
    ;     {
    ;       "sku": "fall-candle",
    ;       "current_quantity": null,
    ;       "discontinued": false,
    ;       "backordered_until": null
    ;     }
    ;   ]
    ; }' https://www.faire.com/api/v1/products/options/inventory-levels

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

    ; Use this online tool to generate code from sample JSON:
    ; Generate Code to Create JSON

    ; The following JSON is sent in the request body.

    ; {
    ;   "inventories": [
    ;     {
    ;       "sku": "vanilla-candle",
    ;       "current_quantity": 24,
    ;       "discontinued": false,
    ;       "backordered_until": null
    ;     },
    ;     {
    ;       "sku": "cinnamon-candle",
    ;       "current_quantity": 0,
    ;       "discontinued": false,
    ;       "backordered_until": "20190314T000915.000Z"
    ;     },
    ;     {
    ;       "sku": "fall-candle",
    ;       "current_quantity": 0,
    ;       "discontinued": true,
    ;       "backordered_until": null
    ;     },
    ;     {
    ;       "sku": "fall-candle",
    ;       "current_quantity": null,
    ;       "discontinued": false,
    ;       "backordered_until": null
    ;     }
    ;   ]
    ; }

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

    CkJsonObject::ckUpdateString(json,"inventories[0].sku","vanilla-candle")
    CkJsonObject::ckUpdateInt(json,"inventories[0].current_quantity",24)
    CkJsonObject::ckUpdateBool(json,"inventories[0].discontinued",0)
    CkJsonObject::ckUpdateNull(json,"inventories[0].backordered_until")
    CkJsonObject::ckUpdateString(json,"inventories[1].sku","cinnamon-candle")
    CkJsonObject::ckUpdateInt(json,"inventories[1].current_quantity",0)
    CkJsonObject::ckUpdateBool(json,"inventories[1].discontinued",0)
    CkJsonObject::ckUpdateString(json,"inventories[1].backordered_until","20190314T000915.000Z")
    CkJsonObject::ckUpdateString(json,"inventories[2].sku","fall-candle")
    CkJsonObject::ckUpdateInt(json,"inventories[2].current_quantity",0)
    CkJsonObject::ckUpdateBool(json,"inventories[2].discontinued",1)
    CkJsonObject::ckUpdateNull(json,"inventories[2].backordered_until")
    CkJsonObject::ckUpdateString(json,"inventories[3].sku","fall-candle")
    CkJsonObject::ckUpdateNull(json,"inventories[3].current_quantity")
    CkJsonObject::ckUpdateBool(json,"inventories[3].discontinued",0)
    CkJsonObject::ckUpdateNull(json,"inventories[3].backordered_until")

    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")
    CkHttp::ckSetRequestHeader(http,"X-FAIRE-ACCESS-TOKEN","<access_token>")

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

    CkJsonObject::ckEmitSb(json,sbRequestBody)

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

    success = CkHttp::ckHttpSb(http,"PATCH","https://www.faire.com/api/v1/products/options/inventory-levels",sbRequestBody,"utf-8","application/json",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        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(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkHttpResponse::ckDispose(resp)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

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

    ; {
    ;   "options": [
    ;     {
    ;       "id": "po_012",
    ;       "product_id": "p_ghi",
    ;       "active": false,
    ;       "deleted": false,
    ;       "name": "Fall Scent",
    ;       "sku": "fall-candle",
    ;       "available_quantity": 0,
    ;       "created_at": "20190313T000915.000Z",
    ;       "updated_at": "20190315T000915.000Z",
    ;       "variations": [
    ;         {
    ;           "name": "Scent",
    ;           "value": "Fall"
    ;         }
    ;       ],
    ;       "retail_price_cents": 599,
    ;       "wholesale_price_cents": 300
    ;     },
    ;     {
    ;       "id": "po_789",
    ;       "product_id": "p_def",
    ;       "active": false,
    ;       "deleted": false,
    ;       "name": "Cinnamon Scent",
    ;       "sku": "cinnamon-candle",
    ;       "available_quantity": 0,
    ;       "created_at": "20190312T000915.000Z",
    ;       "updated_at": "20190315T000915.000Z",
    ;       "backordered_until": "20190314T000915.000Z",
    ;       "variations": [
    ;         {
    ;           "name": "Scent",
    ;           "value": "Cinnamon"
    ;         }
    ;       ],
    ;       "retail_price_cents": 599,
    ;       "wholesale_price_cents": 300
    ;     },
    ;     {
    ;       "id": "po_456",
    ;       "product_id": "p_abc",
    ;       "active": true,
    ;       "deleted": false,
    ;       "name": "Vanilla Scent",
    ;       "sku": "vanilla-candle",
    ;       "available_quantity": 24,
    ;       "created_at": "20190314T000915.000Z",
    ;       "updated_at": "20190315T000915.000Z",
    ;       "variations": [
    ;         {
    ;           "name": "Scent",
    ;           "value": "Vanilla"
    ;         }
    ;       ],
    ;       "retail_price_cents": 599,
    ;       "wholesale_price_cents": 300
    ;     }
    ;   ]
    ; }

    ; 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
    product_id.s
    active.i
    deleted.i
    name.s
    sku.s
    available_quantity.i
    created_at.s
    updated_at.s
    retail_price_cents.i
    wholesale_price_cents.i
    backordered_until.s
    j.i
    count_j.i
    value.s

    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"options")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        id = CkJsonObject::ckStringOf(jResp,"options[i].id")
        product_id = CkJsonObject::ckStringOf(jResp,"options[i].product_id")
        active = CkJsonObject::ckBoolOf(jResp,"options[i].active")
        deleted = CkJsonObject::ckBoolOf(jResp,"options[i].deleted")
        name = CkJsonObject::ckStringOf(jResp,"options[i].name")
        sku = CkJsonObject::ckStringOf(jResp,"options[i].sku")
        available_quantity = CkJsonObject::ckIntOf(jResp,"options[i].available_quantity")
        created_at = CkJsonObject::ckStringOf(jResp,"options[i].created_at")
        updated_at = CkJsonObject::ckStringOf(jResp,"options[i].updated_at")
        retail_price_cents = CkJsonObject::ckIntOf(jResp,"options[i].retail_price_cents")
        wholesale_price_cents = CkJsonObject::ckIntOf(jResp,"options[i].wholesale_price_cents")
        backordered_until = CkJsonObject::ckStringOf(jResp,"options[i].backordered_until")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jResp,"options[i].variations")
        While j < count_j
            CkJsonObject::setCkJ(jResp, j)
            name = CkJsonObject::ckStringOf(jResp,"options[i].variations[j].name")
            value = CkJsonObject::ckStringOf(jResp,"options[i].variations[j].value")
            j = j + 1
        Wend
        i = i + 1
    Wend


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


    ProcedureReturn
EndProcedure