Java
Chilkat2 Java
Etsy: Get the Inventory for a Listing
See more Etsy Examples
Gets the inventory for a listing.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
// Implements the following CURL command:
// curl -X GET \
// https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING
StringBuilder sbResponseBody = new StringBuilder();
try {
http.quickGetSb("https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING", sbResponseBody);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject jResp = new JsonObject();
jResp.loadSb(sbResponseBody);
jResp.setEmitCompact(false);
System.out.println("Response Body:");
System.out.println(jResp.emit());
int respStatusCode = http.getLastStatus();
System.out.println("Response Status Code = " + respStatusCode);
if (respStatusCode >= 400) {
System.out.println("Response Header:");
System.out.println(http.getLastHeader());
System.out.println("Failed.");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "count": 1,
// "results": {
// "products": [
// {
// "product_id": 3361120103,
// "property_values": [
// ],
// "offerings": [
// {
// "offering_id": 3579642570,
// "price": {
// "amount": 16000,
// "divisor": 100,
// "currency_code": "USD",
// "currency_formatted_short": "$160.00",
// "currency_formatted_long": "$160.00 USD",
// "currency_formatted_raw": "160.00"
// },
// "quantity": 1
// }
// ]
// }
// ]
// },
// "params": {
// "listing_id": "720138253",
// "write_missing_inventory": false
// },
// "type": "ListingInventory",
// "pagination": {}
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
int product_id = 0;
int j = 0;
int count_j = 0;
int offering_id = 0;
int priceAmount = 0;
int priceDivisor = 0;
String priceCurrency_code = "";
String priceCurrency_formatted_short = "";
String priceCurrency_formatted_long = "";
String priceCurrency_formatted_raw = "";
int quantity = 0;
int count = jResp.intOf("count");
String paramsListing_id = jResp.stringOf("params.listing_id");
boolean paramsWrite_missing_inventory = jResp.boolOf("params.write_missing_inventory");
String v_type = jResp.stringOf("type");
int i = 0;
int count_i = jResp.sizeOfArray("results.products");
while (i < count_i) {
jResp.setI(i);
product_id = jResp.intOf("results.products[i].product_id");
j = 0;
count_j = jResp.sizeOfArray("results.products[i].property_values");
while (j < count_j) {
jResp.setJ(j);
j = j + 1;
}
j = 0;
count_j = jResp.sizeOfArray("results.products[i].offerings");
while (j < count_j) {
jResp.setJ(j);
offering_id = jResp.intOf("results.products[i].offerings[j].offering_id");
priceAmount = jResp.intOf("results.products[i].offerings[j].price.amount");
priceDivisor = jResp.intOf("results.products[i].offerings[j].price.divisor");
priceCurrency_code = jResp.stringOf("results.products[i].offerings[j].price.currency_code");
priceCurrency_formatted_short = jResp.stringOf("results.products[i].offerings[j].price.currency_formatted_short");
priceCurrency_formatted_long = jResp.stringOf("results.products[i].offerings[j].price.currency_formatted_long");
priceCurrency_formatted_raw = jResp.stringOf("results.products[i].offerings[j].price.currency_formatted_raw");
quantity = jResp.intOf("results.products[i].offerings[j].quantity");
j = j + 1;
}
i = i + 1;
}
}
}