Skip to content

Commit 4e2cc96

Browse files
committed
Small refactoring to JSONRPC classes
1 parent bc06528 commit 4e2cc96

File tree

8 files changed

+121
-69
lines changed

8 files changed

+121
-69
lines changed

samples/jsonrpc/async_client/MainClientFormU.pas

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,24 @@ procedure TMainForm.btnGetUserClick(Sender: TObject);
328328
lReq.Params.Add(edtUserName.Text);
329329
FExecutor.ExecuteRequestAsync('/jsonrpc', lReq,
330330
procedure(Resp: IJSONRPCResponse)
331-
var
332-
lJSON: TJsonObject;
333331
begin
334332
// Remember that TObject descendants (but TDataset, TJDOJSONObject and TJDOJSONArray)
335-
// are serialized as JSON objects
336-
lJSON := Resp.Result.AsObject as TJsonObject;
337-
lbPerson.Items.Add('First Name:'.PadRight(15) + lJSON.S['firstname']);
338-
lbPerson.Items.Add('Last Name:'.PadRight(15) + lJSON.S['lastname']);
339-
lbPerson.Items.Add('Married:'.PadRight(15) + lJSON.B['married'].ToString(TUseBoolStrs.True));
340-
lbPerson.Items.Add('DOB:'.PadRight(15) + DateToStr(lJSON.D['dob']));
333+
// are serialized as JSON objects, so you can always read the JSON object
334+
// lJSON := Resp.Result.AsObject as TJsonObject;
335+
// lbPerson.Items.Add('First Name:'.PadRight(15) + lJSON.S['firstname']);
336+
// lbPerson.Items.Add('Last Name:'.PadRight(15) + lJSON.S['lastname']);
337+
// lbPerson.Items.Add('Married:'.PadRight(15) + lJSON.B['married'].ToString(TUseBoolStrs.True));
338+
// lbPerson.Items.Add('DOB:'.PadRight(15) + DateToStr(lJSON.D['dob']));
339+
var lPerson := TPerson.Create;
340+
try
341+
Resp.ResultAs(lPerson);
342+
lbPerson.Items.Add('First Name:'.PadRight(15) + lPerson.FirstName);
343+
lbPerson.Items.Add('Last Name:'.PadRight(15) + lPerson.LastName);
344+
lbPerson.Items.Add('Married:'.PadRight(15) + lPerson.Married.ToString(TUseBoolStrs.True));
345+
lbPerson.Items.Add('DOB:'.PadRight(15) + DateToStr(lPerson.DOB));
346+
finally
347+
lPerson.Free;
348+
end;
341349
end);
342350
end;
343351

@@ -790,8 +798,7 @@ procedure TMainForm.edtGetCustomersClick(Sender: TObject);
790798
procedure(Resp: IJSONRPCResponse)
791799
begin
792800
FDMemTable1.Active := True;
793-
FDMemTable1.LoadFromTValue(Resp.Result);
794-
FDMemTable1.First;
801+
FDMemTable1.LoadFromJSONRPCResponse(Resp);
795802
end,
796803
procedure(Exc: Exception)
797804
begin
@@ -814,32 +821,27 @@ procedure TMainForm.FormCreate(Sender: TObject);
814821
Sleep(1000 + Random(3000));
815822
end;
816823
Log.Debug('REQUEST : ' + JSONRPCObject.ToString(True), 'jsonrpc');
817-
end);
818-
819-
FExecutor.SetOnReceiveResponseAsync(
824+
end)
825+
.SetOnReceiveResponseAsync(
820826
procedure(Req, Resp: IJSONRPCObject)
821827
begin
822828
Log.Debug('>> OnReceiveResponse // start', 'jsonrpc');
823829
Log.Debug(' REQUEST : ' + Req.ToString(True), 'jsonrpc');
824830
Log.Debug(' RESPONSE: ' + Resp.ToString(True), 'jsonrpc');
825831
Log.Debug('<< OnReceiveResponse // end', 'jsonrpc');
826-
end);
827-
828-
FExecutor.SetOnReceiveHTTPResponseAsync(
832+
end)
833+
.SetOnReceiveHTTPResponseAsync(
829834
procedure(HTTPResp: IHTTPResponse)
830835
begin
831836
Log.Debug('RESPONSE: ' + HTTPResp.ContentAsString(), 'jsonrpc');
837+
end)
838+
.SetConfigureHTTPClientAsync(
839+
procedure (HTTPClient: THTTPClient)
840+
begin
841+
HTTPClient.ResponseTimeout := 20000;
842+
HTTPClient.CustomHeaders['X-DMVCFRAMEWORK'] := 'DMVCFRAMEWORK_VERSION ' + DMVCFRAMEWORK_VERSION;
832843
end);
833844

834-
835-
FExecutor.SetConfigureHTTPClientAsync(
836-
procedure (HTTPClient: THTTPClient)
837-
begin
838-
HTTPClient.ResponseTimeout := 20000;
839-
HTTPClient.CustomHeaders['X-DMVCFRAMEWORK'] := 'DMVCFRAMEWORK_VERSION ' + DMVCFRAMEWORK_VERSION;
840-
end);
841-
842-
843845
dtNextMonday.Date := Date;
844846
// these are the methods to handle http headers in JSONRPC
845847
// the following line and the check on the server is just for demo

samples/jsonrpc/async_client/jsonrpcclient_async.dpr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ uses
44
Vcl.Forms,
55
MainClientFormU in 'MainClientFormU.pas' {MainForm},
66
RandomUtilsU in '..\..\commons\RandomUtilsU.pas',
7-
BusinessObjectsU in '..\..\commons\BusinessObjectsU.pas',
87
CommonTypesU in '..\CommonTypesU.pas',
9-
WaitingFormU in 'WaitingFormU.pas' {WaitingForm};
8+
WaitingFormU in 'WaitingFormU.pas' {WaitingForm},
9+
BusinessObjectsU in '..\..\commons\BusinessObjectsU.pas';
1010

1111
{$R *.res}
1212

samples/jsonrpc/async_client/jsonrpcclient_async.dproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@
108108
<FormType>dfm</FormType>
109109
</DCCReference>
110110
<DCCReference Include="..\..\commons\RandomUtilsU.pas"/>
111-
<DCCReference Include="..\..\commons\BusinessObjectsU.pas"/>
112111
<DCCReference Include="..\CommonTypesU.pas"/>
113112
<DCCReference Include="WaitingFormU.pas">
114113
<Form>WaitingForm</Form>
115114
<FormType>dfm</FormType>
116115
</DCCReference>
116+
<DCCReference Include="..\..\commons\BusinessObjectsU.pas"/>
117117
<BuildConfiguration Include="Base">
118118
<Key>Base</Key>
119119
</BuildConfiguration>

samples/jsonrpc/jsonrpcserver/MyObjectU.pas

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ interface
3333
BusinessObjectsU,
3434
FireDAC.Comp.Client,
3535
MVCFramework.Serializer.Commons,
36-
MVCFramework.Commons, MVCFramework, MVCFramework.JSONRPC, CommonTypesU;
36+
MVCFramework.Commons, MVCFramework,
37+
MVCFramework.JSONRPC, CommonTypesU;
3738

3839
type
3940

@@ -49,6 +50,7 @@ TMyObject = class
4950
procedure OnAfterCallHook(const Context: TWebContext; const JSONResponse: TJDOJsonObject);
5051
public
5152
[MVCDoc('You know, returns aValue1 - aValue2')]
53+
[MVCJSONRPCAllowGET]
5254
function Subtract(Value1, Value2: Integer): Integer;
5355
[MVCDoc('Returns the revers of the string passed as input')]
5456
function ReverseString(const aString: string; const aUpperCase: Boolean): string;

samples/jsonrpc/sync_client/MainClientFormU.pas

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,22 @@ procedure TMainForm.btnGetUserClick(Sender: TObject);
312312
raise Exception.Create(lResp.Error.ErrMessage);
313313

314314
// Remember that TObject descendants (but TDataset, TJDOJSONObject and TJDOJSONArray)
315-
// are serialized as JSON objects
316-
lJSON := lResp.Result.AsObject as TJsonObject;
317-
lbPerson.Items.Add('First Name:'.PadRight(15) + lJSON.S['firstname']);
318-
lbPerson.Items.Add('Last Name:'.PadRight(15) + lJSON.S['lastname']);
319-
lbPerson.Items.Add('Married:'.PadRight(15) + lJSON.B['married'].ToString(TUseBoolStrs.True));
320-
lbPerson.Items.Add('DOB:'.PadRight(15) + DateToStr(lJSON.D['dob']));
315+
// are serialized as JSON objects, so you can always read the JSON object
316+
// lJSON := Resp.Result.AsObject as TJsonObject;
317+
// lbPerson.Items.Add('First Name:'.PadRight(15) + lJSON.S['firstname']);
318+
// lbPerson.Items.Add('Last Name:'.PadRight(15) + lJSON.S['lastname']);
319+
// lbPerson.Items.Add('Married:'.PadRight(15) + lJSON.B['married'].ToString(TUseBoolStrs.True));
320+
// lbPerson.Items.Add('DOB:'.PadRight(15) + DateToStr(lJSON.D['dob']));
321+
var lPerson := TPerson.Create;
322+
try
323+
lResp.ResultAs(lPerson);
324+
lbPerson.Items.Add('First Name:'.PadRight(15) + lPerson.FirstName);
325+
lbPerson.Items.Add('Last Name:'.PadRight(15) + lPerson.LastName);
326+
lbPerson.Items.Add('Married:'.PadRight(15) + lPerson.Married.ToString(TUseBoolStrs.True));
327+
lbPerson.Items.Add('DOB:'.PadRight(15) + DateToStr(lPerson.DOB));
328+
finally
329+
lPerson.Free;
330+
end;
321331
end;
322332

323333
procedure TMainForm.btnInvalid1Click(Sender: TObject);
@@ -510,16 +520,18 @@ procedure TMainForm.btnSubtractWithNamedParamsClick(Sender: TObject);
510520
var
511521
lReq: IJSONRPCRequest;
512522
lResp: IJSONRPCResponse;
523+
lExecutor: IMVCJSONRPCExecutor;
513524
begin
514-
lReq := TJSONRPCRequest.Create;
515-
lReq.Method := 'subtract';
516-
lReq.RequestID := Random(1000);
525+
lExecutor := TMVCJSONRPCExecutor.Create('http://localhost:8080');
526+
lReq := lExecutor.CreateRequest('subtract', Random(1000));
517527
lReq.Params.AddByName('Value1', StrToInt(Edit1.Text));
518528
lReq.Params.AddByName('Value2', StrToInt(Edit2.Text));
519-
lResp := FExecutor.ExecuteRequest('/jsonrpc', lReq);
529+
lResp := lExecutor.ExecuteRequest('/jsonrpc', lReq, jrpcGET);
520530
Edit3.Text := lResp.Result.AsInteger.ToString;
521531
end;
522532

533+
534+
523535
procedure TMainForm.btnWithJSONClick(Sender: TObject);
524536
var
525537
lPerson: TJsonObject;

sources/MVCFramework.DataSet.Utils.pas

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ interface
3939
MVCFramework.Commons,
4040
MVCFramework.Serializer.Commons,
4141
MVCFramework.RESTClient.Intf,
42-
MVCFramework.RESTClient;
42+
MVCFramework.RESTClient, MVCFramework.JSONRPC;
4343

4444
type
4545
TFieldNamePolicy = (fpLowerCase, fpUpperCase, fpAsIs);
4646

4747
TDataSetHelper = class helper for TDataSet
4848
public
49+
procedure LoadFromJSONRPCResponse(const Value: IJSONRPCResponse; const aNameCase: TMVCNameCase = TMVCNameCase.ncUseDefault);
4950
procedure LoadFromTValue(const Value: TValue;
5051
const aNameCase: TMVCNameCase = TMVCNameCase.ncUseDefault);
5152
function AsJSONArray(FieldNameCase: TMVCNameCase = TMVCNameCase.ncUseDefault): string;
@@ -190,8 +191,14 @@ procedure TDataSetHelper.LoadFromTValue(const Value: TValue;
190191

191192
lSer := TMVCJsonDataObjectsSerializer.Create;
192193
try
193-
lSer.JsonArrayToDataSet(TJSONArray(Value.AsObject), Self, [],
194-
TMVCNameCase.ncUseDefault);
194+
DisableControls;
195+
try
196+
lSer.JsonArrayToDataSet(TJSONArray(Value.AsObject), Self, [],
197+
TMVCNameCase.ncUseDefault);
198+
First;
199+
finally
200+
EnableControls;
201+
end;
195202
finally
196203
lSer.Free;
197204
end;
@@ -464,6 +471,11 @@ procedure TDataSetHelper.LoadFromJSONObjectString(const JSONObjectString
464471
TMVCIgnoredList(IgnoredFields), FieldNameCase);
465472
end;
466473

474+
procedure TDataSetHelper.LoadFromJSONRPCResponse(const Value: IJSONRPCResponse; const aNameCase: TMVCNameCase);
475+
begin
476+
LoadFromTValue(Value.Result, aNameCase);
477+
end;
478+
467479
procedure TDataSetHelper.LoadFromJSONObject(const JSONObject: TJSONObject;
468480
const FieldNameCase: TMVCNameCase);
469481
begin

sources/MVCFramework.JSONRPC.Client.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ interface
7474
function SetOnSendCommand(const aOnSendCommandProc: TProc<IJSONRPCObject>): IMVCJSONRPCExecutor;
7575
function SetOnReceiveHTTPResponse(const aOnReceiveHTTPResponse: TProc<IHTTPResponse>): IMVCJSONRPCExecutor;
7676
//end events
77+
//"constructors"
7778
function CreateRequest(const MethodName: String; const RequestID: UInt64): IJSONRPCRequest; overload;
7879
function CreateRequest(const MethodName: String; const RequestID: String): IJSONRPCRequest; overload;
7980
function CreateNotification(const MethodName: String): IJSONRPCNotification;

0 commit comments

Comments
 (0)