Sample code for 30+ languages & platforms
PowerBuilder

Etsy: Get the Inventory for a Listing

See more Etsy Examples

Gets the inventory for a listing.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_SbResponseBody
oleobject loo_JResp
long li_RespStatusCode
long li_Product_id
long j
long li_Count_j
long li_Offering_id
long li_PriceAmount
long li_PriceDivisor
string ls_PriceCurrency_code
string ls_PriceCurrency_formatted_short
string ls_PriceCurrency_formatted_long
string ls_PriceCurrency_formatted_raw
long li_Quantity
long li_Count
string ls_ParamsListing_id
long li_ParamsWrite_missing_inventory
string ls_V_type
long i
long li_Count_i

li_Success = 0

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Implements the following CURL command:

//  curl -X GET \
//    https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Http.QuickGetSb("https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING",loo_SbResponseBody)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponseBody
    return
end if

loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Http.LastStatus
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Http.LastHeader
    Write-Debug "Failed."
    destroy loo_Http
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

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

li_Count = loo_JResp.IntOf("count")
ls_ParamsListing_id = loo_JResp.StringOf("params.listing_id")
li_ParamsWrite_missing_inventory = loo_JResp.BoolOf("params.write_missing_inventory")
ls_V_type = loo_JResp.StringOf("type")
i = 0
li_Count_i = loo_JResp.SizeOfArray("results.products")
do while i < li_Count_i
    loo_JResp.I = i
    li_Product_id = loo_JResp.IntOf("results.products[i].product_id")
    j = 0
    li_Count_j = loo_JResp.SizeOfArray("results.products[i].property_values")
    do while j < li_Count_j
        loo_JResp.J = j
        j = j + 1
    loop
    j = 0
    li_Count_j = loo_JResp.SizeOfArray("results.products[i].offerings")
    do while j < li_Count_j
        loo_JResp.J = j
        li_Offering_id = loo_JResp.IntOf("results.products[i].offerings[j].offering_id")
        li_PriceAmount = loo_JResp.IntOf("results.products[i].offerings[j].price.amount")
        li_PriceDivisor = loo_JResp.IntOf("results.products[i].offerings[j].price.divisor")
        ls_PriceCurrency_code = loo_JResp.StringOf("results.products[i].offerings[j].price.currency_code")
        ls_PriceCurrency_formatted_short = loo_JResp.StringOf("results.products[i].offerings[j].price.currency_formatted_short")
        ls_PriceCurrency_formatted_long = loo_JResp.StringOf("results.products[i].offerings[j].price.currency_formatted_long")
        ls_PriceCurrency_formatted_raw = loo_JResp.StringOf("results.products[i].offerings[j].price.currency_formatted_raw")
        li_Quantity = loo_JResp.IntOf("results.products[i].offerings[j].quantity")
        j = j + 1
    loop
    i = i + 1
loop


destroy loo_Http
destroy loo_SbResponseBody
destroy loo_JResp