Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Faire - Get All Products
See more Faire Examples
Retrieves a list of products, ordered ascending by updated_at. By default, it only returns non-deleted products.Chilkat Pascal (Lazarus/Delphi) Downloads
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.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
queryParams: TJsonObject;
resp: THttpResponse;
sbResponseBody: TStringBuilder;
jResp: TJsonObject;
respStatusCode: Integer;
id: string;
brand_id: string;
short_description: string;
description: string;
wholesale_price_cents: Integer;
retail_price_cents: Integer;
sale_state: string;
active: Boolean;
deleted: Boolean;
name: string;
unit_multiplier: Integer;
taxonomy_typeId: string;
taxonomy_typeName: string;
created_at: string;
updated_at: string;
j: Integer;
count_j: Integer;
page: Integer;
limit: Integer;
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 -H "X-FAIRE-ACCESS-TOKEN: <access_token>" -d "limit=50" -d "page=1" https://www.faire.com/api/v1/products
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
queryParams := TJsonObject.Create;
queryParams.UpdateInt('limit',50);
queryParams.UpdateInt('page',1);
http.SetRequestHeader('X-FAIRE-ACCESS-TOKEN','<access_token>');
resp := THttpResponse.Create;
success := http.HttpParams('GET','https://www.faire.com/api/v1/products',queryParams,resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
sbResponseBody := TStringBuilder.Create;
resp.GetBodySb(sbResponseBody);
jResp := TJsonObject.Create;
jResp.LoadSb(sbResponseBody);
jResp.EmitCompact := False;
WriteLn('Response Body:');
WriteLn(jResp.Emit());
respStatusCode := resp.StatusCode;
WriteLn('Response Status Code = ' + respStatusCode);
if (respStatusCode >= 400) then
begin
WriteLn('Response Header:');
WriteLn(resp.Header);
WriteLn('Failed.');
Exit;
end;
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "page": 1,
// "limit": 50,
// "products": [
// {
// "id": "p_123",
// "brand_id": "b_abc",
// "short_description": "Our candles smell fantastic. Want to know how good? Read our description!",
// "description": "Glad you decided to read our description! We have significantly more characters to describe to you just how good our candles smell.",
// "wholesale_price_cents": 500,
// "retail_price_cents": 1000,
// "sale_state": "FOR_SALE",
// "active": true,
// "deleted": false,
// "name": "Faire's fantastic candle",
// "unit_multiplier": 8,
// "taxonomy_type": {
// "id": "tt_23nl3bzl00",
// "name": "Votive Candle"
// },
// "options": [
// ],
// "created_at": "20190314T000915.000Z",
// "updated_at": "20190315T000915.000Z"
// }
// ]
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
page := jResp.IntOf('page');
limit := jResp.IntOf('limit');
i := 0;
count_i := jResp.SizeOfArray('products');
while i < count_i do
begin
jResp.I := i;
id := jResp.StringOf('products[i].id');
brand_id := jResp.StringOf('products[i].brand_id');
short_description := jResp.StringOf('products[i].short_description');
description := jResp.StringOf('products[i].description');
wholesale_price_cents := jResp.IntOf('products[i].wholesale_price_cents');
retail_price_cents := jResp.IntOf('products[i].retail_price_cents');
sale_state := jResp.StringOf('products[i].sale_state');
active := jResp.BoolOf('products[i].active');
deleted := jResp.BoolOf('products[i].deleted');
name := jResp.StringOf('products[i].name');
unit_multiplier := jResp.IntOf('products[i].unit_multiplier');
taxonomy_typeId := jResp.StringOf('products[i].taxonomy_type.id');
taxonomy_typeName := jResp.StringOf('products[i].taxonomy_type.name');
created_at := jResp.StringOf('products[i].created_at');
updated_at := jResp.StringOf('products[i].updated_at');
j := 0;
count_j := jResp.SizeOfArray('products[i].options');
while j < count_j do
begin
jResp.J := j;
j := j + 1;
end;
i := i + 1;
end;
http.Free;
queryParams.Free;
resp.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.