Sample code for 30+ languages & platforms
DataFlex

Etsy: Get the Inventory for a Listing

See more Etsy Examples

Gets the inventory for a listing.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Handle hoJResp
    Integer iRespStatusCode
    Integer iProduct_id
    Integer j
    Integer iCount_j
    Integer iOffering_id
    Integer iPriceAmount
    Integer iPriceDivisor
    String sPriceCurrency_code
    String sPriceCurrency_formatted_short
    String sPriceCurrency_formatted_long
    String sPriceCurrency_formatted_raw
    Integer iQuantity
    Integer iCount
    String sParamsListing_id
    Boolean iParamsWrite_missing_inventory
    String sV_type
    Integer i
    Integer iCount_i
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // Implements the following CURL command:

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

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComQuickGetSb Of hoHttp "https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING" vSbResponseBody To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
    Set ComEmitCompact Of hoJResp To False

    Showln "Response Body:"
    Get ComEmit Of hoJResp To sTemp1
    Showln sTemp1

    Get ComLastStatus Of hoHttp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComLastHeader Of hoHttp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Procedure_Return
    End

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

    Get ComIntOf Of hoJResp "count" To iCount
    Get ComStringOf Of hoJResp "params.listing_id" To sParamsListing_id
    Get ComBoolOf Of hoJResp "params.write_missing_inventory" To iParamsWrite_missing_inventory
    Get ComStringOf Of hoJResp "type" To sV_type
    Move 0 To i
    Get ComSizeOfArray Of hoJResp "results.products" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJResp To i
        Get ComIntOf Of hoJResp "results.products[i].product_id" To iProduct_id
        Move 0 To j
        Get ComSizeOfArray Of hoJResp "results.products[i].property_values" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoJResp To j
            Move (j + 1) To j
        Loop

        Move 0 To j
        Get ComSizeOfArray Of hoJResp "results.products[i].offerings" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoJResp To j
            Get ComIntOf Of hoJResp "results.products[i].offerings[j].offering_id" To iOffering_id
            Get ComIntOf Of hoJResp "results.products[i].offerings[j].price.amount" To iPriceAmount
            Get ComIntOf Of hoJResp "results.products[i].offerings[j].price.divisor" To iPriceDivisor
            Get ComStringOf Of hoJResp "results.products[i].offerings[j].price.currency_code" To sPriceCurrency_code
            Get ComStringOf Of hoJResp "results.products[i].offerings[j].price.currency_formatted_short" To sPriceCurrency_formatted_short
            Get ComStringOf Of hoJResp "results.products[i].offerings[j].price.currency_formatted_long" To sPriceCurrency_formatted_long
            Get ComStringOf Of hoJResp "results.products[i].offerings[j].price.currency_formatted_raw" To sPriceCurrency_formatted_raw
            Get ComIntOf Of hoJResp "results.products[i].offerings[j].quantity" To iQuantity
            Move (j + 1) To j
        Loop

        Move (i + 1) To i
    Loop



End_Procedure