-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathRestClientFormUnit.pas
More file actions
315 lines (282 loc) · 8.79 KB
/
Copy pathRestClientFormUnit.pas
File metadata and controls
315 lines (282 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
unit RestClientFormUnit;
// mORMot RESTful API test case 1.02
interface
uses
// RTL
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Classes,
System.Generics.Collections,
Vcl.Forms,
Vcl.Dialogs,
Vcl.Controls,
Vcl.StdCtrls,
Vcl.ExtCtrls,
// mORMot
mORMot,
SynLog,
SynCommons,
mORMotHttpClient, // tmp
// Custom
RestClientUnit,
RestMethodsInterfaceUnit;
type
TCustomRecord = record helper for rCustomRecord
procedure FillFromClient();
end;
lClientAction = (Auto, Start, Stop, Restart);
TForm1 = class(TForm)
EditServerAdress: TEdit;
EditServerPort: TEdit;
ButtonStartStop: TButton;
TimerRefreshLogMemo: TTimer;
GroupBoxIRestMethods: TGroupBox;
ButtonMethHelloWorld: TButton;
ButtonMethSum: TButton;
ButtonGetCustomRecord: TButton;
LabelAuthenticationMode: TLabel;
ComboBoxAuthentication: TComboBox;
ButtonMethSendCustomRecord: TButton;
MemoLog: TMemo;
ButtonCLS: TButton;
CheckBoxAutoScroll: TCheckBox;
CheckBoxDisableLog: TCheckBox;
ButtonMethSendMultipleCustomRecords: TButton;
LabelProtocol: TLabel;
ComboBoxProtocol: TComboBox;
EditUserLogin: TEdit;
EditUserPassword: TEdit;
Label1: TLabel;
Label2: TLabel;
ButtonMethGetMethodCustomResult: TButton;
procedure FormCreate(Sender: TObject);
procedure ButtonStartStopClick(Sender: TObject);
procedure ButtonCLSClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure TimerRefreshLogMemoTimer(Sender: TObject);
procedure ButtonMethHelloWorldClick(Sender: TObject);
procedure ButtonMethSumClick(Sender: TObject);
procedure ButtonGetCustomRecordClick(Sender: TObject);
procedure ComboBoxAuthenticationChange(Sender: TObject);
procedure ButtonMethSendCustomRecordClick(Sender: TObject);
procedure CheckBoxDisableLogClick(Sender: TObject);
procedure ButtonMethSendMultipleCustomRecordsClick(Sender: TObject);
procedure ComboBoxProtocolChange(Sender: TObject);
procedure ButtonMethGetMethodCustomResultClick(Sender: TObject);
private
{ Private declarations }
function LogEvent(Sender: TTextWriter; Level: TSynLogInfo; const Text: RawUTF8): boolean;
procedure StartStopClient(ClientAction: lClientAction = Auto);
public
{ Public declarations }
end;
TLocalLog = class
Level: TSynLogInfo;
Text: RawUTF8;
end;
var
Form1: TForm1;
LogThreadSafeList: TThreadList<TLocalLog>;
implementation
{$R *.dfm}
{ TCustomResult }
procedure TCustomRecord.FillFromClient();
var
i: Integer;
begin
ResultCode := 200;
ResultStr := 'Awesome';
ResultTimeStamp := Now();
SetLength(ResultArray, 3);
for i := 0 to 2 do
ResultArray[i] := 'str_' + i.ToString();
end;
{ Form1 }
// On Form1 create
procedure TForm1.FormCreate(Sender: TObject);
begin
// Create thread safe List with log data class
LogThreadSafeList := TThreadList<TLocalLog>.Create();
// Enable logging
with TSQLLog.Family do
begin
Level := LOG_VERBOSE;
EchoCustom := LogEvent;
NoFile := True;
end;
end;
// On Form1 destory
procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
List: TList<TLocalLog>;
begin
// Clear and destroy LogThreadSafeList
List := LogThreadSafeList.LockList();
for i := 0 to List.Count - 1 do
List.Items[i].Free;
List.Clear;
LogThreadSafeList.UnlockList();
FreeAndNil(LogThreadSafeList);
end;
{ CLIENT EVENTS, START / STOP }
// Depends on the client status, start or stop client (create or destroy objects)
procedure TForm1.StartStopClient(ClientAction: lClientAction = Auto);
var
pClientCreated: boolean;
ClientSettings: rClientSettings;
begin
pClientCreated := RestClient.Initialized;
// Unload current client if required
RestClient.DeInitialize();
// Create client if required
if ((ClientAction = lClientAction.Auto) and not pClientCreated) or ((ClientAction = lClientAction.Restart) and pClientCreated) or (ClientAction = lClientAction.Start) then
begin
ClientSettings.Protocol := lProtocol(ComboBoxProtocol.ItemIndex);
ClientSettings.AuthMode := lAuthenticationMode(ComboBoxAuthentication.ItemIndex);;
ClientSettings.HostOrIP := EditServerAdress.Text;
ClientSettings.Port := EditServerPort.Text;
ClientSettings.UserLogin := StringToUTF8(EditUserLogin.Text);
ClientSettings.UserPassword := StringToUTF8(EditUserPassword.Text);
RestClient.Initialize(ClientSettings);
end;
end;
// Processing mORMot log event
function TForm1.LogEvent(Sender: TTextWriter; Level: TSynLogInfo; const Text: RawUTF8): boolean;
var
List: TList<TLocalLog>;
LogEventData: TLocalLog;
begin
Result := False;
if Assigned(LogThreadSafeList) then
begin
List := LogThreadSafeList.LockList;
try
LogEventData := TLocalLog.Create();
LogEventData.Level := Level;
LogEventData.Text := Text;
List.Add(LogEventData);
Result := True;
finally
LogThreadSafeList.UnlockList();
end;
end;
end;
{ UI }
// Grabbing new events from thread safe list
procedure TForm1.TimerRefreshLogMemoTimer(Sender: TObject);
var
List: TList<TLocalLog>;
i: Integer;
begin
if Assigned(LogThreadSafeList) then
begin
List := LogThreadSafeList.LockList();
try
if Assigned(Form1) and not Application.Terminated and (List.Count > 0) then
begin
for i := 0 to List.Count - 1 do
begin
Form1.MemoLog.Lines.BeginUpdate();
Form1.MemoLog.Lines.Add(string(List.Items[i].Text));
Form1.MemoLog.Lines.EndUpdate();
List.Items[i].Free;
end;
List.Clear();
if CheckBoxAutoScroll.Checked then
SendMessage(Form1.MemoLog.Handle, WM_VSCROLL, SB_BOTTOM, 0);
end;
finally
LogThreadSafeList.UnlockList();
end;
end;
if RestClient.Initialized then
ButtonStartStop.Caption := 'Stop client'
else
ButtonStartStop.Caption := 'Start client';
end;
// Changing client protocol
procedure TForm1.ComboBoxProtocolChange(Sender: TObject);
begin
EditServerPort.Enabled := lProtocol(ComboBoxProtocol.ItemIndex) <> lProtocol.NamedPipe;
StartStopClient(Restart);
end;
// Changing client authentication mode
procedure TForm1.ComboBoxAuthenticationChange(Sender: TObject);
begin
if lAuthenticationMode(ComboBoxAuthentication.ItemIndex) = lAuthenticationMode.SSPI then
begin
EditUserLogin.Text := '';
EditUserPassword.Text := '';
end;
StartStopClient(Restart);
end;
// Clears log memo
procedure TForm1.ButtonCLSClick(Sender: TObject);
begin
MemoLog.Clear;
end;
// Button start stop client
procedure TForm1.ButtonStartStopClick(Sender: TObject);
begin
StartStopClient();
end;
// Checkbox Enable/Disable logging to memo (slow down performance when enabled)
procedure TForm1.CheckBoxDisableLogClick(Sender: TObject);
begin
if not CheckBoxDisableLog.Checked then
TSQLLog.Family.Level := LOG_VERBOSE
else
TSQLLog.Family.Level := [];
end;
{ IRestMethods execution }
procedure TForm1.ButtonMethHelloWorldClick(Sender: TObject);
begin
if Assigned(RestClient.RestMethods) then
RestClient.RestMethods.HelloWorld();
end;
procedure TForm1.ButtonMethSendCustomRecordClick(Sender: TObject);
var
CustomResult: rCustomRecord;
begin
if Assigned(RestClient.RestMethods) then
begin
CustomResult.FillFromClient();
RestClient.RestMethods.SendCustomRecord(CustomResult);
end;
end;
procedure TForm1.ButtonMethSumClick(Sender: TObject);
begin
if Assigned(RestClient.RestMethods) then
RestClient.RestMethods.Sum(Random(100) + 0.6, Random(100) + 0.3);
end;
procedure TForm1.ButtonGetCustomRecordClick(Sender: TObject);
var
CustomResult: rCustomRecord;
begin
if Assigned(RestClient.RestMethods) then
CustomResult := RestClient.RestMethods.GetCustomRecord();
end;
procedure TForm1.ButtonMethSendMultipleCustomRecordsClick(Sender: TObject);
var
cr: rCustomRecord;
ccr: rCustomComplicatedRecord;
begin
if Assigned(RestClient.RestMethods) then
begin
cr.FillFromClient();
ccr.SimpleString := 'Simple string, Простая строка, 単純な文字列';
ccr.SimpleInteger := 100500;
ccr.AnotherRecord := cr;
RestClient.RestMethods.SendMultipleCustomRecords(cr, ccr);
end;
end;
procedure TForm1.ButtonMethGetMethodCustomResultClick(Sender: TObject);
var
ServiceCustomAnswer: TServiceCustomAnswer;
begin
if Assigned(RestClient.RestMethods) then
ServiceCustomAnswer := RestClient.RestMethods.GetMethodCustomResult();
end;
end.