Sample code for 30+ languages & platforms
Unicode C

Faire - Update Inventory Levels

See more Faire Examples

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

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkJsonObjectW json;
    HCkStringBuilderW sbRequestBody;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *id;
    const wchar_t *product_id;
    BOOL active;
    BOOL deleted;
    const wchar_t *name;
    const wchar_t *sku;
    int available_quantity;
    const wchar_t *created_at;
    const wchar_t *updated_at;
    int retail_price_cents;
    int wholesale_price_cents;
    const wchar_t *backordered_until;
    int j;
    int count_j;
    const wchar_t *value;
    int i;
    int count_i;

    success = FALSE;

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

    http = CkHttpW_Create();

    // 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 = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"inventories[0].sku",L"vanilla-candle");
    CkJsonObjectW_UpdateInt(json,L"inventories[0].current_quantity",24);
    CkJsonObjectW_UpdateBool(json,L"inventories[0].discontinued",FALSE);
    CkJsonObjectW_UpdateNull(json,L"inventories[0].backordered_until");
    CkJsonObjectW_UpdateString(json,L"inventories[1].sku",L"cinnamon-candle");
    CkJsonObjectW_UpdateInt(json,L"inventories[1].current_quantity",0);
    CkJsonObjectW_UpdateBool(json,L"inventories[1].discontinued",FALSE);
    CkJsonObjectW_UpdateString(json,L"inventories[1].backordered_until",L"20190314T000915.000Z");
    CkJsonObjectW_UpdateString(json,L"inventories[2].sku",L"fall-candle");
    CkJsonObjectW_UpdateInt(json,L"inventories[2].current_quantity",0);
    CkJsonObjectW_UpdateBool(json,L"inventories[2].discontinued",TRUE);
    CkJsonObjectW_UpdateNull(json,L"inventories[2].backordered_until");
    CkJsonObjectW_UpdateString(json,L"inventories[3].sku",L"fall-candle");
    CkJsonObjectW_UpdateNull(json,L"inventories[3].current_quantity");
    CkJsonObjectW_UpdateBool(json,L"inventories[3].discontinued",FALSE);
    CkJsonObjectW_UpdateNull(json,L"inventories[3].backordered_until");

    CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");
    CkHttpW_SetRequestHeader(http,L"X-FAIRE-ACCESS-TOKEN",L"<access_token>");

    sbRequestBody = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(json,sbRequestBody);

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpSb(http,L"PATCH",L"https://www.faire.com/api/v1/products/options/inventory-levels",sbRequestBody,L"utf-8",L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkHttpResponseW_Dispose(resp);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    // 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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"options");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        id = CkJsonObjectW_stringOf(jResp,L"options[i].id");
        product_id = CkJsonObjectW_stringOf(jResp,L"options[i].product_id");
        active = CkJsonObjectW_BoolOf(jResp,L"options[i].active");
        deleted = CkJsonObjectW_BoolOf(jResp,L"options[i].deleted");
        name = CkJsonObjectW_stringOf(jResp,L"options[i].name");
        sku = CkJsonObjectW_stringOf(jResp,L"options[i].sku");
        available_quantity = CkJsonObjectW_IntOf(jResp,L"options[i].available_quantity");
        created_at = CkJsonObjectW_stringOf(jResp,L"options[i].created_at");
        updated_at = CkJsonObjectW_stringOf(jResp,L"options[i].updated_at");
        retail_price_cents = CkJsonObjectW_IntOf(jResp,L"options[i].retail_price_cents");
        wholesale_price_cents = CkJsonObjectW_IntOf(jResp,L"options[i].wholesale_price_cents");
        backordered_until = CkJsonObjectW_stringOf(jResp,L"options[i].backordered_until");
        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(jResp,L"options[i].variations");
        while (j < count_j) {
            CkJsonObjectW_putJ(jResp,j);
            name = CkJsonObjectW_stringOf(jResp,L"options[i].variations[j].name");
            value = CkJsonObjectW_stringOf(jResp,L"options[i].variations[j].value");
            j = j + 1;
        }

        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkHttpResponseW_Dispose(resp);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }