Sample code for 30+ languages & platforms
Android™

Faire - Update Inventory Levels

See more Faire Examples

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

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

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

    CkHttp http = new CkHttp();

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

    CkJsonObject json = new CkJsonObject();
    json.UpdateString("inventories[0].sku","vanilla-candle");
    json.UpdateInt("inventories[0].current_quantity",24);
    json.UpdateBool("inventories[0].discontinued",false);
    json.UpdateNull("inventories[0].backordered_until");
    json.UpdateString("inventories[1].sku","cinnamon-candle");
    json.UpdateInt("inventories[1].current_quantity",0);
    json.UpdateBool("inventories[1].discontinued",false);
    json.UpdateString("inventories[1].backordered_until","20190314T000915.000Z");
    json.UpdateString("inventories[2].sku","fall-candle");
    json.UpdateInt("inventories[2].current_quantity",0);
    json.UpdateBool("inventories[2].discontinued",true);
    json.UpdateNull("inventories[2].backordered_until");
    json.UpdateString("inventories[3].sku","fall-candle");
    json.UpdateNull("inventories[3].current_quantity");
    json.UpdateBool("inventories[3].discontinued",false);
    json.UpdateNull("inventories[3].backordered_until");

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

    CkStringBuilder sbRequestBody = new CkStringBuilder();
    json.EmitSb(sbRequestBody);

    CkHttpResponse resp = new CkHttpResponse();
    success = http.HttpSb("PATCH","https://www.faire.com/api/v1/products/options/inventory-levels",sbRequestBody,"utf-8","application/json",resp);
    if (success == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    resp.GetBodySb(sbResponseBody);

    CkJsonObject jResp = new CkJsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    Log.i(TAG, "Response Body:");
    Log.i(TAG, jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));
    if (respStatusCode >= 400) {
        Log.i(TAG, "Response Header:");
        Log.i(TAG, resp.header());
        Log.i(TAG, "Failed.");
        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

    String id;
    String product_id;
    boolean active;
    boolean deleted;
    String name;
    String sku;
    int available_quantity;
    String created_at;
    String updated_at;
    int retail_price_cents;
    int wholesale_price_cents;
    String backordered_until;
    int j;
    int count_j;
    String value;

    int i = 0;
    int count_i = jResp.SizeOfArray("options");
    while (i < count_i) {
        jResp.put_I(i);
        id = jResp.stringOf("options[i].id");
        product_id = jResp.stringOf("options[i].product_id");
        active = jResp.BoolOf("options[i].active");
        deleted = jResp.BoolOf("options[i].deleted");
        name = jResp.stringOf("options[i].name");
        sku = jResp.stringOf("options[i].sku");
        available_quantity = jResp.IntOf("options[i].available_quantity");
        created_at = jResp.stringOf("options[i].created_at");
        updated_at = jResp.stringOf("options[i].updated_at");
        retail_price_cents = jResp.IntOf("options[i].retail_price_cents");
        wholesale_price_cents = jResp.IntOf("options[i].wholesale_price_cents");
        backordered_until = jResp.stringOf("options[i].backordered_until");
        j = 0;
        count_j = jResp.SizeOfArray("options[i].variations");
        while (j < count_j) {
            jResp.put_J(j);
            name = jResp.stringOf("options[i].variations[j].name");
            value = jResp.stringOf("options[i].variations[j].value");
            j = j + 1;
            }

        i = i + 1;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}