Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Lightspeed - Create a Product
See more Lightspeed Examples
Create a new product based on the given parameters.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;
json: TJsonObject;
resp: THttpResponse;
sbResponseBody: TStringBuilder;
jResp: TJsonObject;
respStatusCode: Integer;
productId: Integer;
productCreatedAt: string;
productUpdatedAt: string;
productIsVisible: Boolean;
productVisibility: string;
productHasMatrix: Boolean;
productData01: string;
productData02: string;
productData03: string;
productUrl: string;
productTitle: string;
productFulltitle: string;
productDescription: string;
productContent: string;
productSet: Boolean;
productBrandResourceId: Integer;
productBrandResourceUrl: string;
productBrandResourceLink: string;
productCategoriesResourceId: Boolean;
productCategoriesResourceUrl: string;
productCategoriesResourceLink: string;
productDeliverydateResourceId: Integer;
productDeliverydateResourceUrl: string;
productDeliverydateResourceLink: string;
productImage: Boolean;
productImages: Boolean;
productRelationsResourceId: Boolean;
productRelationsResourceUrl: string;
productRelationsResourceLink: string;
productMetafieldsResourceId: Boolean;
productMetafieldsResourceUrl: string;
productMetafieldsResourceLink: string;
productReviewsResourceId: Boolean;
productReviewsResourceUrl: string;
productReviewsResourceLink: string;
productType: Boolean;
productAttributesResourceId: Boolean;
productAttributesResourceUrl: string;
productAttributesResourceLink: string;
productSupplierResourceId: Integer;
productSupplierResourceUrl: string;
productSupplierResourceLink: string;
productTagsResourceId: Boolean;
productTagsResourceUrl: string;
productTagsResourceLink: string;
productVariantsResourceId: Boolean;
productVariantsResourceUrl: string;
productVariantsResourceLink: string;
productMovementsResourceId: Boolean;
productMovementsResourceUrl: string;
productMovementsResourceLink: string;
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 -u API_KEY:API_SECRET \
// -H "Content-Type: application/json" \
// -X POST \
// -d '{
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }' \
// "https://api.webshopapp.com/en/products.json"
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
http.Login := 'API_KEY';
http.Password := 'API_SECRET';
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }
json := TJsonObject.Create;
json.UpdateString('product.visibility','visible');
json.UpdateString('product.data01','');
json.UpdateString('product.data02','');
json.UpdateString('product.data03','');
json.UpdateString('product.title','Lookin Sharp T-Shirt');
json.UpdateString('product.fulltitle','Lookin Sharp T-Shirt');
json.UpdateString('product.description','Description of the Lookin Sharp T-Shirt');
json.UpdateString('product.content','Long Description of the Lookin Sharp T-Shirt');
json.UpdateInt('product.deliverydate',6488);
json.UpdateInt('product.supplier',78794);
json.UpdateInt('product.brand',1171202);
http.SetRequestHeader('Content-Type','application/json');
// Use the correct cluster for your shop. Here are the choices:
// eu1 https://api.webshopapp.com/en/
// us1 https://api.shoplightspeed.com/en/
resp := THttpResponse.Create;
success := http.HttpJson('POST','https://api.webshopapp.com/en/products.json',json,'application/json',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)
// {
// "product": {
// "id": 20974460,
// "createdAt": "2019-05-28T20:14:26+00:00",
// "updatedAt": "2019-05-28T20:14:26+00:00",
// "isVisible": true,
// "visibility": "visible",
// "hasMatrix": false,
// "data01": "",
// "data02": "",
// "data03": "",
// "url": "lookin-sharp-t-shirt",
// "title": "Lookin' Sharp T-Shirt",
// "fulltitle": "Lookin' Sharp T-Shirt",
// "description": "Description of the Lookin' Sharp T-Shirt",
// "content": "<p>Long Description of the Lookin' Sharp T-Shirt</p>",
// "set": false,
// "brand": {
// "resource": {
// "id": 1171202,
// "url": "brands/1171202",
// "link": "https://api.shoplightspeed.com/us/brands/1171202.json"
// }
// },
// "categories": {
// "resource": {
// "id": false,
// "url": "categories/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/categories/products.json?product=20974460"
// }
// },
// "deliverydate": {
// "resource": {
// "id": 6488,
// "url": "deliverydates/6488",
// "link": "https://api.shoplightspeed.com/us/deliverydates/6488.json"
// }
// },
// "image": false,
// "images": false,
// "relations": {
// "resource": {
// "id": false,
// "url": "products/20974460/relations",
// "link": "https://api.shoplightspeed.com/us/products/20974460/relations.json"
// }
// },
// "metafields": {
// "resource": {
// "id": false,
// "url": "products/20974460/metafields",
// "link": "https://api.shoplightspeed.com/us/products/20974460/metafields.json"
// }
// },
// "reviews": {
// "resource": {
// "id": false,
// "url": "reviews?product=20974460",
// "link": "https://api.shoplightspeed.com/us/reviews.json?product=20974460"
// }
// },
// "type": false,
// "attributes": {
// "resource": {
// "id": false,
// "url": "products/20974460/attributes",
// "link": "https://api.shoplightspeed.com/us/products/20974460/attributes.json"
// }
// },
// "supplier": {
// "resource": {
// "id": 78794,
// "url": "suppliers/78794",
// "link": "https://api.shoplightspeed.com/us/suppliers/78794.json"
// }
// },
// "tags": {
// "resource": {
// "id": false,
// "url": "tags/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/tags/products.json?product=20974460"
// }
// },
// "variants": {
// "resource": {
// "id": false,
// "url": "variants?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants.json?product=20974460"
// }
// },
// "movements": {
// "resource": {
// "id": false,
// "url": "variants/movements?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants/movements.json?product=20974460"
// }
// }
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
productId := jResp.IntOf('product.id');
productCreatedAt := jResp.StringOf('product.createdAt');
productUpdatedAt := jResp.StringOf('product.updatedAt');
productIsVisible := jResp.BoolOf('product.isVisible');
productVisibility := jResp.StringOf('product.visibility');
productHasMatrix := jResp.BoolOf('product.hasMatrix');
productData01 := jResp.StringOf('product.data01');
productData02 := jResp.StringOf('product.data02');
productData03 := jResp.StringOf('product.data03');
productUrl := jResp.StringOf('product.url');
productTitle := jResp.StringOf('product.title');
productFulltitle := jResp.StringOf('product.fulltitle');
productDescription := jResp.StringOf('product.description');
productContent := jResp.StringOf('product.content');
productSet := jResp.BoolOf('product.set');
productBrandResourceId := jResp.IntOf('product.brand.resource.id');
productBrandResourceUrl := jResp.StringOf('product.brand.resource.url');
productBrandResourceLink := jResp.StringOf('product.brand.resource.link');
productCategoriesResourceId := jResp.BoolOf('product.categories.resource.id');
productCategoriesResourceUrl := jResp.StringOf('product.categories.resource.url');
productCategoriesResourceLink := jResp.StringOf('product.categories.resource.link');
productDeliverydateResourceId := jResp.IntOf('product.deliverydate.resource.id');
productDeliverydateResourceUrl := jResp.StringOf('product.deliverydate.resource.url');
productDeliverydateResourceLink := jResp.StringOf('product.deliverydate.resource.link');
productImage := jResp.BoolOf('product.image');
productImages := jResp.BoolOf('product.images');
productRelationsResourceId := jResp.BoolOf('product.relations.resource.id');
productRelationsResourceUrl := jResp.StringOf('product.relations.resource.url');
productRelationsResourceLink := jResp.StringOf('product.relations.resource.link');
productMetafieldsResourceId := jResp.BoolOf('product.metafields.resource.id');
productMetafieldsResourceUrl := jResp.StringOf('product.metafields.resource.url');
productMetafieldsResourceLink := jResp.StringOf('product.metafields.resource.link');
productReviewsResourceId := jResp.BoolOf('product.reviews.resource.id');
productReviewsResourceUrl := jResp.StringOf('product.reviews.resource.url');
productReviewsResourceLink := jResp.StringOf('product.reviews.resource.link');
productType := jResp.BoolOf('product.type');
productAttributesResourceId := jResp.BoolOf('product.attributes.resource.id');
productAttributesResourceUrl := jResp.StringOf('product.attributes.resource.url');
productAttributesResourceLink := jResp.StringOf('product.attributes.resource.link');
productSupplierResourceId := jResp.IntOf('product.supplier.resource.id');
productSupplierResourceUrl := jResp.StringOf('product.supplier.resource.url');
productSupplierResourceLink := jResp.StringOf('product.supplier.resource.link');
productTagsResourceId := jResp.BoolOf('product.tags.resource.id');
productTagsResourceUrl := jResp.StringOf('product.tags.resource.url');
productTagsResourceLink := jResp.StringOf('product.tags.resource.link');
productVariantsResourceId := jResp.BoolOf('product.variants.resource.id');
productVariantsResourceUrl := jResp.StringOf('product.variants.resource.url');
productVariantsResourceLink := jResp.StringOf('product.variants.resource.link');
productMovementsResourceId := jResp.BoolOf('product.movements.resource.id');
productMovementsResourceUrl := jResp.StringOf('product.movements.resource.url');
productMovementsResourceLink := jResp.StringOf('product.movements.resource.link');
http.Free;
json.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.