2018-11-21 22:11:58 +01:00
|
|
|
|
unit MainClientFormU;
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
Winapi.Windows,
|
|
|
|
|
Winapi.Messages,
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
System.Variants,
|
|
|
|
|
System.Classes,
|
|
|
|
|
Vcl.Graphics,
|
|
|
|
|
Vcl.Controls,
|
|
|
|
|
Vcl.Forms,
|
|
|
|
|
Vcl.Dialogs,
|
|
|
|
|
System.Net.HttpClientComponent,
|
|
|
|
|
Vcl.StdCtrls,
|
|
|
|
|
System.Net.URLClient,
|
|
|
|
|
System.Net.HttpClient,
|
|
|
|
|
Data.DB,
|
|
|
|
|
Vcl.Grids,
|
|
|
|
|
Vcl.DBGrids,
|
|
|
|
|
FireDAC.Stan.Intf,
|
|
|
|
|
FireDAC.Stan.Option,
|
|
|
|
|
FireDAC.Stan.Param,
|
|
|
|
|
FireDAC.Stan.Error,
|
|
|
|
|
FireDAC.DatS,
|
|
|
|
|
FireDAC.Phys.Intf,
|
|
|
|
|
FireDAC.DApt.Intf,
|
|
|
|
|
FireDAC.Comp.DataSet,
|
|
|
|
|
FireDAC.Comp.Client,
|
|
|
|
|
Vcl.ComCtrls,
|
|
|
|
|
Vcl.ExtCtrls,
|
|
|
|
|
MVCFramework.JSONRPC.Client;
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
TMainForm = class(TForm)
|
|
|
|
|
DataSource1: TDataSource;
|
|
|
|
|
FDMemTable1: TFDMemTable;
|
|
|
|
|
FDMemTable1Code: TIntegerField;
|
|
|
|
|
FDMemTable1Name: TStringField;
|
|
|
|
|
GroupBox1: TGroupBox;
|
|
|
|
|
edtValue1: TEdit;
|
|
|
|
|
edtValue2: TEdit;
|
|
|
|
|
btnSubstract: TButton;
|
|
|
|
|
edtResult: TEdit;
|
|
|
|
|
edtReverseString: TEdit;
|
|
|
|
|
btnReverseString: TButton;
|
|
|
|
|
edtReversedString: TEdit;
|
|
|
|
|
GroupBox2: TGroupBox;
|
|
|
|
|
edtUserName: TEdit;
|
|
|
|
|
btnGetUser: TButton;
|
|
|
|
|
lbPerson: TListBox;
|
|
|
|
|
GroupBox4: TGroupBox;
|
|
|
|
|
edtFirstName: TLabeledEdit;
|
|
|
|
|
edtLastName: TLabeledEdit;
|
|
|
|
|
chkMarried: TCheckBox;
|
|
|
|
|
dtDOB: TDateTimePicker;
|
|
|
|
|
btnSave: TButton;
|
|
|
|
|
dtNextMonday: TDateTimePicker;
|
|
|
|
|
btnAddDay: TButton;
|
|
|
|
|
btnInvalid1: TButton;
|
|
|
|
|
btnInvalid2: TButton;
|
|
|
|
|
btnNotification: TButton;
|
|
|
|
|
btnInvalidMethod: TButton;
|
|
|
|
|
PageControl1: TPageControl;
|
|
|
|
|
TabSheet1: TTabSheet;
|
|
|
|
|
TabSheet2: TTabSheet;
|
|
|
|
|
GroupBox5: TGroupBox;
|
|
|
|
|
edtSearchText: TEdit;
|
|
|
|
|
btnSearch: TButton;
|
|
|
|
|
ListBox1: TListBox;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
CheckBox1: TCheckBox;
|
|
|
|
|
btnDates: TButton;
|
2019-08-23 12:01:33 +02:00
|
|
|
|
btnFloatsTests: TButton;
|
2019-11-05 14:40:39 +01:00
|
|
|
|
btnWithJSON: TButton;
|
2020-05-02 16:39:32 +02:00
|
|
|
|
Edit1: TEdit;
|
|
|
|
|
Edit2: TEdit;
|
|
|
|
|
btnSubtractWithNamedParams: TButton;
|
|
|
|
|
Edit3: TEdit;
|
2020-09-22 15:18:22 +02:00
|
|
|
|
PageControl2: TPageControl;
|
|
|
|
|
TabSheet3: TTabSheet;
|
|
|
|
|
TabSheet4: TTabSheet;
|
|
|
|
|
edtFilter: TEdit;
|
|
|
|
|
edtGetCustomers: TButton;
|
|
|
|
|
DBGrid1: TDBGrid;
|
|
|
|
|
btnGetMulti: TButton;
|
|
|
|
|
lbMulti: TListBox;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
procedure btnSubstractClick(Sender: TObject);
|
|
|
|
|
procedure btnReverseStringClick(Sender: TObject);
|
|
|
|
|
procedure edtGetCustomersClick(Sender: TObject);
|
|
|
|
|
procedure btnGetUserClick(Sender: TObject);
|
|
|
|
|
procedure btnSaveClick(Sender: TObject);
|
|
|
|
|
procedure btnAddDayClick(Sender: TObject);
|
|
|
|
|
procedure FormCreate(Sender: TObject);
|
|
|
|
|
procedure btnInvalid1Click(Sender: TObject);
|
|
|
|
|
procedure btnInvalid2Click(Sender: TObject);
|
|
|
|
|
procedure btnNotificationClick(Sender: TObject);
|
|
|
|
|
procedure btnInvalidMethodClick(Sender: TObject);
|
|
|
|
|
procedure btnSearchClick(Sender: TObject);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
procedure btnDatesClick(Sender: TObject);
|
2019-08-23 12:01:33 +02:00
|
|
|
|
procedure btnFloatsTestsClick(Sender: TObject);
|
2019-11-05 14:40:39 +01:00
|
|
|
|
procedure btnWithJSONClick(Sender: TObject);
|
2020-05-02 16:39:32 +02:00
|
|
|
|
procedure btnSubtractWithNamedParamsClick(Sender: TObject);
|
2020-09-22 15:18:22 +02:00
|
|
|
|
procedure btnGetMultiClick(Sender: TObject);
|
|
|
|
|
procedure btnGetListOfDatasetClick(Sender: TObject);
|
2020-09-25 00:32:55 +02:00
|
|
|
|
procedure btnObjDictClick(Sender: TObject);
|
2018-11-21 22:11:58 +01:00
|
|
|
|
private
|
|
|
|
|
FExecutor: IMVCJSONRPCExecutor;
|
2020-09-25 00:32:55 +02:00
|
|
|
|
// FExecutor2: IMVCJSONRPCExecutor;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
public
|
|
|
|
|
{ Public declarations }
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
var
|
|
|
|
|
MainForm: TMainForm;
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
2020-09-22 15:18:22 +02:00
|
|
|
|
System.Generics.Collections,
|
2018-11-21 22:11:58 +01:00
|
|
|
|
MVCFramework.JSONRPC,
|
|
|
|
|
MVCFramework.Serializer.JsonDataObjects,
|
|
|
|
|
JsonDataObjects,
|
|
|
|
|
MVCFramework.Serializer.Commons,
|
2020-09-22 15:18:22 +02:00
|
|
|
|
MVCFramework.Commons,
|
|
|
|
|
MVCFramework.Serializer.Defaults,
|
2018-11-21 22:11:58 +01:00
|
|
|
|
MVCFramework.DataSet.Utils,
|
2019-08-23 12:01:33 +02:00
|
|
|
|
BusinessObjectsU,
|
|
|
|
|
System.Math,
|
|
|
|
|
System.Rtti;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
|
2020-08-05 09:50:06 +02:00
|
|
|
|
|
2018-11-21 22:11:58 +01:00
|
|
|
|
procedure TMainForm.btnAddDayClick(Sender: TObject);
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'getnextmonday';
|
|
|
|
|
lReq.RequestID := Random(1000);
|
|
|
|
|
lReq.Params.Add(dtNextMonday.Date);
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
dtNextMonday.Date := ISOTimeStampToDateTime(lResp.Result.AsString);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.btnDatesClick(Sender: TObject);
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create(1234, 'playwithdatesandtimes');
|
|
|
|
|
lReq.Params.Add(1234.5678, pdtFloat);
|
|
|
|
|
lReq.Params.Add(Time(), pdtTime);
|
|
|
|
|
lReq.Params.Add(Date(), pdtDate);
|
|
|
|
|
lReq.Params.Add(Now(), pdtDateTime);
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2020-09-02 19:43:34 +02:00
|
|
|
|
ShowMessage(DateTimeToStr(lResp.Result.AsType<TDateTime>));
|
2020-08-05 09:50:06 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-08-23 12:01:33 +02:00
|
|
|
|
procedure TMainForm.btnFloatsTestsClick(Sender: TObject);
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
|
|
|
|
lRes: Extended;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create(1234, 'floatstest');
|
|
|
|
|
lReq.Params.Add(1234.5678, pdtFloat);
|
|
|
|
|
lReq.Params.Add(2345.6789, pdtFloat);
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2019-08-23 12:01:33 +02:00
|
|
|
|
lRes := lResp.Result.AsType<Extended>;
|
|
|
|
|
lRes := RoundTo(lRes, -4);
|
2020-05-02 16:39:32 +02:00
|
|
|
|
Assert(SameValue(lRes, 3580.2467), 'Wrong result: ' + FloatToStrF(lRes, ffGeneral, 18, 9));
|
2019-08-23 12:01:33 +02:00
|
|
|
|
|
|
|
|
|
lReq := TJSONRPCRequest.Create(1234, 'floatstest');
|
|
|
|
|
lReq.Params.Add(123);
|
|
|
|
|
lReq.Params.Add(234);
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2019-08-23 12:01:33 +02:00
|
|
|
|
lRes := lResp.Result.AsType<Extended>;
|
|
|
|
|
lRes := RoundTo(lRes, -4);
|
2020-05-02 16:39:32 +02:00
|
|
|
|
Assert(SameValue(lRes, 357), 'Wrong result: ' + FloatToStrF(lRes, ffGeneral, 18, 9));
|
2019-08-23 12:01:33 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2018-11-21 22:11:58 +01:00
|
|
|
|
procedure TMainForm.btnGetUserClick(Sender: TObject);
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
lJSON: TJsonObject;
|
|
|
|
|
begin
|
|
|
|
|
lbPerson.Clear;
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'getuser';
|
|
|
|
|
lReq.RequestID := Random(1000);
|
|
|
|
|
lReq.Params.Add(edtUserName.Text);
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
if Assigned(lResp.Error) then
|
|
|
|
|
raise Exception.Create(lResp.Error.ErrMessage);
|
2018-11-21 22:11:58 +01:00
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
|
// Remember that TObject descendants (but TDataset, TJDOJSONObject and TJDOJSONArray)
|
|
|
|
|
// are serialized as JSON objects
|
|
|
|
|
lJSON := lResp.Result.AsObject as TJsonObject;
|
|
|
|
|
lbPerson.Items.Add('First Name:'.PadRight(15) + lJSON.S['firstname']);
|
|
|
|
|
lbPerson.Items.Add('Last Name:'.PadRight(15) + lJSON.S['lastname']);
|
|
|
|
|
lbPerson.Items.Add('Married:'.PadRight(15) + lJSON.B['married'].ToString(TUseBoolStrs.True));
|
|
|
|
|
lbPerson.Items.Add('DOB:'.PadRight(15) + DateToStr(lJSON.D['dob']));
|
2018-11-21 22:11:58 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.btnInvalid1Click(Sender: TObject);
|
|
|
|
|
var
|
2020-09-02 19:43:34 +02:00
|
|
|
|
lReq: IJSONRPCRequest;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lResp: IJSONRPCResponse;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
begin
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lReq := TJSONRPCRequest.Create(1234);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'invalidmethod1';
|
|
|
|
|
lReq.Params.Add(1);
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
ShowMessage(lResp.Error.ErrMessage);
|
2018-11-21 22:11:58 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.btnInvalid2Click(Sender: TObject);
|
|
|
|
|
var
|
2020-09-02 19:43:34 +02:00
|
|
|
|
lReq: IJSONRPCRequest;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lResp: IJSONRPCResponse;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
begin
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lReq := TJSONRPCRequest.Create(1234);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'invalidmethod2';
|
|
|
|
|
lReq.Params.Add(1);
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
ShowMessage(lResp.Error.ErrMessage);
|
2018-11-21 22:11:58 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.btnInvalidMethodClick(Sender: TObject);
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lNotification: IJSONRPCNotification;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
begin
|
|
|
|
|
lNotification := TJSONRPCNotification.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lNotification.Method := 'notexists';
|
2020-09-25 00:32:55 +02:00
|
|
|
|
FExecutor.ExecuteNotification('/jsonrpc', lNotification);
|
2018-11-21 22:11:58 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.btnNotificationClick(Sender: TObject);
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lNotification: IJSONRPCNotification;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
begin
|
|
|
|
|
lNotification := TJSONRPCNotification.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lNotification.Method := 'dosomething';
|
2020-09-25 00:32:55 +02:00
|
|
|
|
FExecutor.ExecuteNotification('/jsonrpc', lNotification);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.btnObjDictClick(Sender: TObject);
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
|
|
|
|
lMultiDS: TMultiDataset;
|
|
|
|
|
begin
|
|
|
|
|
FDMemTable1.Active := False;
|
|
|
|
|
lReq := TJSONRPCRequest.Create(Random(1000), 'getobjdict');
|
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
|
|
|
|
|
|
|
|
|
lMultiDS := TMultiDataset.Create;
|
|
|
|
|
try
|
|
|
|
|
JsonObjectToObject(lResp.ResultAsJSONObject, lMultiDS);
|
|
|
|
|
lbMulti.Clear;
|
|
|
|
|
|
|
|
|
|
lMultiDS.Customers.First;
|
|
|
|
|
lbMulti.Items.Add('** CUSTOMERS **');
|
|
|
|
|
while not lMultiDS.Customers.Eof do
|
|
|
|
|
begin
|
|
|
|
|
lbMulti.Items.Add(Format('%-20s (Code %3s)', [lMultiDS.Customers.FieldByName('Name').AsString,
|
|
|
|
|
lMultiDS.Customers.FieldByName('Code').AsString]));
|
|
|
|
|
lMultiDS.Customers.Next;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
lMultiDS.People.First;
|
|
|
|
|
lbMulti.Items.Add('** PEOPLE **');
|
|
|
|
|
while not lMultiDS.People.Eof do
|
|
|
|
|
begin
|
|
|
|
|
lbMulti.Items.Add(Format('%s %s', [lMultiDS.People.FieldByName('FirstName').AsString,
|
|
|
|
|
lMultiDS.People.FieldByName('LastName').AsString]));
|
|
|
|
|
lMultiDS.People.Next;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
lMultiDS.Free;
|
|
|
|
|
end;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.btnReverseStringClick(Sender: TObject);
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'reversestring';
|
|
|
|
|
lReq.RequestID := Random(1000);
|
2020-05-02 16:39:32 +02:00
|
|
|
|
lReq.Params.AddByName('aString', edtReverseString.Text);
|
|
|
|
|
lReq.Params.AddByName('aUpperCase', CheckBox1.Checked);
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
edtReversedString.Text := lResp.Result.AsString;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.btnSaveClick(Sender: TObject);
|
|
|
|
|
var
|
|
|
|
|
lPerson: TPerson;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'saveperson';
|
|
|
|
|
lReq.RequestID := Random(1000);
|
|
|
|
|
lPerson := TPerson.Create;
|
2020-05-02 16:39:32 +02:00
|
|
|
|
lReq.Params.AddByName('Person', lPerson, pdtObject);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lPerson.FirstName := edtFirstName.Text;
|
|
|
|
|
lPerson.LastName := edtLastName.Text;
|
|
|
|
|
lPerson.Married := chkMarried.Checked;
|
|
|
|
|
lPerson.DOB := dtDOB.Date;
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
ShowMessage('Person saved with ID = ' + lResp.Result.AsInteger.ToString);
|
2018-11-21 22:11:58 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.btnSearchClick(Sender: TObject);
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
lJSON: TJsonArray;
|
|
|
|
|
I: Integer;
|
2019-12-17 14:52:11 +01:00
|
|
|
|
lJObj: TJsonObject;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
begin
|
2019-12-17 14:52:11 +01:00
|
|
|
|
ListBox1.Clear;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'searchproducts';
|
|
|
|
|
lReq.RequestID := 1234;
|
|
|
|
|
lReq.Params.Add(edtSearchText.Text);
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/rpcdatamodule', lReq);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
if Assigned(lResp.Error) then
|
|
|
|
|
raise Exception.Create(lResp.Error.ErrMessage);
|
2018-11-21 22:11:58 +01:00
|
|
|
|
|
2019-01-08 12:48:27 +01:00
|
|
|
|
// Remember that TObject descendants (but TDataset, TJDOJSONObject and TJDOJSONArray)
|
|
|
|
|
// are serialized as JSON objects
|
|
|
|
|
lJSON := lResp.Result.AsObject as TJsonArray;
|
|
|
|
|
for I := 0 to lJSON.Count - 1 do
|
|
|
|
|
begin
|
2019-12-17 14:52:11 +01:00
|
|
|
|
lJObj := lJSON[I].ObjectValue;
|
2020-09-02 19:43:34 +02:00
|
|
|
|
ListBox1.Items.Add(Format('%6s: %-34s <20> %5.2f', [lJObj.S['codice'], lJObj.S['descrizione'], lJObj.F['prezzo']]));
|
2018-11-21 22:11:58 +01:00
|
|
|
|
end;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
// lbPerson.Items.Add('First Name:'.PadRight(15) + lJSON.S['firstname']);
|
|
|
|
|
// lbPerson.Items.Add('Last Name:'.PadRight(15) + lJSON.S['lastname']);
|
|
|
|
|
// lbPerson.Items.Add('Married:'.PadRight(15) + lJSON.B['married'].ToString(TUseBoolStrs.True));
|
|
|
|
|
// lbPerson.Items.Add('DOB:'.PadRight(15) + DateToStr(lJSON.D['dob']));
|
2018-11-21 22:11:58 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.btnSubstractClick(Sender: TObject);
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq.Method := 'subtract';
|
|
|
|
|
lReq.RequestID := Random(1000);
|
|
|
|
|
lReq.Params.Add(StrToInt(edtValue1.Text));
|
|
|
|
|
lReq.Params.Add(StrToInt(edtValue2.Text));
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
edtResult.Text := lResp.Result.AsInteger.ToString;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-05-02 16:39:32 +02:00
|
|
|
|
procedure TMainForm.btnSubtractWithNamedParamsClick(Sender: TObject);
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
|
|
|
|
lReq.Method := 'subtract';
|
|
|
|
|
lReq.RequestID := Random(1000);
|
|
|
|
|
lReq.Params.AddByName('Value1', StrToInt(Edit1.Text));
|
|
|
|
|
lReq.Params.AddByName('Value2', StrToInt(Edit2.Text));
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2020-05-02 16:39:32 +02:00
|
|
|
|
Edit3.Text := lResp.Result.AsInteger.ToString;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-11-05 14:40:39 +01:00
|
|
|
|
procedure TMainForm.btnWithJSONClick(Sender: TObject);
|
|
|
|
|
var
|
|
|
|
|
lPerson: TJsonObject;
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
|
|
|
|
begin
|
|
|
|
|
lReq := TJSONRPCRequest.Create;
|
|
|
|
|
lReq.Method := 'SaveObjectWithJSON';
|
|
|
|
|
lReq.RequestID := 1234;
|
|
|
|
|
lPerson := TJsonObject.Create;
|
|
|
|
|
lReq.Params.Add(lPerson, pdTJDOJsonObject);
|
|
|
|
|
lPerson.S['StringProp'] := 'Hello World';
|
|
|
|
|
lPerson.O['JSONObject'] := TJsonObject.Parse('{"name":"Daniele"}') as TJsonObject;
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2019-11-05 14:40:39 +01:00
|
|
|
|
|
|
|
|
|
lPerson := lResp.Result.AsObject as TJsonObject;
|
|
|
|
|
ShowMessage(lPerson.ToJSON(False));
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-22 15:18:22 +02:00
|
|
|
|
procedure TMainForm.btnGetListOfDatasetClick(Sender: TObject);
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
|
|
|
|
lMultiDS: TObjectList<TDataSet>;
|
|
|
|
|
begin
|
|
|
|
|
FDMemTable1.Active := False;
|
|
|
|
|
lReq := TJSONRPCRequest.Create(Random(1000), 'GetDataSetList');
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2020-09-22 15:18:22 +02:00
|
|
|
|
|
|
|
|
|
lMultiDS := TObjectList<TDataSet>.Create(True);
|
|
|
|
|
try
|
|
|
|
|
JsonArrayToList(lResp.ResultAsJSONArray, WrapAsList(lMultiDS), TDataSet, TMVCSerializationType.stDefault, nil);
|
|
|
|
|
finally
|
|
|
|
|
lMultiDS.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.btnGetMultiClick(Sender: TObject);
|
|
|
|
|
var
|
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
|
|
|
|
lMultiDS: TMultiDataset;
|
|
|
|
|
begin
|
|
|
|
|
FDMemTable1.Active := False;
|
|
|
|
|
lReq := TJSONRPCRequest.Create(Random(1000), 'getmulti');
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2020-09-22 15:18:22 +02:00
|
|
|
|
|
|
|
|
|
lMultiDS := TMultiDataset.Create;
|
|
|
|
|
try
|
|
|
|
|
JsonObjectToObject(lResp.ResultAsJSONObject, lMultiDS);
|
|
|
|
|
lbMulti.Clear;
|
|
|
|
|
|
|
|
|
|
lMultiDS.Customers.First;
|
|
|
|
|
lbMulti.Items.Add('** CUSTOMERS **');
|
|
|
|
|
while not lMultiDS.Customers.Eof do
|
|
|
|
|
begin
|
|
|
|
|
lbMulti.Items.Add(Format('%-20s (Code %3s)', [lMultiDS.Customers.FieldByName('Name').AsString,
|
|
|
|
|
lMultiDS.Customers.FieldByName('Code').AsString]));
|
|
|
|
|
lMultiDS.Customers.Next;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
lMultiDS.People.First;
|
|
|
|
|
lbMulti.Items.Add('** PEOPLE **');
|
|
|
|
|
while not lMultiDS.People.Eof do
|
|
|
|
|
begin
|
|
|
|
|
lbMulti.Items.Add(Format('%s %s', [lMultiDS.People.FieldByName('FirstName').AsString,
|
|
|
|
|
lMultiDS.People.FieldByName('LastName').AsString]));
|
|
|
|
|
lMultiDS.People.Next;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
lMultiDS.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-11-21 22:11:58 +01:00
|
|
|
|
procedure TMainForm.edtGetCustomersClick(Sender: TObject);
|
|
|
|
|
var
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq: IJSONRPCRequest;
|
|
|
|
|
lResp: IJSONRPCResponse;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
begin
|
|
|
|
|
FDMemTable1.Active := False;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
lReq := TJSONRPCRequest.Create(Random(1000), 'getcustomers');
|
2020-05-02 16:39:32 +02:00
|
|
|
|
lReq.Params.AddByName('FilterString', edtFilter.Text);
|
2020-09-25 00:32:55 +02:00
|
|
|
|
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
FDMemTable1.Active := True;
|
|
|
|
|
FDMemTable1.LoadFromTValue(lResp.Result);
|
2019-12-17 14:52:11 +01:00
|
|
|
|
FDMemTable1.First;
|
2018-11-21 22:11:58 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMainForm.FormCreate(Sender: TObject);
|
|
|
|
|
begin
|
2020-09-25 00:32:55 +02:00
|
|
|
|
FExecutor := TMVCJSONRPCExecutor.Create('http://localhost:8080');
|
|
|
|
|
/// jsonrpc');
|
|
|
|
|
// FExecutor2 := TMVCJSONRPCExecutor.Create('http://localhost:8080/rpcdatamodule');
|
2018-11-21 22:11:58 +01:00
|
|
|
|
dtNextMonday.Date := Date;
|
|
|
|
|
|
|
|
|
|
// these are the methods to handle http headers in JSONRPC
|
|
|
|
|
// the following line and the check on the server is just for demo
|
2020-05-02 16:39:32 +02:00
|
|
|
|
Assert(FExecutor.HTTPHeadersCount = 0);
|
2018-11-21 22:11:58 +01:00
|
|
|
|
FExecutor.AddHTTPHeader(TNetHeader.Create('x-token', TGUID.NewGuid.ToString));
|
2020-05-02 16:39:32 +02:00
|
|
|
|
Assert(FExecutor.HTTPHeadersCount = 1);
|
2018-11-21 22:11:58 +01:00
|
|
|
|
FExecutor.ClearHTTPHeaders;
|
2020-05-02 16:39:32 +02:00
|
|
|
|
Assert(FExecutor.HTTPHeadersCount = 0);
|
2018-11-21 22:11:58 +01:00
|
|
|
|
FExecutor.AddHTTPHeader(TNetHeader.Create('x-token', TGUID.NewGuid.ToString));
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end.
|