Sample code for 30+ languages & platforms
PureBasic

Etsy: Get the Inventory for a Listing

See more Etsy Examples

Gets the inventory for a listing.

Chilkat PureBasic Downloads

PureBasic
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 GET \
    ;   https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING

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

    success = CkHttp::ckQuickGetSb(http,"https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING",sbResponseBody)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    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 = CkHttp::ckLastStatus(http)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttp::ckLastHeader(http)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    ; 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

    product_id.i
    j.i
    count_j.i
    offering_id.i
    priceAmount.i
    priceDivisor.i
    priceCurrency_code.s
    priceCurrency_formatted_short.s
    priceCurrency_formatted_long.s
    priceCurrency_formatted_raw.s
    quantity.i

    count.i = CkJsonObject::ckIntOf(jResp,"count")
    paramsListing_id.s = CkJsonObject::ckStringOf(jResp,"params.listing_id")
    paramsWrite_missing_inventory.i = CkJsonObject::ckBoolOf(jResp,"params.write_missing_inventory")
    v_type.s = CkJsonObject::ckStringOf(jResp,"type")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"results.products")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        product_id = CkJsonObject::ckIntOf(jResp,"results.products[i].product_id")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jResp,"results.products[i].property_values")
        While j < count_j
            CkJsonObject::setCkJ(jResp, j)
            j = j + 1
        Wend
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jResp,"results.products[i].offerings")
        While j < count_j
            CkJsonObject::setCkJ(jResp, j)
            offering_id = CkJsonObject::ckIntOf(jResp,"results.products[i].offerings[j].offering_id")
            priceAmount = CkJsonObject::ckIntOf(jResp,"results.products[i].offerings[j].price.amount")
            priceDivisor = CkJsonObject::ckIntOf(jResp,"results.products[i].offerings[j].price.divisor")
            priceCurrency_code = CkJsonObject::ckStringOf(jResp,"results.products[i].offerings[j].price.currency_code")
            priceCurrency_formatted_short = CkJsonObject::ckStringOf(jResp,"results.products[i].offerings[j].price.currency_formatted_short")
            priceCurrency_formatted_long = CkJsonObject::ckStringOf(jResp,"results.products[i].offerings[j].price.currency_formatted_long")
            priceCurrency_formatted_raw = CkJsonObject::ckStringOf(jResp,"results.products[i].offerings[j].price.currency_formatted_raw")
            quantity = CkJsonObject::ckIntOf(jResp,"results.products[i].offerings[j].quantity")
            j = j + 1
        Wend
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure