unit frmMain;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs, REST.Types,
Vcl.StdCtrls, System.JSON,
REST.Client, AdvUtil,
Vcl.Grids, AdvObj, BaseGrid, AdvGrid,
Vcl.ExtCtrls;
type
TRows =
array of array of string;
// [Cols, Rows]
TCols =
array of string;
TForm1 =
class(TForm)
edtTrckngNr: TEdit;
btnStrt: TButton;
mmResult: TMemo;
SG_Result: TAdvStringGrid;
Panel1: TPanel;
lblTrckngNr: TLabel;
edtAPIKEY: TEdit;
lblAPIKEY: TLabel;
mmURL: TMemo;
lblURL: TLabel;
procedure btnStrtClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure FillAdvGrid(SG: TAdvStringGrid; Cols: TCols; Rows: TRows);
var
i, j: Integer;
begin
SG.Filter.Clear;
SG.FilterActive:=false;
SG.Clear;
SG.FloatingFooter.Visible:=false;
for i := 0
to SG.ColCount -1
do begin
SG.Cols[i].BeginUpdate;
end;
if Assigned(Rows)
then begin
// Wir brauchen eine Zeile mehr für die Spaltenüberschriften
SG.RowCount := length(Rows[0]) +1;
SG.ColCount := length(Cols);
SG.FixedRows := 0;
// Spaltenüberschriften in die erste Zeile schreiben
for i := 0
to length(Cols) - 1
do begin
SG.Cols[i].Add(Cols[i]);
SG.Cells[i, 0] := Cols[i];
end;
// zwei-dimensionales Zeilen-Array in den Zellen ausgeben
for i := 0
to length(Cols) - 1
do begin
for j := 0
to length(Rows[0]) - 1
do begin
SG.Cells[i, j + 1] := Rows[i, j];
end;
end;
end;
for i := 0
to SG.ColCount -1
do begin
SG.Cols[i].EndUpdate;
end;
end;
procedure JSArrayToColsRows(Response:
string; ArraySectionPath:
string;
var Cols: TCols;
var Rows: TRows);
var JSValue: TJSONValue;
JSObjEvent: TJSONObject;
JSArryEvents: TJSONArray;
I, J: integer;
begin
JSValue:= TJSonObject.ParseJSONValue(Response);
JSArryEvents:= TJSONArray(JSValue.FindValue(ArraySectionPath));
for I := 0
to JSArryEvents.Count -1
do begin
if JSArryEvents.Items[I]
is TJSONObject
then begin
JSObjEvent := TJSONObject(JSArryEvents.Items[I]);
SetLength(Cols, JSObjEvent.Count);
SetLength(Rows, Length(Cols), I+1);
for J := 0
to JSObjEvent.Count -1
do begin
Cols[J]:= JSObjEvent.Pairs[J].JsonString.ToString;
Rows[J, Length(Rows[0]) -1]:= JSObjEvent.Pairs[J].JsonValue.ToString;
end;
end;
end;
end;
function cUrlCall (TrackNr, APIKEY:
string;
var URL:
string;
var Cols: TCols;
var Rows: TRows):
string;
var client : TRESTClient;
request : TRESTRequest;
begin
client := TRESTClient.Create('
https://api-eu.dhl.com');
try
try
request := TRESTRequest.Create(client);
request.Method := rmGet;
request.Resource := '
track/shipments';
request.AddParameter('
trackingNumber', TrackNr, pkGETorPOST, []);
request.AddParameter('
language', '
DE', pkGETorPOST, []);
request.Params.AddItem('
DHL-API-Key', APIKEY, pkHTTPHEADER, [poDoNotEncode]);
URL:= request.GetFullRequestURL;
request.Execute;
JSArrayToColsRows(request.Response.Content, '
shipments[0].events', Cols, Rows);
Result:= request.Response.Content;
Except
ON e:
Exception do begin
Result:= E.
Message;
ShowMessage('
Keine Daten erhalten');
end;
end;
finally
client.Free;
end;
end;
procedure TForm1.btnStrtClick(Sender: TObject);
var RequestResult:
string;
URL:
string;
Cols: TCols;
Rows: TRows;
begin
RequestResult:= cUrlCall(edtTrckngNr.Text, edtAPIKEY.Text,
URL, Cols, Rows);
mmURL.lines.Text:=
URL;
mmResult.Lines.Text:= RequestResult;
SG_Result.AutoSize:= false;
FillAdvGrid(SG_Result, Cols, Rows);
SG_Result.FixedRows:= 1;
SG_Result.AutoSize:= true;
end;
//In CMD eingeben
// curl https://api-eu.dhl.com/track/shipments?trackingNumber=yyyyyyyyyyyyyyy -H DHL-API-Key:xxxxxxxxxxxxxxxxx -v
end.