Added CreateRequest and CreateNotification methods on JSONRPCExecutor

This commit is contained in:
Daniele Teti 2024-07-28 23:54:45 +02:00
parent 7e55bfae86
commit d932857f34
12 changed files with 86 additions and 73 deletions

View File

@ -384,8 +384,7 @@ procedure TMainForm.btnNotificationClick(Sender: TObject);
var
lNotification: IJSONRPCNotification;
begin
lNotification := TJSONRPCNotification.Create;
lNotification.Method := 'dosomething';
lNotification := FExecutor.CreateNotification('dosomething');
FExecutor.ExecuteNotificationAsync('/jsonrpc', lNotification);
end;
@ -433,9 +432,7 @@ procedure TMainForm.btnReverseStringClick(Sender: TObject);
var
lReq: IJSONRPCRequest;
begin
lReq := TJSONRPCRequest.Create;
lReq.Method := 'reversestring';
lReq.RequestID := Random(1000);
lReq := FExecutor.CreateRequest('reversestring', Random(1000));
lReq.Params.AddByName('aString', edtReverseString.Text);
lReq.Params.AddByName('aUpperCase', CheckBox1.Checked);
FExecutor.ExecuteRequestAsync('/jsonrpc', lReq,
@ -517,9 +514,7 @@ var
lReq: IJSONRPCRequest;
begin
fExecutorAsync := TMVCJSONRPCExecutor.Create('http://localhost:8080');
lReq := TJSONRPCRequest.Create;
lReq.Method := 'subtract';
lReq.RequestID := Random(1000);
lReq := fExecutorAsync.CreateRequest('subtract', Random(1000));
lReq.Params.Add(StrToInt(edtValue1.Text));
lReq.Params.Add(StrToInt(edtValue2.Text));
fExecutorAsync
@ -534,9 +529,7 @@ procedure TMainForm.btnSubtractWithNamedParamsClick(Sender: TObject);
var
lReq: IJSONRPCRequest;
begin
lReq := TJSONRPCRequest.Create;
lReq.Method := 'subtract';
lReq.RequestID := Random(1000);
lReq := fExecutor.CreateRequest('subtract', Random(1000));
lReq.Params.AddByName('Value1', StrToInt(Edit1.Text));
lReq.Params.AddByName('Value2', StrToInt(Edit2.Text));
FExecutor.ExecuteRequestAsync('/jsonrpc', lReq,
@ -551,9 +544,7 @@ var
lPerson: TJsonObject;
lReq: IJSONRPCRequest;
begin
lReq := TJSONRPCRequest.Create;
lReq.Method := 'SaveObjectWithJSON';
lReq.RequestID := 1234;
lReq := FExecutor.CreateRequest('SaveObjectWithJSON', 1234);
lPerson := TJsonObject.Create;
lReq.Params.Add(lPerson, pdTJDOJsonObject);
lPerson.S['StringProp'] := 'Hello World';

View File

@ -1,4 +1,4 @@
program jsonrpcclientwithobjects_async;
program jsonrpcclient_async;
uses
Vcl.Forms,

View File

@ -8,8 +8,8 @@
<Platform Condition="'$(Platform)'==''">Win32</Platform>
<TargetedPlatforms>1</TargetedPlatforms>
<AppType>Application</AppType>
<MainSource>jsonrpcclientwithobjects_async.dpr</MainSource>
<ProjectName Condition="'$(ProjectName)'==''">jsonrpcclientwithobjects_async</ProjectName>
<MainSource>jsonrpcclient_async.dpr</MainSource>
<ProjectName Condition="'$(ProjectName)'==''">jsonrpcclient_async</ProjectName>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
<Base>true</Base>
@ -58,7 +58,7 @@
<Icon_MainIcon>$(BDS)\bin\delphi_PROJECTICON.ico</Icon_MainIcon>
<UWP_DelphiLogo44>$(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png</UWP_DelphiLogo44>
<UWP_DelphiLogo150>$(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png</UWP_DelphiLogo150>
<SanitizedProjectName>jsonrpcclientwithobjects_async</SanitizedProjectName>
<SanitizedProjectName>jsonrpcclient_async</SanitizedProjectName>
<VerInfo_Locale>1040</VerInfo_Locale>
<VerInfo_Keys>CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
</PropertyGroup>
@ -132,7 +132,7 @@
<BorlandProject>
<Delphi.Personality>
<Source>
<Source Name="MainSource">jsonrpcclientwithobjects_async.dpr</Source>
<Source Name="MainSource">jsonrpcclient_async.dpr</Source>
</Source>
<Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dcloffice2k280.bpl">Microsoft Office 2000 Sample Automation Server Wrapper Components</Excluded_Packages>
@ -140,9 +140,9 @@
</Excluded_Packages>
</Delphi.Personality>
<Deployment Version="4">
<DeployFile LocalName="Win32\Debug\jsonrpcclientwithobjects_async.exe" Configuration="Debug" Class="ProjectOutput">
<DeployFile LocalName="..\bin\jsonrpcclient_async.exe" Configuration="Debug" Class="ProjectOutput">
<Platform Name="Win32">
<RemoteName>jsonrpcclientwithobjects_async.exe</RemoteName>
<RemoteName>jsonrpcclient_async.exe</RemoteName>
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>

View File

@ -3,13 +3,13 @@
<ProjectGuid>{AFDF54C5-5184-4A5F-A230-FB7F37B3B2F0}</ProjectGuid>
</PropertyGroup>
<ItemGroup>
<Projects Include="jsonrpcserver\jsonrpcserverwithobjects.dproj">
<Projects Include="jsonrpcserver\jsonrpcserver.dproj">
<Dependencies/>
</Projects>
<Projects Include="sync_client\jsonrpcclientwithobjects_sync.dproj">
<Projects Include="sync_client\jsonrpcclient_sync.dproj">
<Dependencies/>
</Projects>
<Projects Include="async_client\jsonrpcclientwithobjects_async.dproj">
<Projects Include="async_client\jsonrpcclient_async.dproj">
<Dependencies/>
</Projects>
</ItemGroup>
@ -20,41 +20,41 @@
<Default.Personality/>
</BorlandProject>
</ProjectExtensions>
<Target Name="jsonrpcserverwithobjects">
<MSBuild Projects="jsonrpcserver\jsonrpcserverwithobjects.dproj"/>
<Target Name="jsonrpcserver">
<MSBuild Projects="jsonrpcserver\jsonrpcserver.dproj"/>
</Target>
<Target Name="jsonrpcserverwithobjects:Clean">
<MSBuild Projects="jsonrpcserver\jsonrpcserverwithobjects.dproj" Targets="Clean"/>
<Target Name="jsonrpcserver:Clean">
<MSBuild Projects="jsonrpcserver\jsonrpcserver.dproj" Targets="Clean"/>
</Target>
<Target Name="jsonrpcserverwithobjects:Make">
<MSBuild Projects="jsonrpcserver\jsonrpcserverwithobjects.dproj" Targets="Make"/>
<Target Name="jsonrpcserver:Make">
<MSBuild Projects="jsonrpcserver\jsonrpcserver.dproj" Targets="Make"/>
</Target>
<Target Name="jsonrpcclientwithobjects_sync">
<MSBuild Projects="sync_client\jsonrpcclientwithobjects_sync.dproj"/>
<Target Name="jsonrpcclient_sync">
<MSBuild Projects="sync_client\jsonrpcclient_sync.dproj"/>
</Target>
<Target Name="jsonrpcclientwithobjects_sync:Clean">
<MSBuild Projects="sync_client\jsonrpcclientwithobjects_sync.dproj" Targets="Clean"/>
<Target Name="jsonrpcclient_sync:Clean">
<MSBuild Projects="sync_client\jsonrpcclient_sync.dproj" Targets="Clean"/>
</Target>
<Target Name="jsonrpcclientwithobjects_sync:Make">
<MSBuild Projects="sync_client\jsonrpcclientwithobjects_sync.dproj" Targets="Make"/>
<Target Name="jsonrpcclient_sync:Make">
<MSBuild Projects="sync_client\jsonrpcclient_sync.dproj" Targets="Make"/>
</Target>
<Target Name="jsonrpcclientwithobjects_async">
<MSBuild Projects="async_client\jsonrpcclientwithobjects_async.dproj"/>
<Target Name="jsonrpcclient_async">
<MSBuild Projects="async_client\jsonrpcclient_async.dproj"/>
</Target>
<Target Name="jsonrpcclientwithobjects_async:Clean">
<MSBuild Projects="async_client\jsonrpcclientwithobjects_async.dproj" Targets="Clean"/>
<Target Name="jsonrpcclient_async:Clean">
<MSBuild Projects="async_client\jsonrpcclient_async.dproj" Targets="Clean"/>
</Target>
<Target Name="jsonrpcclientwithobjects_async:Make">
<MSBuild Projects="async_client\jsonrpcclientwithobjects_async.dproj" Targets="Make"/>
<Target Name="jsonrpcclient_async:Make">
<MSBuild Projects="async_client\jsonrpcclient_async.dproj" Targets="Make"/>
</Target>
<Target Name="Build">
<CallTarget Targets="jsonrpcserverwithobjects;jsonrpcclientwithobjects_sync;jsonrpcclientwithobjects_async"/>
<CallTarget Targets="jsonrpcserver;jsonrpcclient_sync;jsonrpcclient_async"/>
</Target>
<Target Name="Clean">
<CallTarget Targets="jsonrpcserverwithobjects:Clean;jsonrpcclientwithobjects_sync:Clean;jsonrpcclientwithobjects_async:Clean"/>
<CallTarget Targets="jsonrpcserver:Clean;jsonrpcclient_sync:Clean;jsonrpcclient_async:Clean"/>
</Target>
<Target Name="Make">
<CallTarget Targets="jsonrpcserverwithobjects:Make;jsonrpcclientwithobjects_sync:Make;jsonrpcclientwithobjects_async:Make"/>
<CallTarget Targets="jsonrpcserver:Make;jsonrpcclient_sync:Make;jsonrpcclient_async:Make"/>
</Target>
<Import Project="$(BDS)\Bin\CodeGear.Group.Targets" Condition="Exists('$(BDS)\Bin\CodeGear.Group.Targets')"/>
</Project>

View File

@ -411,21 +411,21 @@ end;
procedure TMyObject.OnBeforeCallHook(const Context: TWebContext; const JSONRequest: TJDOJsonObject);
begin
Log.Info('TMyObjectWithHooks.OnBeforeCallHook >> ', 'jsonrpc');
Log.Info(JSONRequest.ToJSON(False), 'jsonrpc');
Log.Info(sLineBreak + JSONRequest.ToJSON(False), 'jsonrpc');
Log.Info('TMyObjectWithHooks.OnBeforeCallHook << ', 'jsonrpc');
end;
procedure TMyObject.OnBeforeRoutingHook(const Context: TWebContext; const JSON: TJDOJsonObject);
begin
Log.Info('TMyObjectWithHooks.OnBeforeRoutingHook >> ', 'jsonrpc');
Log.Info(JSON.ToJSON(False), 'jsonrpc');
Log.Info(sLineBreak + JSON.ToJSON(False), 'jsonrpc');
Log.Info('TMyObjectWithHooks.OnBeforeRoutingHook << ', 'jsonrpc');
end;
procedure TMyObject.OnAfterCallHook(const Context: TWebContext; const JSONResponse: TJDOJsonObject);
begin
Log.Info('TMyObjectWithHooks.OnAfterCallHook >> ', 'jsonrpc');
Log.Info(JSONResponse.ToJSON(False), 'jsonrpc');
Log.Info(sLineBreak + JSONResponse.ToJSON(False), 'jsonrpc');
Log.Info('TMyObjectWithHooks.OnAfterCallHook << ', 'jsonrpc');
end;

View File

@ -1,4 +1,4 @@
program jsonrpcserverwithobjects;
program jsonrpcserver;
{$APPTYPE CONSOLE}

View File

@ -3,13 +3,13 @@
<ProjectGuid>{AF5FBC36-0D1D-4C07-B2E3-C2A2E688AC6F}</ProjectGuid>
<ProjectVersion>20.1</ProjectVersion>
<FrameworkType>VCL</FrameworkType>
<MainSource>jsonrpcserverwithobjects.dpr</MainSource>
<MainSource>jsonrpcserver.dpr</MainSource>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
<Platform Condition="'$(Platform)'==''">Win32</Platform>
<TargetedPlatforms>1</TargetedPlatforms>
<AppType>Console</AppType>
<ProjectName Condition="'$(ProjectName)'==''">jsonrpcserverwithobjects</ProjectName>
<ProjectName Condition="'$(ProjectName)'==''">jsonrpcserver</ProjectName>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
<Base>true</Base>
@ -60,7 +60,7 @@
<Icns_MainIcns>$(BDS)\bin\delphi_PROJECTICNS.icns</Icns_MainIcns>
<DCC_UnitSearchPath>$(DMVC);$(DCC_UnitSearchPath)</DCC_UnitSearchPath>
<DCC_Framework>VCL;$(DCC_Framework)</DCC_Framework>
<SanitizedProjectName>jsonrpcserverwithobjects</SanitizedProjectName>
<SanitizedProjectName>jsonrpcserver</SanitizedProjectName>
<VerInfo_Locale>1040</VerInfo_Locale>
<DCC_ExeOutput>..\bin</DCC_ExeOutput>
<VerInfo_Keys>CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
@ -136,7 +136,7 @@
<BorlandProject>
<Delphi.Personality>
<Source>
<Source Name="MainSource">jsonrpcserverwithobjects.dpr</Source>
<Source Name="MainSource">jsonrpcserver.dpr</Source>
</Source>
<Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dcloffice2k280.bpl">Microsoft Office 2000 Sample Automation Server Wrapper Components</Excluded_Packages>
@ -148,9 +148,9 @@
<DeployFile LocalName="$(BDS)\Redist\iossimulator\libcgunwind.1.0.dylib" Class="DependencyModule"/>
<DeployFile LocalName="$(BDS)\Redist\osx32\libcgsqlite3.dylib" Class="DependencyModule"/>
<DeployFile LocalName="$(BDS)\Redist\osx32\libcgunwind.1.0.dylib" Class="DependencyModule"/>
<DeployFile LocalName="..\bin\jsonrpcserverwithobjects.exe" Configuration="Debug" Class="ProjectOutput">
<DeployFile LocalName="..\bin\jsonrpcserver.exe" Configuration="Debug" Class="ProjectOutput">
<Platform Name="Win32">
<RemoteName>jsonrpcserverwithobjects.exe</RemoteName>
<RemoteName>jsonrpcserver.exe</RemoteName>
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>

View File

@ -20,8 +20,6 @@ object MainForm: TMainForm
ActivePage = TabSheet1
Align = alClient
TabOrder = 0
ExplicitWidth = 838
ExplicitHeight = 603
object TabSheet1: TTabSheet
Caption = 'Invoking Plain PODO'
object GroupBox1: TGroupBox
@ -484,7 +482,7 @@ object MainForm: TMainForm
AlignWithMargins = True
Left = 3
Top = 3
Width = 808
Width = 828
Height = 69
Align = alTop
Caption =
@ -499,6 +497,7 @@ object MainForm: TMainForm
Font.Style = []
ParentFont = False
WordWrap = True
ExplicitWidth = 808
end
object btnGenericExcWithCustomHandling: TButton
Left = 0

View File

@ -2,7 +2,7 @@
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2023 Daniele Teti and the DMVCFramework Team
// Copyright (c) 2010-2024 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//
@ -357,8 +357,7 @@ procedure TMainForm.btnNotificationClick(Sender: TObject);
var
lNotification: IJSONRPCNotification;
begin
lNotification := TJSONRPCNotification.Create;
lNotification.Method := 'dosomething';
lNotification := FExecutor.CreateNotification('dosomething');
FExecutor.ExecuteNotification('/jsonrpc', lNotification);
end;
@ -500,9 +499,7 @@ var
lExecutor: IMVCJSONRPCExecutor;
begin
lExecutor := TMVCJSONRPCExecutor.Create('http://localhost:8080');
lReq := TJSONRPCRequest.Create;
lReq.Method := 'subtract';
lReq.RequestID := Random(1000);
lReq := lExecutor.CreateRequest('subtract', Random(1000));
lReq.Params.Add(StrToInt(edtValue1.Text));
lReq.Params.Add(StrToInt(edtValue2.Text));
lResp := lExecutor.ExecuteRequest('/jsonrpc', lReq);

View File

@ -1,4 +1,4 @@
program jsonrpcclientwithobjects_sync;
program jsonrpcclient_sync;
uses
Vcl.Forms,

View File

@ -3,13 +3,13 @@
<ProjectGuid>{300F83FF-8F7B-43FD-B740-A3DFDF7238ED}</ProjectGuid>
<ProjectVersion>20.1</ProjectVersion>
<FrameworkType>VCL</FrameworkType>
<MainSource>jsonrpcclientwithobjects_sync.dpr</MainSource>
<MainSource>jsonrpcclient_sync.dpr</MainSource>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
<Platform Condition="'$(Platform)'==''">Win32</Platform>
<TargetedPlatforms>1</TargetedPlatforms>
<AppType>Application</AppType>
<ProjectName Condition="'$(ProjectName)'==''">jsonrpcclientwithobjects_sync</ProjectName>
<ProjectName Condition="'$(ProjectName)'==''">jsonrpcclient_sync</ProjectName>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
<Base>true</Base>
@ -59,7 +59,7 @@
<Icon_MainIcon>$(BDS)\bin\delphi_PROJECTICON.ico</Icon_MainIcon>
<UWP_DelphiLogo44>$(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png</UWP_DelphiLogo44>
<UWP_DelphiLogo150>$(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png</UWP_DelphiLogo150>
<SanitizedProjectName>jsonrpcclientwithobjects_sync</SanitizedProjectName>
<SanitizedProjectName>jsonrpcclient_sync</SanitizedProjectName>
<VerInfo_Locale>1040</VerInfo_Locale>
<VerInfo_Keys>CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
</PropertyGroup>
@ -129,7 +129,7 @@
<BorlandProject>
<Delphi.Personality>
<Source>
<Source Name="MainSource">jsonrpcclientwithobjects_sync.dpr</Source>
<Source Name="MainSource">jsonrpcclient_sync.dpr</Source>
</Source>
<Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dcloffice2k280.bpl">Microsoft Office 2000 Sample Automation Server Wrapper Components</Excluded_Packages>
@ -137,13 +137,13 @@
</Excluded_Packages>
</Delphi.Personality>
<Deployment Version="4">
<DeployFile LocalName="Win32\Debug\jsonrpcclientwithobjects.exe" Configuration="Debug" Class="ProjectOutput"/>
<DeployFile LocalName="bin\jsonrpcclientwithobjects_sync.exe" Configuration="Debug" Class="ProjectOutput">
<DeployFile LocalName="..\bin\jsonrpcclient_sync.exe" Configuration="Debug" Class="ProjectOutput">
<Platform Name="Win32">
<RemoteName>jsonrpcclientwithobjects_sync.exe</RemoteName>
<RemoteName>jsonrpcclient_sync.exe</RemoteName>
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployFile LocalName="Win32\Debug\jsonrpcclientwithobjects.exe" Configuration="Debug" Class="ProjectOutput"/>
<DeployClass Name="AdditionalDebugSymbols">
<Platform Name="iOSSimulator">
<Operation>1</Operation>

View File

@ -74,7 +74,9 @@ type
function SetOnSendCommand(const aOnSendCommandProc: TProc<IJSONRPCObject>): IMVCJSONRPCExecutor;
function SetOnReceiveHTTPResponse(const aOnReceiveHTTPResponse: TProc<IHTTPResponse>): IMVCJSONRPCExecutor;
//end events
function CreateRequest(const MethodName: String; const RequestID: UInt64): IJSONRPCRequest; overload;
function CreateRequest(const MethodName: String; const RequestID: String): IJSONRPCRequest; overload;
function CreateNotification(const MethodName: String): IJSONRPCNotification;
end;
IMVCJSONRPCExecutorAsync = interface
@ -110,6 +112,9 @@ type
function SetOnValidateServerCertificate(const aOnValidateServerCertificate: TValidateCertificateEvent)
: IMVCJSONRPCExecutor;
//events
function CreateRequest(const MethodName: String; const RequestID: UInt64): IJSONRPCRequest; overload;
function CreateRequest(const MethodName: String; const RequestID: String): IJSONRPCRequest; overload;
function CreateNotification(const MethodName: String): IJSONRPCNotification;
//async
function SetOnReceiveResponseAsync(const aOnReceiveResponseAsyncProc: TProc<IJSONRPCObject, IJSONRPCObject>)
: IMVCJSONRPCExecutor;
@ -205,6 +210,9 @@ type
procedure AddHTTPHeader(const aNetHeader: TNetHeader);
procedure ClearHTTPHeaders;
function HTTPHeadersCount: Integer;
function CreateRequest(const MethodName: String; const RequestID: UInt64): IJSONRPCRequest; overload;
function CreateRequest(const MethodName: String; const RequestID: String): IJSONRPCRequest; overload;
function CreateNotification(const MethodName: String): IJSONRPCNotification;
//events
//sync
function SetOnReceiveData(const aOnReceiveData: TReceiveDataEvent): IMVCJSONRPCExecutor;
@ -303,6 +311,24 @@ begin
.SetOnValidateServerCertificate(nil);
end;
function TMVCJSONRPCExecutor.CreateNotification(
const MethodName: String): IJSONRPCNotification;
begin
Result := TJSONRPCNotification.Create(MethodName);
end;
function TMVCJSONRPCExecutor.CreateRequest(const MethodName,
RequestID: String): IJSONRPCRequest;
begin
Result := TJSONRPCRequest.Create(RequestID, MethodName);
end;
function TMVCJSONRPCExecutor.CreateRequest(const MethodName: String;
const RequestID: UInt64): IJSONRPCRequest;
begin
Result := TJSONRPCRequest.Create(RequestID, MethodName);
end;
destructor TMVCJSONRPCExecutor.Destroy;
begin
fHTTP.Free;