mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-15 15:55:54 +01:00
removed tmvcrestclient from samples
This commit is contained in:
parent
5c5e3f707b
commit
4e4c7f2198
@ -8,7 +8,7 @@ uses
|
||||
FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
|
||||
FireDAC.DApt.Intf, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
|
||||
Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls, Vcl.StdCtrls, MVCFramework.RESTClient,
|
||||
Vcl.DBCtrls, MVCFramework.RESTClient.Intf;
|
||||
Vcl.DBCtrls;
|
||||
|
||||
type
|
||||
TMainForm = class(TForm)
|
||||
@ -31,6 +31,7 @@ type
|
||||
procedure FormCreate(Sender: TObject);
|
||||
procedure dsArticlesBeforePost(DataSet: TDataSet);
|
||||
procedure dsArticlesBeforeDelete(DataSet: TDataSet);
|
||||
procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
||||
procedure dsArticlesBeforeRefresh(DataSet: TDataSet);
|
||||
procedure dsArticlesAfterOpen(DataSet: TDataSet);
|
||||
procedure btnOpenClick(Sender: TObject);
|
||||
@ -41,9 +42,9 @@ type
|
||||
private
|
||||
fFilter: string;
|
||||
fLoading: Boolean;
|
||||
fRESTClient: IMVCRESTClient;
|
||||
fRESTClient: TRESTClient;
|
||||
{ Private declarations }
|
||||
procedure ShowError(const AResponse: IMVCRESTResponse);
|
||||
procedure ShowError(const AResponse: IRESTResponse);
|
||||
procedure SetFilter(const Value: string);
|
||||
public
|
||||
property Filter: string read fFilter write SetFilter;
|
||||
@ -86,19 +87,19 @@ end;
|
||||
|
||||
procedure TMainForm.dsArticlesAfterOpen(DataSet: TDataSet);
|
||||
var
|
||||
Res: IMVCRESTResponse;
|
||||
Res: IRESTResponse;
|
||||
begin
|
||||
if fFilter.IsEmpty then
|
||||
begin
|
||||
// this a simple sychronous request...
|
||||
Res := fRESTClient.Get('/articles');
|
||||
Res := fRESTClient.doGET('/articles', []);
|
||||
end
|
||||
else
|
||||
begin
|
||||
Res := fRESTClient.AddQueryStringParam('q', fFilter).Get('/articles/searches');
|
||||
Res := fRESTClient.doGET('/articles/searches', [], ['q'], [fFilter]);
|
||||
end;
|
||||
|
||||
if not Res.Success then
|
||||
if Res.HasError then
|
||||
begin
|
||||
ShowError(Res);
|
||||
Exit;
|
||||
@ -107,7 +108,7 @@ begin
|
||||
DataSet.DisableControls;
|
||||
try
|
||||
fLoading := true;
|
||||
dsArticles.LoadJSONArrayFromJSONObjectProperty('data', Res.Content);
|
||||
dsArticles.LoadJSONArrayFromJSONObjectProperty('data', Res.BodyAsString);
|
||||
fLoading := false;
|
||||
dsArticles.First;
|
||||
finally
|
||||
@ -117,11 +118,11 @@ end;
|
||||
|
||||
procedure TMainForm.dsArticlesBeforeDelete(DataSet: TDataSet);
|
||||
var
|
||||
Res: IMVCRESTResponse;
|
||||
Res: IRESTResponse;
|
||||
begin
|
||||
if dsArticles.State = dsBrowse then
|
||||
Res := fRESTClient.DataSetDelete('/articles', dsArticlesid.AsString);
|
||||
if not(Res.StatusCode in [200]) then
|
||||
if not(Res.ResponseCode in [200]) then
|
||||
begin
|
||||
ShowError(Res);
|
||||
Abort;
|
||||
@ -130,15 +131,15 @@ end;
|
||||
|
||||
procedure TMainForm.dsArticlesBeforePost(DataSet: TDataSet);
|
||||
var
|
||||
Res: IMVCRESTResponse;
|
||||
Res: IRESTResponse;
|
||||
begin
|
||||
if not fLoading then
|
||||
begin
|
||||
if dsArticles.State = dsInsert then
|
||||
Res := fRESTClient.DataSetInsert('/articles', dsArticles)
|
||||
else
|
||||
Res := fRESTClient.DataSetUpdate('/articles', dsArticlesid.AsString, dsArticles);
|
||||
if not(Res.StatusCode in [200, 201]) then
|
||||
Res := fRESTClient.DataSetUpdate('/articles', dsArticles, dsArticlesid.AsString);
|
||||
if not(Res.ResponseCode in [200, 201]) then
|
||||
begin
|
||||
ShowError(Res);
|
||||
Abort;
|
||||
@ -158,19 +159,22 @@ end;
|
||||
|
||||
procedure TMainForm.dsArticlesBeforeRowRequest(DataSet: TFDDataSet);
|
||||
var
|
||||
Res: IMVCRESTResponse;
|
||||
Res: IRESTResponse;
|
||||
begin
|
||||
Res := fRESTClient
|
||||
.AddPathParam('Id', DataSet.FieldByName('id').AsString)
|
||||
.Get('/articles/($Id)');
|
||||
Res := fRESTClient.doGET('/articles', [DataSet.FieldByName('id').AsString]);
|
||||
fLoading := true;
|
||||
DataSet.LoadJSONObjectFromJSONObjectProperty('data', Res.Content);
|
||||
DataSet.LoadJSONObjectFromJSONObjectProperty('data', Res.BodyAsString);
|
||||
fLoading := false;
|
||||
end;
|
||||
|
||||
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
|
||||
begin
|
||||
fRESTClient.Free;
|
||||
end;
|
||||
|
||||
procedure TMainForm.FormCreate(Sender: TObject);
|
||||
begin
|
||||
fRESTClient := TMVCRESTClient.New.BaseURL('localhost', 8080);
|
||||
fRESTClient := MVCFramework.RESTClient.TRESTClient.Create('localhost', 8080);
|
||||
end;
|
||||
|
||||
procedure TMainForm.SetFilter(const Value: string);
|
||||
@ -179,17 +183,17 @@ begin
|
||||
EditFilter.Text := Value;
|
||||
end;
|
||||
|
||||
procedure TMainForm.ShowError(const AResponse: IMVCRESTResponse);
|
||||
procedure TMainForm.ShowError(const AResponse: IRESTResponse);
|
||||
begin
|
||||
if not AResponse.Success then
|
||||
if AResponse.HasError then
|
||||
MessageDlg(
|
||||
AResponse.StatusCode.ToString + ': ' + AResponse.StatusText + sLineBreak +
|
||||
'[' + AResponse.Content + ']',
|
||||
AResponse.Error.HTTPError.ToString + ': ' + AResponse.Error.ExceptionMessage + sLineBreak +
|
||||
'[' + AResponse.Error.ExceptionClassname + ']',
|
||||
mtError, [mbOK], 0)
|
||||
else
|
||||
MessageDlg(
|
||||
AResponse.StatusCode.ToString + ': ' + AResponse.StatusText + sLineBreak +
|
||||
AResponse.Content,
|
||||
AResponse.ResponseCode.ToString + ': ' + AResponse.ResponseText + sLineBreak +
|
||||
AResponse.BodyAsString,
|
||||
mtError, [mbOK], 0);
|
||||
end;
|
||||
|
||||
|
@ -7,7 +7,7 @@ uses
|
||||
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
|
||||
FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
|
||||
FireDAC.DApt.Intf, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
|
||||
Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls, Vcl.StdCtrls, MVCFramework.RESTClient, MVCFramework.RESTClient.Intf,
|
||||
Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls, Vcl.StdCtrls, MVCFramework.RESTClient,
|
||||
Vcl.DBCtrls, MVCFramework.DataSet.Utils;
|
||||
|
||||
type
|
||||
@ -42,10 +42,10 @@ type
|
||||
private
|
||||
fFilter: string;
|
||||
fLoading: Boolean;
|
||||
fRESTClient: IMVCRESTClient;
|
||||
fRESTClient: TRESTClient;
|
||||
fAPIBinder: TMVCAPIBinder;
|
||||
{ Private declarations }
|
||||
procedure ShowError(const AResponse: IMVCRESTResponse);
|
||||
procedure ShowError(const AResponse: IRESTResponse);
|
||||
procedure SetFilter(const Value: string);
|
||||
public
|
||||
property Filter: string read fFilter write SetFilter;
|
||||
@ -88,19 +88,19 @@ end;
|
||||
|
||||
procedure TMainForm.dsArticlesAfterOpen(DataSet: TDataSet);
|
||||
var
|
||||
Res: IMVCRESTResponse;
|
||||
Res: IRESTResponse;
|
||||
begin
|
||||
if fFilter.IsEmpty then
|
||||
begin
|
||||
// this a simple sychronous request...
|
||||
Res := fRESTClient.Get('/articles');
|
||||
Res := fRESTClient.doGET('/articles', []);
|
||||
end
|
||||
else
|
||||
begin
|
||||
Res := fRESTClient.AddQueryStringParam('q', fFilter).Get('/articles/searches');
|
||||
Res := fRESTClient.doGET('/articles/searches', [], ['q'], [fFilter]);
|
||||
end;
|
||||
|
||||
if not Res.Success then
|
||||
if Res.HasError then
|
||||
begin
|
||||
ShowError(Res);
|
||||
Exit;
|
||||
@ -109,7 +109,7 @@ begin
|
||||
DataSet.DisableControls;
|
||||
try
|
||||
fLoading := true;
|
||||
dsArticles.LoadJSONArrayFromJSONObjectProperty('data', Res.Content);
|
||||
dsArticles.LoadJSONArrayFromJSONObjectProperty('data', Res.BodyAsString);
|
||||
fLoading := false;
|
||||
dsArticles.First;
|
||||
finally
|
||||
@ -119,11 +119,11 @@ end;
|
||||
|
||||
procedure TMainForm.dsArticlesBeforeDelete(DataSet: TDataSet);
|
||||
var
|
||||
Res: IMVCRESTResponse;
|
||||
Res: IRESTResponse;
|
||||
begin
|
||||
if dsArticles.State = dsBrowse then
|
||||
Res := fRESTClient.DataSetDelete('/articles', dsArticlesid.AsString);
|
||||
if not(Res.StatusCode in [200]) then
|
||||
if not(Res.ResponseCode in [200]) then
|
||||
begin
|
||||
ShowError(Res);
|
||||
Abort;
|
||||
@ -132,15 +132,15 @@ end;
|
||||
|
||||
procedure TMainForm.dsArticlesBeforePost(DataSet: TDataSet);
|
||||
var
|
||||
Res: IMVCRESTResponse;
|
||||
Res: IRESTResponse;
|
||||
begin
|
||||
if not fLoading then
|
||||
begin
|
||||
if dsArticles.State = dsInsert then
|
||||
Res := fRESTClient.DataSetInsert('/articles', dsArticles)
|
||||
else
|
||||
Res := fRESTClient.DataSetUpdate('/articles', dsArticlesid.AsString, dsArticles);
|
||||
if not(Res.StatusCode in [200, 201]) then
|
||||
Res := fRESTClient.DataSetUpdate('/articles', dsArticles, dsArticlesid.AsString);
|
||||
if not(Res.ResponseCode in [200, 201]) then
|
||||
begin
|
||||
ShowError(Res);
|
||||
Abort;
|
||||
@ -160,23 +160,23 @@ end;
|
||||
|
||||
procedure TMainForm.dsArticlesBeforeRowRequest(DataSet: TFDDataSet);
|
||||
var
|
||||
Res: IMVCRESTResponse;
|
||||
Res: IRESTResponse;
|
||||
begin
|
||||
Res := fRESTClient.AddPathParam('Id', DataSet.FieldByName('id').AsString).Get('/articles/($Id)');
|
||||
Res := fRESTClient.doGET('/articles', [DataSet.FieldByName('id').AsString]);
|
||||
fLoading := true;
|
||||
DataSet.LoadJSONObjectFromJSONObjectProperty('data', Res.Content);
|
||||
DataSet.LoadJSONObjectFromJSONObjectProperty('data', Res.BodyAsString);
|
||||
fLoading := false;
|
||||
end;
|
||||
|
||||
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
|
||||
begin
|
||||
fAPIBinder.Free;
|
||||
fRESTClient.Free;
|
||||
end;
|
||||
|
||||
procedure TMainForm.FormCreate(Sender: TObject);
|
||||
begin
|
||||
ReportMemoryLeaksOnShutdown := True;
|
||||
fRESTClient := TMVCRESTClient.New.BaseURL('localhost', 8080);
|
||||
fRESTClient := MVCFramework.RESTClient.TRESTClient.Create('localhost', 8080);
|
||||
fAPIBinder := TMVCAPIBinder.Create(fRESTClient);
|
||||
fAPIBinder.BindDataSetToAPI(dsArticles, '/articles', 'id');
|
||||
end;
|
||||
@ -187,12 +187,18 @@ begin
|
||||
EditFilter.Text := Value;
|
||||
end;
|
||||
|
||||
procedure TMainForm.ShowError(const AResponse: IMVCRESTResponse);
|
||||
procedure TMainForm.ShowError(const AResponse: IRESTResponse);
|
||||
begin
|
||||
MessageDlg(
|
||||
AResponse.StatusCode.ToString + ': ' + AResponse.StatusText + sLineBreak +
|
||||
'[' + AResponse.Content + ']',
|
||||
mtError, [mbOK], 0)
|
||||
if AResponse.HasError then
|
||||
MessageDlg(
|
||||
AResponse.Error.HTTPError.ToString + ': ' + AResponse.Error.ExceptionMessage + sLineBreak +
|
||||
'[' + AResponse.Error.ExceptionClassname + ']',
|
||||
mtError, [mbOK], 0)
|
||||
else
|
||||
MessageDlg(
|
||||
AResponse.ResponseCode.ToString + ': ' + AResponse.ResponseText + sLineBreak +
|
||||
AResponse.BodyAsString,
|
||||
mtError, [mbOK], 0);
|
||||
end;
|
||||
|
||||
end.
|
||||
|
@ -2,7 +2,7 @@ object Form10: TForm10
|
||||
Left = 0
|
||||
Top = 0
|
||||
Caption = 'Form10'
|
||||
ClientHeight = 484
|
||||
ClientHeight = 448
|
||||
ClientWidth = 831
|
||||
Color = clBtnFace
|
||||
Font.Charset = DEFAULT_CHARSET
|
||||
@ -14,28 +14,9 @@ object Form10: TForm10
|
||||
OnCreate = FormCreate
|
||||
PixelsPerInch = 96
|
||||
TextHeight = 13
|
||||
object Label2: TLabel
|
||||
AlignWithMargins = True
|
||||
Left = 3
|
||||
Top = 3
|
||||
Width = 825
|
||||
Height = 39
|
||||
Align = alTop
|
||||
Alignment = taCenter
|
||||
Caption =
|
||||
'Please use the demo available in samples\jsonrpc_with_published_' +
|
||||
'objects\'
|
||||
Font.Charset = DEFAULT_CHARSET
|
||||
Font.Color = clWindowText
|
||||
Font.Height = -21
|
||||
Font.Name = 'Tahoma'
|
||||
Font.Style = []
|
||||
ParentFont = False
|
||||
Layout = tlCenter
|
||||
end
|
||||
object GroupBox1: TGroupBox
|
||||
Left = 8
|
||||
Top = 48
|
||||
Top = 16
|
||||
Width = 815
|
||||
Height = 124
|
||||
Caption = 'Simple Types'
|
||||
@ -183,7 +164,7 @@ object Form10: TForm10
|
||||
end
|
||||
object GroupBox2: TGroupBox
|
||||
Left = 8
|
||||
Top = 178
|
||||
Top = 146
|
||||
Width = 489
|
||||
Height = 159
|
||||
Caption = 'Returning Objects'
|
||||
@ -221,7 +202,7 @@ object Form10: TForm10
|
||||
end
|
||||
object GroupBox3: TGroupBox
|
||||
Left = 509
|
||||
Top = 178
|
||||
Top = 146
|
||||
Width = 314
|
||||
Height = 294
|
||||
Caption = 'Returning Datasets'
|
||||
@ -258,7 +239,7 @@ object Form10: TForm10
|
||||
end
|
||||
object GroupBox4: TGroupBox
|
||||
Left = 8
|
||||
Top = 343
|
||||
Top = 311
|
||||
Width = 489
|
||||
Height = 129
|
||||
Caption = 'Passing Objects as parameters'
|
||||
@ -317,7 +298,7 @@ object Form10: TForm10
|
||||
object DataSource1: TDataSource
|
||||
DataSet = FDMemTable1
|
||||
Left = 767
|
||||
Top = 216
|
||||
Top = 184
|
||||
end
|
||||
object FDMemTable1: TFDMemTable
|
||||
FetchOptions.AssignedValues = [evMode]
|
||||
@ -328,7 +309,7 @@ object Form10: TForm10
|
||||
UpdateOptions.CheckRequired = False
|
||||
UpdateOptions.AutoCommitUpdates = True
|
||||
Left = 767
|
||||
Top = 280
|
||||
Top = 248
|
||||
object FDMemTable1Code: TIntegerField
|
||||
FieldName = 'Code'
|
||||
end
|
||||
|
@ -66,7 +66,6 @@ type
|
||||
btnInvalid2: TButton;
|
||||
btnNotification: TButton;
|
||||
btnInvalidMethod: TButton;
|
||||
Label2: TLabel;
|
||||
procedure btnSubstractClick(Sender: TObject);
|
||||
procedure btnReverseStringClick(Sender: TObject);
|
||||
procedure edtGetCustomersClick(Sender: TObject);
|
||||
@ -139,25 +138,25 @@ end;
|
||||
|
||||
procedure TForm10.btnInvalid1Click(Sender: TObject);
|
||||
var
|
||||
lReq: IJSONRPCNotification;
|
||||
lReq: IJSONRPCRequest;
|
||||
lResp: IJSONRPCResponse;
|
||||
begin
|
||||
lReq := TJSONRPCNotification.Create;
|
||||
lReq := TJSONRPCRequest.Create;
|
||||
lReq.Method := 'invalidmethod1';
|
||||
lReq.Params.Add(1);
|
||||
lResp := FExecutor.ExecuteNotification(lReq);
|
||||
lResp := FExecutor.ExecuteRequest(lReq);
|
||||
ShowMessage(lResp.Error.ErrMessage);
|
||||
end;
|
||||
|
||||
procedure TForm10.btnInvalid2Click(Sender: TObject);
|
||||
var
|
||||
lReq: IJSONRPCNotification;
|
||||
lReq: IJSONRPCRequest;
|
||||
lResp: IJSONRPCResponse;
|
||||
begin
|
||||
lReq := TJSONRPCNotification.Create;
|
||||
lReq := TJSONRPCRequest.Create;
|
||||
lReq.Method := 'invalidmethod2';
|
||||
lReq.Params.Add(1);
|
||||
lResp := FExecutor.ExecuteNotification(lReq);
|
||||
lResp := FExecutor.ExecuteRequest(lReq);
|
||||
ShowMessage(lResp.Error.ErrMessage);
|
||||
end;
|
||||
|
||||
|
@ -420,37 +420,6 @@ object MainForm: TMainForm
|
||||
end
|
||||
end
|
||||
end
|
||||
object TabSheet3: TTabSheet
|
||||
Caption = 'Hooks Demo'
|
||||
ImageIndex = 2
|
||||
object btnDoNothing: TButton
|
||||
Left = 24
|
||||
Top = 24
|
||||
Width = 145
|
||||
Height = 33
|
||||
Caption = 'Do Nothing'
|
||||
TabOrder = 0
|
||||
OnClick = btnDoNothingClick
|
||||
end
|
||||
object btnDoNothingError: TButton
|
||||
Left = 24
|
||||
Top = 63
|
||||
Width = 145
|
||||
Height = 33
|
||||
Caption = 'Do Nothing With Errors'
|
||||
TabOrder = 1
|
||||
OnClick = btnDoNothingErrorClick
|
||||
end
|
||||
object btnNotExistent: TButton
|
||||
Left = 24
|
||||
Top = 102
|
||||
Width = 145
|
||||
Height = 33
|
||||
Caption = 'Invalid Method'
|
||||
TabOrder = 2
|
||||
OnClick = btnNotExistentClick
|
||||
end
|
||||
end
|
||||
end
|
||||
object DataSource1: TDataSource
|
||||
DataSet = FDMemTable1
|
||||
|
@ -81,10 +81,6 @@ type
|
||||
Edit2: TEdit;
|
||||
btnSubtractWithNamedParams: TButton;
|
||||
Edit3: TEdit;
|
||||
TabSheet3: TTabSheet;
|
||||
btnDoNothing: TButton;
|
||||
btnDoNothingError: TButton;
|
||||
btnNotExistent: TButton;
|
||||
procedure btnSubstractClick(Sender: TObject);
|
||||
procedure btnReverseStringClick(Sender: TObject);
|
||||
procedure edtGetCustomersClick(Sender: TObject);
|
||||
@ -101,13 +97,9 @@ type
|
||||
procedure btnFloatsTestsClick(Sender: TObject);
|
||||
procedure btnWithJSONClick(Sender: TObject);
|
||||
procedure btnSubtractWithNamedParamsClick(Sender: TObject);
|
||||
procedure btnDoNothingClick(Sender: TObject);
|
||||
procedure btnNotExistentClick(Sender: TObject);
|
||||
procedure btnDoNothingErrorClick(Sender: TObject);
|
||||
private
|
||||
FExecutor: IMVCJSONRPCExecutor;
|
||||
FExecutor2: IMVCJSONRPCExecutor;
|
||||
FExecutor3: IMVCJSONRPCExecutor;
|
||||
public
|
||||
{ Public declarations }
|
||||
end;
|
||||
@ -154,35 +146,7 @@ begin
|
||||
lReq.Params.Add(Date(), pdtDate);
|
||||
lReq.Params.Add(Now(), pdtDateTime);
|
||||
lResp := FExecutor.ExecuteRequest(lReq);
|
||||
ShowMessage('Using Positional Parameters: ' +
|
||||
DateTimeToStr(ISOTimeStampToDateTime(lResp.Result.AsString)));
|
||||
|
||||
// (const aJustAFloat: Double; const aTime: TTime; const aDate: TDate; const aDateAndTime: TDateTime)
|
||||
lReq := TJSONRPCRequest.Create(1234, 'playwithdatesandtimes');
|
||||
lReq.Params.AddByName('aJustAFloat', 1234.5678, pdtFloat);
|
||||
lReq.Params.AddByName('ATIME', Time(), pdtTime);
|
||||
lReq.Params.AddByName('adate', Date(), pdtDate);
|
||||
lReq.Params.AddByName('adateAndTime', Now(), pdtDateTime);
|
||||
lResp := FExecutor.ExecuteRequest(lReq);
|
||||
ShowMessage('Using Named Parameters: ' +
|
||||
DateTimeToStr(ISOTimeStampToDateTime(lResp.Result.AsString)));
|
||||
|
||||
end;
|
||||
|
||||
procedure TMainForm.btnDoNothingClick(Sender: TObject);
|
||||
var
|
||||
lReq: IJSONRPCNotification;
|
||||
begin
|
||||
lReq := TJSONRPCNotification.Create('DoSomething');
|
||||
FExecutor3.ExecuteNotification(lReq);
|
||||
end;
|
||||
|
||||
procedure TMainForm.btnDoNothingErrorClick(Sender: TObject);
|
||||
var
|
||||
lReq: IJSONRPCNotification;
|
||||
begin
|
||||
lReq := TJSONRPCNotification.Create('DoSomethingWithError');
|
||||
FExecutor3.ExecuteNotification(lReq);
|
||||
ShowMessage(DateTimeToStr(lResp.Result.AsType<TDateTime>));
|
||||
end;
|
||||
|
||||
procedure TMainForm.btnFloatsTestsClick(Sender: TObject);
|
||||
@ -234,25 +198,25 @@ end;
|
||||
|
||||
procedure TMainForm.btnInvalid1Click(Sender: TObject);
|
||||
var
|
||||
lReq: IJSONRPCNotification;
|
||||
lReq: IJSONRPCRequest;
|
||||
lResp: IJSONRPCResponse;
|
||||
begin
|
||||
lReq := TJSONRPCNotification.Create;
|
||||
lReq := TJSONRPCRequest.Create;
|
||||
lReq.Method := 'invalidmethod1';
|
||||
lReq.Params.Add(1);
|
||||
lResp := FExecutor.ExecuteNotification(lReq);
|
||||
lResp := FExecutor.ExecuteRequest(lReq);
|
||||
ShowMessage(lResp.Error.ErrMessage);
|
||||
end;
|
||||
|
||||
procedure TMainForm.btnInvalid2Click(Sender: TObject);
|
||||
var
|
||||
lReq: IJSONRPCNotification;
|
||||
lReq: IJSONRPCRequest;
|
||||
lResp: IJSONRPCResponse;
|
||||
begin
|
||||
lReq := TJSONRPCNotification.Create;
|
||||
lReq := TJSONRPCRequest.Create;
|
||||
lReq.Method := 'invalidmethod2';
|
||||
lReq.Params.Add(1);
|
||||
lResp := FExecutor.ExecuteNotification(lReq);
|
||||
lResp := FExecutor.ExecuteRequest(lReq);
|
||||
ShowMessage(lResp.Error.ErrMessage);
|
||||
end;
|
||||
|
||||
@ -330,8 +294,7 @@ begin
|
||||
for I := 0 to lJSON.Count - 1 do
|
||||
begin
|
||||
lJObj := lJSON[I].ObjectValue;
|
||||
ListBox1.Items.Add(Format('%6s: %-34s € %5.2f', [lJObj.S['codice'], lJObj.S['descrizione'],
|
||||
lJObj.F['prezzo']]));
|
||||
ListBox1.Items.Add(Format('%6s: %-34s € %5.2f', [lJObj.S['codice'], lJObj.S['descrizione'], lJObj.F['prezzo']]));
|
||||
end;
|
||||
// lbPerson.Items.Add('First Name:'.PadRight(15) + lJSON.S['firstname']);
|
||||
// lbPerson.Items.Add('Last Name:'.PadRight(15) + lJSON.S['lastname']);
|
||||
@ -386,14 +349,6 @@ begin
|
||||
ShowMessage(lPerson.ToJSON(False));
|
||||
end;
|
||||
|
||||
procedure TMainForm.btnNotExistentClick(Sender: TObject);
|
||||
var
|
||||
lReq: IJSONRPCNotification;
|
||||
begin
|
||||
lReq := TJSONRPCNotification.Create('blablabla');
|
||||
FExecutor3.ExecuteNotification(lReq);
|
||||
end;
|
||||
|
||||
procedure TMainForm.edtGetCustomersClick(Sender: TObject);
|
||||
var
|
||||
lReq: IJSONRPCRequest;
|
||||
@ -412,8 +367,6 @@ procedure TMainForm.FormCreate(Sender: TObject);
|
||||
begin
|
||||
FExecutor := TMVCJSONRPCExecutor.Create('http://localhost:8080/jsonrpc');
|
||||
FExecutor2 := TMVCJSONRPCExecutor.Create('http://localhost:8080/rpcdatamodule');
|
||||
FExecutor3 := TMVCJSONRPCExecutor.Create('http://localhost:8080/jsonrpchooks');
|
||||
|
||||
dtNextMonday.Date := Date;
|
||||
|
||||
// these are the methods to handle http headers in JSONRPC
|
||||
@ -424,7 +377,6 @@ begin
|
||||
FExecutor.ClearHTTPHeaders;
|
||||
Assert(FExecutor.HTTPHeadersCount = 0);
|
||||
FExecutor.AddHTTPHeader(TNetHeader.Create('x-token', TGUID.NewGuid.ToString));
|
||||
PageControl1.ActivePageIndex := 0;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
@ -40,7 +40,6 @@ uses
|
||||
MVCFramework.Commons,
|
||||
MyObjectU,
|
||||
MVCFramework.JSONRPC,
|
||||
MVCFramework.Middleware.CORS,
|
||||
MainDM;
|
||||
|
||||
procedure TMyWebModule.WebModuleCreate(Sender: TObject);
|
||||
@ -58,14 +57,6 @@ begin
|
||||
begin
|
||||
Result := TdmMain.Create(nil);
|
||||
end, '/rpcdatamodule');
|
||||
|
||||
FMVC.PublishObject(
|
||||
function: TObject
|
||||
begin
|
||||
Result := TMyObjectWithHooks.Create;
|
||||
end, '/jsonrpchooks');
|
||||
|
||||
FMVC.AddMiddleware(TCORSMiddleware.Create());
|
||||
end;
|
||||
|
||||
procedure TMyWebModule.WebModuleDestroy(Sender: TObject);
|
||||
|
@ -52,10 +52,10 @@ type
|
||||
function GetCustomersDataset: TFDMemTable;
|
||||
function GetPeopleDataset: TFDMemTable;
|
||||
public
|
||||
procedure OnBeforeCall(const Context: TWebContext; const JSONRequest: TJDOJsonObject);
|
||||
procedure OnBeforeRouting(const Context: TWebContext; const JSON: TJDOJsonObject);
|
||||
procedure OnAfterCallHook(
|
||||
const Context: TWebContext; const JSONResponse: TJDOJsonObject);
|
||||
procedure OnBeforeCall(const JSONRequest: TJDOJsonObject);
|
||||
procedure OnBeforeRouting(const JSON: TJDOJsonObject);
|
||||
procedure OnBeforeSendResponse(
|
||||
const JSONResponse: TJDOJsonObject);
|
||||
public
|
||||
[MVCDoc('You know, returns aValue1 - aValue2')]
|
||||
function Subtract(Value1, Value2: Integer): Integer;
|
||||
@ -79,17 +79,6 @@ type
|
||||
|
||||
end;
|
||||
|
||||
TMyObjectWithHooks = class
|
||||
public
|
||||
// hooks
|
||||
procedure OnBeforeRoutingHook(const Context: TWebContext; const JSON: TJsonObject);
|
||||
procedure OnBeforeCallHook(const Context: TWebContext; const JSON: TJsonObject);
|
||||
procedure OnAfterCallHook(const Context: TWebContext; const JSON: TJsonObject);
|
||||
// dummy method
|
||||
procedure DoSomething;
|
||||
procedure DoSomethingWithError;
|
||||
end;
|
||||
|
||||
TUtils = class sealed
|
||||
class function JSONObjectAs<T: constructor, class>(const JSON: TJsonObject): T;
|
||||
end;
|
||||
@ -311,53 +300,26 @@ end;
|
||||
|
||||
{ TMyObjectWithHooks }
|
||||
|
||||
procedure TMyObject.OnBeforeCall(const Context: TWebContext; const JSONRequest: TJDOJsonObject);
|
||||
procedure TMyObject.OnBeforeCall(const JSONRequest: TJDOJsonObject);
|
||||
begin
|
||||
Log.Info('TMyObjectWithHooks.OnBeforeCall >> ', 'jsonrpc');
|
||||
Log.Info(JSONRequest.ToJSON(false), 'jsonrpc');
|
||||
Log.Info('TMyObjectWithHooks.OnBeforeCall << ', 'jsonrpc');
|
||||
end;
|
||||
|
||||
procedure TMyObject.OnBeforeRouting(const Context: TWebContext; const JSON: TJDOJsonObject);
|
||||
procedure TMyObject.OnBeforeRouting(const JSON: TJDOJsonObject);
|
||||
begin
|
||||
Log.Info('TMyObjectWithHooks.OnBeforeRouting >> ', 'jsonrpc');
|
||||
Log.Info(JSON.ToJSON(false), 'jsonrpc');
|
||||
Log.Info('TMyObjectWithHooks.OnBeforeRouting << ', 'jsonrpc');
|
||||
end;
|
||||
|
||||
procedure TMyObject.OnAfterCallHook(
|
||||
const Context: TWebContext; const JSONResponse: TJDOJsonObject);
|
||||
procedure TMyObject.OnBeforeSendResponse(
|
||||
const JSONResponse: TJDOJsonObject);
|
||||
begin
|
||||
Log.Info('TMyObjectWithHooks.OnBeforeSendResponse >> ', 'jsonrpc');
|
||||
Log.Info(JSONResponse.ToJSON(false), 'jsonrpc');
|
||||
Log.Info('TMyObjectWithHooks.OnBeforeSendResponse << ', 'jsonrpc');
|
||||
end;
|
||||
|
||||
{ TMyObjectWithHooks }
|
||||
|
||||
procedure TMyObjectWithHooks.DoSomething;
|
||||
begin
|
||||
// do nothing
|
||||
end;
|
||||
|
||||
procedure TMyObjectWithHooks.DoSomethingWithError;
|
||||
begin
|
||||
raise Exception.Create('Boom');
|
||||
end;
|
||||
|
||||
procedure TMyObjectWithHooks.OnAfterCallHook(const Context: TWebContext; const JSON: TJsonObject);
|
||||
begin
|
||||
// do nothing
|
||||
end;
|
||||
|
||||
procedure TMyObjectWithHooks.OnBeforeCallHook(const Context: TWebContext; const JSON: TJsonObject);
|
||||
begin
|
||||
// do nothing
|
||||
end;
|
||||
|
||||
procedure TMyObjectWithHooks.OnBeforeRoutingHook(const Context: TWebContext; const JSON: TJsonObject);
|
||||
begin
|
||||
// do nothing
|
||||
end;
|
||||
|
||||
end.
|
||||
|
@ -1,27 +1,3 @@
|
||||
// ***************************************************************************
|
||||
//
|
||||
// Delphi MVC Framework
|
||||
//
|
||||
// Copyright (c) 2010-2020 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 MainControllerU;
|
||||
|
||||
interface
|
||||
@ -34,7 +10,7 @@ type
|
||||
[MVCPath('/api')]
|
||||
TMainController = class(TMVCController)
|
||||
public
|
||||
[MVCPath]
|
||||
[MVCPath('/')]
|
||||
[MVCHTTPMethod([httpGET])]
|
||||
procedure Index;
|
||||
|
||||
|
@ -1,27 +1,3 @@
|
||||
// ***************************************************************************
|
||||
//
|
||||
// Delphi MVC Framework
|
||||
//
|
||||
// Copyright (c) 2010-2020 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 WebModuleU;
|
||||
|
||||
interface
|
||||
@ -113,13 +89,11 @@ begin
|
||||
// Enable Server Signature in response
|
||||
Config[TMVCConfigKey.ExposeServerSignature] := 'false';
|
||||
end);
|
||||
FMVC.AddController(TMainController);
|
||||
FMVC.AddMiddleware(TMVCAnalyticsMiddleware.Create(GetLoggerForAnalytics));
|
||||
FMVC.AddController(TMainController).AddMiddleware(TMVCAnalyticsMiddleware.Create(GetLoggerForAnalytics));
|
||||
FMVC.AddMiddleware(TMVCStaticFilesMiddleware.Create(
|
||||
'/', { StaticFilesPath }
|
||||
TPath.Combine(ExtractFilePath(GetModuleName(HInstance)), 'www'), { DocumentRoot }
|
||||
'index.html', {IndexDocument - Before it was named fallbackresource}
|
||||
False
|
||||
'index.html' {IndexDocument - Before it was named fallbackresource}
|
||||
));
|
||||
end;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user