Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Etsy: Get the Inventory for a Listing

See more Etsy Examples

Gets the inventory for a listing.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.StringBuilder,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  respStatusCode: Integer;
  product_id: Integer;
  j: Integer;
  count_j: Integer;
  offering_id: Integer;
  priceAmount: Integer;
  priceDivisor: Integer;
  priceCurrency_code: string;
  priceCurrency_formatted_short: string;
  priceCurrency_formatted_long: string;
  priceCurrency_formatted_raw: string;
  quantity: Integer;
  count: Integer;
  paramsListing_id: string;
  paramsWrite_missing_inventory: Boolean;
  v_type: string;
  i: Integer;
  count_i: Integer;

begin
  success := False;

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

  http := THttp.Create;

  //  Implements the following CURL command:

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

  sbResponseBody := TStringBuilder.Create;
  success := http.QuickGetSb('https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING',sbResponseBody);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(jResp.Emit());

  respStatusCode := http.LastStatus;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(http.LastHeader);
      WriteLn('Failed.');
      Exit;
    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

  count := jResp.IntOf('count');
  paramsListing_id := jResp.StringOf('params.listing_id');
  paramsWrite_missing_inventory := jResp.BoolOf('params.write_missing_inventory');
  v_type := jResp.StringOf('type');
  i := 0;
  count_i := jResp.SizeOfArray('results.products');
  while i < count_i do
    begin
      jResp.I := 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 do
        begin
          jResp.J := j;
          j := j + 1;
        end;

      j := 0;
      count_j := jResp.SizeOfArray('results.products[i].offerings');
      while j < count_j do
        begin
          jResp.J := 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;
        end;

      i := i + 1;
    end;



  http.Free;
  sbResponseBody.Free;
  jResp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.