2024-07-02 19:14:45 +02:00
// ***************************************************************************
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2023 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//
// ***************************************************************************
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// *************************************************************************** }
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, Vcl. Mask, WaitingFormU;
type
TMainForm = class( TForm)
DataSource1: TDataSource;
FDMemTable1: TFDMemTable;
FDMemTable1Code: TIntegerField;
FDMemTable1Name: TStringField;
GroupBox1: TGroupBox;
edtValue1: TEdit;
edtValue2: TEdit;
btnSubtract: 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;
CheckBox1: TCheckBox;
btnDates: TButton;
btnFloatsTests: TButton;
btnWithJSON: TButton;
Edit1: TEdit;
Edit2: TEdit;
btnSubtractWithNamedParams: TButton;
Edit3: TEdit;
PageControl2: TPageControl;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
edtFilter: TEdit;
edtGetCustomers: TButton;
DBGrid1: TDBGrid;
btnGetMulti: TButton;
lbMulti: TListBox;
btnGenericException: TButton;
TabSheet5: TTabSheet;
Label1: TLabel;
btnException: TButton;
btnGenericExcWithCustomHandling: TButton;
btnGenericExcWithCustomHAndling2: TButton;
btnGenericExcWithoutCustomHandling: TButton;
TabSheet6: TTabSheet;
btnSingleRec: TButton;
lbLogRec: TMemo;
btnGetArrayOfRecords: TButton;
btnGetDynArray: TButton;
btnPassAndGetRecord: TButton;
btnEchoComplexArray: TButton;
btnComplex: TButton;
btnParallel: TButton;
procedure btnSubtractClick( 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) ;
procedure btnDatesClick( Sender: TObject) ;
procedure btnFloatsTestsClick( Sender: TObject) ;
procedure btnWithJSONClick( Sender: TObject) ;
procedure btnSubtractWithNamedParamsClick( Sender: TObject) ;
procedure btnGetMultiClick( Sender: TObject) ;
procedure btnGetListOfDatasetClick( Sender: TObject) ;
procedure btnObjDictClick( Sender: TObject) ;
procedure btnExceptionClick( Sender: TObject) ;
procedure btnGenericExceptionClick( Sender: TObject) ;
procedure btnGenericExcWithCustomHandlingClick( Sender: TObject) ;
procedure btnGenericExcWithCustomHAndling2Click( Sender: TObject) ;
procedure btnGenericExcWithoutCustomHandlingClick( Sender: TObject) ;
procedure btnSingleRecClick( Sender: TObject) ;
procedure btnGetArrayOfRecordsClick( Sender: TObject) ;
procedure btnGetDynArrayClick( Sender: TObject) ;
procedure btnPassAndGetRecordClick( Sender: TObject) ;
procedure btnEchoComplexArrayClick( Sender: TObject) ;
procedure btnComplexClick( Sender: TObject) ;
procedure btnParallelClick( Sender: TObject) ;
private
fExecutor: IMVCJSONRPCExecutorAsync;
fExecutorAsync: IMVCJSONRPCExecutorAsync;
2024-10-10 01:26:34 +02:00
fGeneralErrorHandler: TJSONRPCErrorHandlerProc;
2024-07-02 19:14:45 +02:00
fWaiting: TWaitingForm;
public
{ Public declarations }
end ;
var
MainForm: TMainForm;
implementation
uses
System. Generics. Collections,
MVCFramework. JSONRPC,
MVCFramework. Serializer. JsonDataObjects,
JsonDataObjects,
System. UITypes,
MVCFramework. Serializer. Commons,
MVCFramework. Commons,
MVCFramework. Logger,
MVCFramework. Serializer. Defaults,
MVCFramework. DataSet. Utils,
SyncObjs,
BusinessObjectsU,
System. Math,
System. Rtti, CommonTypesU, MVCFramework. AsyncTask;
{$R *.dfm}
procedure TMainForm. btnAddDayClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
lReq : = TJSONRPCRequest. Create;
lReq. Method : = 'getnextmonday' ;
lReq. RequestID : = Random( 1 0 0 0 ) ;
lReq. Params. Add( dtNextMonday. Date) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
dtNextMonday. Date : = ISODateToDate( Resp. Result . AsString) ;
end ) ;
end ;
procedure TMainForm. btnComplexClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
lComplex: TNestedArraysRec;
begin
lReq : = TJSONRPCRequest. Create;
lReq. Method : = 'EchoComplexArrayOfRecords2' ;
lReq. RequestID : = Random( 1 0 0 0 ) ;
lComplex. TestRecProp : = TTestRec. Create( 1 0 ) ;
SetLength( lComplex. ArrayProp1, 2 ) ;
SetLength( lComplex. ArrayProp2, 2 ) ;
lComplex. ArrayProp1[ 0 ] : = TTestRec. Create( 1 0 ) ;
lComplex. ArrayProp1[ 1 ] : = TTestRec. Create( 1 0 ) ;
lComplex. ArrayProp2[ 0 ] : = TTestRec. Create( 1 0 ) ;
lComplex. ArrayProp2[ 1 ] : = TTestRec. Create( 1 0 ) ;
lReq. Params. Add( TValue. From< TNestedArraysRec> ( lComplex) , pdtRecordOrArrayOfRecord) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
lComplex : = TJSONUtils. JSONObjectToRecord< TNestedArraysRec> ( Resp) ;
lbLogRec. Lines. Clear;
lbLogRec. Lines. Add( lComplex. ToString) ;
end ,
2024-10-10 01:26:34 +02:00
procedure( Exc: Exception)
2024-07-02 19:14:45 +02:00
begin
ShowMessage( Exc. ClassName + ': ' + Exc. Message ) ;
end ) ;
end ;
procedure TMainForm. btnDatesClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
lReq : = TJSONRPCRequest. Create( 1 2 3 4 , 'playwithdatesandtimes' ) ;
lReq. Params. Add( 1234.5678 , pdtFloat) ;
lReq. Params. Add( Time( ) , pdtTime) ;
lReq. Params. Add( Date( ) , pdtDate) ;
lReq. Params. Add( Now( ) , pdtDateTime) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
ShowMessage( Resp. Result . AsString) ;
end ) ;
end ;
procedure TMainForm. btnEchoComplexArrayClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
lPeople: TTestRecDynArray;
I: Integer ;
begin
lReq : = TJSONRPCRequest. Create;
lReq. Method : = 'EchoComplexArrayOfRecords' ;
lReq. RequestID : = Random( 1 0 0 0 ) ;
SetLength( lPeople, 2 ) ;
lPeople[ 0 ] : = TTestRec. Create( 1 ) ;
lPeople[ 1 ] : = TTestRec. Create( 2 ) ;
lReq. Params. Add( TValue. From< TTestRecDynArray> ( lPeople) , pdtRecordOrArrayOfRecord) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
procedure( Resp: IJSONRPCResponse)
begin
lPeople : = TJSONUtils. JSONArrayToArrayOfRecord< TTestRec> ( Resp) ;
lbLogRec. Lines. Clear;
lbLogRec. Lines. Add( '--- array of record elements ---' ) ;
I : = 1 ;
for var lPRec in lPeople do
begin
lbLogRec. Lines. Add( 'ITEM: ' + I. ToString) ;
lbLogRec. Lines. Add( lPRec. ToString) ;
Inc( I) ;
end ;
end ) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. btnExceptionClick( Sender: TObject) ;
var
lReq: IJSONRPCNotification;
begin
ShowMessage( 'Now will be raised a custom exception on the server. This exception will be catched by the client' ) ;
lReq : = TJSONRPCNotification. Create( 'RaiseCustomException' ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteNotificationAsync( '/jsonrpc' , lReq, fGeneralErrorHandler) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. btnFloatsTestsClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
lRes: Extended ;
begin
lReq : = TJSONRPCRequest. Create( 1 2 3 4 , 'floatstest' ) ;
lReq. Params. Add( 1234.5678 , pdtFloat) ;
lReq. Params. Add( 2345.6789 , pdtFloat) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
lRes : = Resp. Result . AsType< Extended > ;
lRes : = RoundTo( lRes, - 4 ) ;
Assert( SameValue( lRes, 3580.2467 ) , 'Wrong result: ' + FloatToStrF( lRes, ffGeneral, 1 8 , 9 ) ) ;
lReq : = TJSONRPCRequest. Create( 1 2 3 4 , 'floatstest' ) ;
lReq. Params. Add( 1 2 3 ) ;
lReq. Params. Add( 2 3 4 ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
procedure( Resp: IJSONRPCResponse)
var
lRes: Extended ;
begin
lRes : = Resp. Result . AsType< Extended > ;
lRes : = RoundTo( lRes, - 4 ) ;
Assert( SameValue( lRes, 3 5 7 ) , 'Wrong result: ' + FloatToStrF( lRes, ffGeneral, 1 8 , 9 ) ) ;
end ) ;
2024-07-02 19:14:45 +02:00
end ) ;
end ;
procedure TMainForm. btnGetUserClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
lbPerson. Clear;
lReq : = TJSONRPCRequest. Create;
lReq. Method : = 'getuser' ;
lReq. RequestID : = Random( 1 0 0 0 ) ;
lReq. Params. Add( edtUserName. Text ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
procedure( Resp: IJSONRPCResponse)
begin
// Remember that TObject descendants (but TDataset, TJDOJSONObject and TJDOJSONArray)
// are serialized as JSON objects, so you can always read the JSON object
// lJSON := Resp.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']));
var
lPerson : = TPerson. Create;
try
Resp. ResultAs( lPerson) ;
lbPerson. Items. Add( 'First Name:' . PadRight( 1 5 ) + lPerson. FirstName) ;
lbPerson. Items. Add( 'Last Name:' . PadRight( 1 5 ) + lPerson. LastName) ;
lbPerson. Items. Add( 'Married:' . PadRight( 1 5 ) + lPerson. Married. ToString( TUseBoolStrs. True ) ) ;
lbPerson. Items. Add( 'DOB:' . PadRight( 1 5 ) + DateToStr( lPerson. DOB) ) ;
finally
lPerson. Free;
end ;
end ) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. btnInvalid1Click( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
lReq : = TJSONRPCRequest. Create( 1 2 3 4 ) ;
lReq. Method : = 'invalidmethod1' ;
lReq. Params. Add( 1 ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
procedure( Resp: IJSONRPCResponse)
begin
ShowMessage( Resp. Error. ErrMessage) ;
end ) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. btnInvalid2Click( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
lReq : = TJSONRPCRequest. Create( 1 2 3 4 ) ;
lReq. Method : = 'invalidmethod2' ;
lReq. Params. Add( 1 ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteNotificationAsync( '/jsonrpc' , lReq,
procedure( Exc: Exception)
2024-07-02 19:14:45 +02:00
begin
ShowMessage( Exc. Message ) ;
end ) ;
end ;
procedure TMainForm. btnInvalidMethodClick( Sender: TObject) ;
var
lNotification: IJSONRPCNotification;
begin
lNotification : = TJSONRPCNotification. Create;
lNotification. Method : = 'notexists' ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteNotificationAsync( '/jsonrpc' , lNotification) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. btnNotificationClick( Sender: TObject) ;
var
lNotification: IJSONRPCNotification;
begin
2024-10-10 01:26:34 +02:00
lNotification : = fExecutor. CreateNotification( 'dosomething' ) ;
fExecutor. ExecuteNotificationAsync( '/jsonrpc' , lNotification) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. btnObjDictClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
lResp: IJSONRPCResponse;
lMultiDS: TMultiDataset;
begin
FDMemTable1. Active : = False ;
lReq : = TJSONRPCRequest. Create( Random( 1 0 0 0 ) , 'getobjdict' ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( JSONRPCResponse: IJSONRPCResponse)
begin
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 ) ;
end ;
procedure TMainForm. btnReverseStringClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
2024-10-10 01:26:34 +02:00
lReq : = fExecutor. CreateRequest( 'reversestring' , Random( 1 0 0 0 ) ) ;
2024-07-02 19:14:45 +02:00
lReq. Params. AddByName( 'aString' , edtReverseString. Text ) ;
lReq. Params. AddByName( 'aUpperCase' , CheckBox1. Checked) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
procedure( Resp: IJSONRPCResponse)
begin
edtReversedString. Text : = Resp. Result . AsString;
end ) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. btnSaveClick( Sender: TObject) ;
var
lPerson: TPerson;
lReq: IJSONRPCRequest;
begin
lReq : = TJSONRPCRequest. Create;
lReq. Method : = 'saveperson' ;
lReq. RequestID : = Random( 1 0 0 0 ) ;
lPerson : = TPerson. Create;
lReq. Params. AddByName( 'Person' , lPerson, pdtObject) ;
lPerson. FirstName : = edtFirstName. Text ;
lPerson. LastName : = edtLastName. Text ;
lPerson. Married : = chkMarried. Checked;
lPerson. DOB : = dtDOB. Date;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
ShowMessage( 'Person saved with ID = ' + Resp. Result . AsInteger. ToString) ;
end ) ;
end ;
procedure TMainForm. btnSearchClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
lJSON: TJsonArray;
lJObj: TJsonObject;
begin
ListBox1. Clear;
lReq : = TJSONRPCRequest. Create;
lReq. Method : = 'searchproducts' ;
lReq. RequestID : = 1 2 3 4 ;
lReq. Params. Add( edtSearchText. Text ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/rpcdatamodule' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
var
I: Integer ;
begin
// Remember that TObject descendants (but TDataset, TJDOJSONObject and TJDOJSONArray)
// are serialized as JSON objects
lJSON : = Resp. Result . AsObject as TJsonArray;
for I : = 0 to lJSON. Count - 1 do
begin
lJObj : = lJSON[ I] . ObjectValue;
2024-10-10 01:26:34 +02:00
ListBox1. Items. Add( Format( '%6s: %-34s <20> %5.2f' , [ lJObj. S[ 'codice' ] , lJObj. S[ 'descrizione' ] ,
lJObj. F[ 'prezzo' ] ] ) ) ;
2024-07-02 19:14:45 +02:00
end ;
end ) ;
end ;
procedure TMainForm. btnSingleRecClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
lReq : = TJSONRPCRequest. Create;
lReq. Method : = 'GetPersonRec' ;
lReq. RequestID : = Random( 1 0 0 0 ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
var
lPersonRec: TTestRec;
begin
lPersonRec : = TJSONUtils. JSONObjectToRecord< TTestRec> ( Resp) ;
lbLogRec. Lines. Text : = Resp. ResultAsJSONObject. ToJSON( False ) ;
lbLogRec. Lines. Add( '-- record --' ) ;
lbLogRec. Lines. Add( lPersonRec. ToString) ;
end , fGeneralErrorHandler) ;
end ;
procedure TMainForm. btnSubtractClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
fExecutorAsync : = TMVCJSONRPCExecutor. Create( 'http://localhost:8080' ) ;
2024-07-28 23:54:45 +02:00
lReq : = fExecutorAsync. CreateRequest( 'subtract' , Random( 1 0 0 0 ) ) ;
2024-07-02 19:14:45 +02:00
lReq. Params. Add( StrToInt( edtValue1. Text ) ) ;
lReq. Params. Add( StrToInt( edtValue2. Text ) ) ;
2024-10-10 01:26:34 +02:00
fExecutorAsync. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( JSONRPCResp: IJSONRPCResponse)
begin
edtResult. Text : = JSONRPCResp. Result . AsInteger. ToString;
end ) ;
end ;
procedure TMainForm. btnSubtractWithNamedParamsClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
2024-07-28 23:54:45 +02:00
lReq : = fExecutor. CreateRequest( 'subtract' , Random( 1 0 0 0 ) ) ;
2024-07-02 19:14:45 +02:00
lReq. Params. AddByName( 'Value1' , StrToInt( Edit1. Text ) ) ;
lReq. Params. AddByName( 'Value2' , StrToInt( Edit2. Text ) ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
Edit3. Text : = Resp. Result . AsInteger. ToString;
end ) ;
end ;
procedure TMainForm. btnWithJSONClick( Sender: TObject) ;
var
lPerson: TJsonObject;
lReq: IJSONRPCRequest;
begin
2024-10-10 01:26:34 +02:00
lReq : = fExecutor. CreateRequest( 'SaveObjectWithJSON' , 1 2 3 4 ) ;
2024-07-02 19:14:45 +02:00
lPerson : = TJsonObject. Create;
lReq. Params. Add( lPerson, pdTJDOJsonObject) ;
lPerson. S[ 'StringProp' ] : = 'Hello World' ;
lPerson. O[ 'JSONObject' ] : = TJsonObject. Parse( '{"name":"Daniele"}' ) as TJsonObject;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
lPerson : = Resp. Result . AsObject as TJsonObject;
ShowMessage( lPerson. ToJSON( False ) ) ;
end ) ;
end ;
procedure TMainForm. btnParallelClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
lThreadCount: Int64 ;
Val1, Val2, Val3, Val4: String ;
begin
lReq : = TJSONRPCRequest. Create;
lReq. Method : = 'subtract' ;
lReq. RequestID : = Random( 1 0 0 0 ) ;
lReq. Params. AddByName( 'Value1' , StrToInt( Edit1. Text ) ) ;
lReq. Params. AddByName( 'Value2' , StrToInt( Edit2. Text ) ) ;
lThreadCount : = 4 ;
TThread. CreateAnonymousThread(
procedure
begin
2024-10-10 01:26:34 +02:00
while TInterlocked. Read( lThreadCount) > 0 do
begin
Sleep( 1 0 0 ) ;
end ;
TThread. Queue( nil ,
procedure
begin
ShowMessage( Val1 + sLineBreak + Val2 + sLineBreak + Val3 + sLineBreak + Val4 + sLineBreak) ;
end ) ;
end ) . Start;
2024-07-02 19:14:45 +02:00
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
Val1 : = Resp. Result . AsInteger. ToString;
TInterlocked. Decrement( lThreadCount) ;
end ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
Val2 : = Resp. Result . AsInteger. ToString;
TInterlocked. Decrement( lThreadCount) ;
end ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
Val3 : = Resp. Result . AsInteger. ToString;
TInterlocked. Decrement( lThreadCount) ;
end ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
Val4 : = Resp. Result . AsInteger. ToString;
TInterlocked. Decrement( lThreadCount) ;
end ) ;
end ;
procedure TMainForm. btnPassAndGetRecordClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
lPersonRec: TTestRec;
begin
lReq : = TJSONRPCRequest. Create;
lReq. Method : = 'SavePersonRec' ;
lReq. RequestID : = Random( 1 0 0 0 ) ;
lPersonRec : = TTestRec. Create( 2 ) ;
lReq. Params. Add( TValue. From< TTestRec> ( lPersonRec) , pdtRecordOrArrayOfRecord) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
procedure( Resp: IJSONRPCResponse)
2024-07-02 19:14:45 +02:00
var
lResPersonRec: TTestRec;
begin
lResPersonRec : = TJSONUtils. JSONObjectToRecord< TTestRec> ( Resp) ;
lbLogRec. Lines. Text : = Resp. ResultAsJSONObject. ToJSON( False ) ;
end ) ;
end ;
procedure TMainForm. btnGenericExceptionClick( Sender: TObject) ;
var
lReq: IJSONRPCNotification;
begin
ShowMessage( 'Now will be raised a EDivByZero exception on the server. This exception will be catched by the client' ) ;
lReq : = TJSONRPCRequest. Create( 1 2 3 4 , 'RaiseGenericException' ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteNotificationAsync( '/jsonrpc' , lReq) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. btnGenericExcWithCustomHAndling2Click( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
ShowMessage
( 'Now will be raised a EInvalidPointerOperation exception on the server. However this exception will be handled by a custom exception handler wich will add a data property with extra data' ) ;
lReq : = TJSONRPCRequest. Create( 1 2 3 4 , 'RaiseGenericException' ) ;
lReq. Params. Add( 2 ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpcex' , lReq, nil ) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. btnGenericExcWithCustomHandlingClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
ShowMessage
( 'Now will be raised a EDivByZero exception on the server. However this exception will be handled by a custom exception handler wich will add a data property with extra data' ) ;
lReq : = TJSONRPCRequest. Create( 1 2 3 4 , 'RaiseGenericException' ) ;
lReq. Params. Add( 1 ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpcex' , lReq, nil ) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. btnGenericExcWithoutCustomHandlingClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
ShowMessage( 'Now will be raised a Exception exception on the server.' ) ;
lReq : = TJSONRPCRequest. Create( 1 2 3 4 , 'RaiseGenericException' ) ;
lReq. Params. Add( 9 9 ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpcex' , lReq, nil , fGeneralErrorHandler) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. btnGetArrayOfRecordsClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
lPeopleRec: TArray< TTestRec> ; // server serializes a static array, we read it as dynarray
I: Integer ;
begin
lReq : = TJSONRPCRequest. Create;
lReq. Method : = 'GetPeopleRecStaticArray' ;
lReq. RequestID : = Random( 1 0 0 0 ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
lPeopleRec : = TJSONUtils. JSONArrayToArrayOfRecord< TTestRec> ( Resp) ;
lbLogRec. Lines. Text : = Resp. ResultAsJSONArray. ToJSON( False ) ;
lbLogRec. Lines. Add( '-- array of record elements --' ) ;
I : = 1 ;
for var lPRec in lPeopleRec do
begin
lbLogRec. Lines. Add( 'ITEM : ' + I. ToString) ;
lbLogRec. Lines. Add( lPRec. ToString) ;
Inc( I) ;
end ;
end , fGeneralErrorHandler) ;
end ;
procedure TMainForm. btnGetDynArrayClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
lReq : = TJSONRPCRequest. Create;
lReq. Method : = 'GetPeopleRecDynArray' ;
lReq. RequestID : = Random( 1 0 0 0 ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
var
2024-10-10 01:26:34 +02:00
lPeopleRec: TArray< TTestRec> ;
2024-07-02 19:14:45 +02:00
begin
lPeopleRec : = TJSONUtils. JSONArrayToArrayOfRecord< TTestRec> ( Resp) ;
lbLogRec. Lines. Text : = Resp. ResultAsJSONArray. ToJSON( False ) ;
end , fGeneralErrorHandler) ;
end ;
procedure TMainForm. btnGetListOfDatasetClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
FDMemTable1. Active : = False ;
lReq : = TJSONRPCRequest. Create( Random( 1 0 0 0 ) , 'GetDataSetList' ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
var
lMultiDS: TObjectList< TDataSet> ;
begin
lMultiDS : = TObjectList< TDataSet> . Create( True ) ;
try
JsonArrayToList( Resp. ResultAsJSONArray, WrapAsList( lMultiDS) , TDataSet, TMVCSerializationType. stDefault, nil ) ;
finally
lMultiDS. Free;
end ;
end ) ;
end ;
procedure TMainForm. btnGetMultiClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
FDMemTable1. Active : = False ;
lReq : = TJSONRPCRequest. Create( Random( 1 0 0 0 ) , 'getmulti' ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
var
lMultiDS: TMultiDataset;
begin
lMultiDS : = TMultiDataset. Create;
try
JsonObjectToObject( Resp. 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 ;
2024-10-10 01:26:34 +02:00
end , nil , jrpcPOST) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. edtGetCustomersClick( Sender: TObject) ;
var
lReq: IJSONRPCRequest;
begin
FDMemTable1. Active : = False ;
lReq : = TJSONRPCRequest. Create( Random( 1 0 0 0 ) , 'getcustomers' ) ;
lReq. Params. AddByName( 'FilterString' , edtFilter. Text ) ;
2024-10-10 01:26:34 +02:00
fExecutor. ExecuteRequestAsync( '/jsonrpc' , lReq,
2024-07-02 19:14:45 +02:00
procedure( Resp: IJSONRPCResponse)
begin
FDMemTable1. Active : = True ;
2024-10-10 00:13:07 +02:00
FDMemTable1. LoadFromJSONRPCResponse( Resp) ;
2024-07-02 19:14:45 +02:00
end ,
procedure( Exc: Exception)
begin
ShowMessage( Exc. ClassName + ': ' + Exc. Message ) ;
2024-10-10 01:26:34 +02:00
end , jrpcPOST) ;
2024-07-02 19:14:45 +02:00
end ;
procedure TMainForm. FormCreate( Sender: TObject) ;
const
SIMULATE_SLOW_NETWORK = False ;
begin
2024-10-10 01:26:34 +02:00
fExecutor : = TMVCJSONRPCExecutor. Create( 'http://localhost:8080' ) ;
2024-07-02 19:14:45 +02:00
2024-10-10 01:26:34 +02:00
fExecutor. SetOnSendCommandAsync(
2024-07-02 19:14:45 +02:00
procedure( JSONRPCObject: IJSONRPCObject)
begin
if SIMULATE_SLOW_NETWORK then
begin
Sleep( 1 0 0 0 + Random( 3 0 0 0 ) ) ;
end ;
Log. Debug( 'REQUEST : ' + JSONRPCObject. ToString( True ) , 'jsonrpc' ) ;
2024-10-10 01:26:34 +02:00
end ) ;
fExecutorAsync : = TMVCJSONRPCExecutor. Create( 'http://localhost:8080' ) ;
fExecutorAsync. SetOnReceiveResponseAsync(
2024-07-02 19:14:45 +02:00
procedure( Req, Resp: IJSONRPCObject)
begin
Log. Debug( '>> OnReceiveResponse // start' , 'jsonrpc' ) ;
Log. Debug( ' REQUEST : ' + Req. ToString( True ) , 'jsonrpc' ) ;
Log. Debug( ' RESPONSE: ' + Resp. ToString( True ) , 'jsonrpc' ) ;
Log. Debug( '<< OnReceiveResponse // end' , 'jsonrpc' ) ;
2024-10-10 00:13:07 +02:00
end )
2024-10-10 01:26:34 +02:00
. SetOnReceiveHTTPResponseAsync(
2024-07-02 19:14:45 +02:00
procedure( HTTPResp: IHTTPResponse)
begin
Log. Debug( 'RESPONSE: ' + HTTPResp. ContentAsString( ) , 'jsonrpc' ) ;
2024-10-10 00:13:07 +02:00
end )
2024-10-10 01:26:34 +02:00
. SetConfigureHTTPClientAsync(
procedure( HttpClient: THTTPClient)
2024-10-10 00:13:07 +02:00
begin
2024-10-10 01:26:34 +02:00
HttpClient. ResponseTimeout : = 2 0 0 0 0 ;
HttpClient. CustomHeaders[ 'X-DMVCFRAMEWORK' ] : = 'DMVCFRAMEWORK_VERSION ' + DMVCFRAMEWORK_VERSION;
2024-07-02 19:14:45 +02:00
end ) ;
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
2024-10-10 01:26:34 +02:00
Assert( fExecutor. HTTPHeadersCount = 0 ) ;
fExecutor. AddHTTPHeader( TNetHeader. Create( 'x-token' , TGUID. NewGuid. ToString) ) ;
Assert( fExecutor. HTTPHeadersCount = 1 ) ;
fExecutor. ClearHTTPHeaders;
Assert( fExecutor. HTTPHeadersCount = 0 ) ;
fExecutor. AddHTTPHeader( TNetHeader. Create( 'x-token' , TGUID. NewGuid. ToString) ) ;
2024-07-02 19:14:45 +02:00
PageControl1. ActivePageIndex : = 0 ;
fGeneralErrorHandler : = procedure( Exc: Exception)
begin
ShowMessage( Exc. ClassName + ': ' + Exc. Message ) ;
end ;
fWaiting : = TWaitingForm. Create( Self) ;
fWaiting. PopupParent : = Self;
2024-10-10 01:26:34 +02:00
fExecutor. SetOnBeginAsyncRequest(
2024-07-02 19:14:45 +02:00
procedure
begin
fWaiting. IncreaseWaitingCount;
end ) ;
2024-10-10 01:26:34 +02:00
fExecutor. SetOnEndAsyncRequest(
2024-07-02 19:14:45 +02:00
procedure
begin
fWaiting. DecreaseWaitingCount;
end ) ;
end ;
end .