-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathRestClientUnit.pas
More file actions
225 lines (204 loc) · 7.94 KB
/
Copy pathRestClientUnit.pas
File metadata and controls
225 lines (204 loc) · 7.94 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
unit RestClientUnit;
interface
uses
// RTL
System.SysUtils,
System.Classes,
System.StrUtils,
Vcl.Dialogs,
// mORMot
mORMot,
mORMotHttpClient,
SynCommons,
// Custom
RestMethodsInterfaceUnit;
type
lProtocol = (HTTP_Socket, HTTP_HTTPsys, HTTPsys_SSL, HTTPsys_AES, HTTP_WebSocket, WebSocketBidir_JSON, WebSocketBidir_Binary, WebSocketBidir_BinaryAES, NamedPipe);
lAuthenticationMode = (NoAuthentication, Default, None, HttpBasic, SSPI);
rClientSettings = record
Protocol: lProtocol;
AuthMode: lAuthenticationMode;
HostOrIP: string;
Port: string;
UserLogin: RawUTF8;
UserPassword: RawUTF8;
end;
rConnectionSettings = record
SendTimeout: Cardinal;
ReceiveTimeout: Cardinal;
ConnectTimeout: Cardinal;
procedure LanNetworkPreset();
end;
tRestClient = class
private
fModel: TSQLModel;
fClientSettings: rClientSettings;
fConnectionSettings: rConnectionSettings;
fInitialized: boolean;
public
fClient: TSQLRestClientURI;
RestMethods: IRestMethods;
property Initialized: boolean read fInitialized;
constructor Create();
destructor Destroy(); override;
function Initialize(ClSettings: rClientSettings; ConSettings: rConnectionSettings): boolean; overload;
function Initialize(ClSettings: rClientSettings): boolean; overload;
procedure DeInitialize();
end;
var
RestClient: tRestClient;
implementation
{ rConnectionSettings }
procedure rConnectionSettings.LanNetworkPreset();
begin
SendTimeout := 5000;
ReceiveTimeout := 5000;
ConnectTimeout := 10000;
end;
{ tRestClient }
constructor tRestClient.Create();
begin
fConnectionSettings.LanNetworkPreset();
end;
destructor tRestClient.Destroy();
begin
DeInitialize();
inherited;
end;
function tRestClient.Initialize(ClSettings: rClientSettings; ConSettings: rConnectionSettings): boolean;
begin
fConnectionSettings := ConSettings;
Result := Initialize(ClSettings);
end;
function tRestClient.Initialize(ClSettings: rClientSettings): boolean;
begin
Result := False;
// Destroy current object
DeInitialize();
// Client initialization (for better understanding, each section contain separate code, later should be refactored)
fClientSettings := ClSettings;
fModel := TSQLModel.Create([], ROOT_NAME);
case fClientSettings.Protocol of
HTTP_Socket:
begin
fClient := TSQLHttpClientWinSock.Create(AnsiString(fClientSettings.HostOrIP), AnsiString(fClientSettings.Port), fModel, fConnectionSettings.SendTimeout, fConnectionSettings.ReceiveTimeout,
fConnectionSettings.ConnectTimeout);
TSQLHttpClientWinSock(fClient).KeepAliveMS := CONNECTION_TIMEOUT;
end;
HTTP_HTTPsys:
begin
fClient := TSQLHttpClientWinHTTP.Create(AnsiString(fClientSettings.HostOrIP), AnsiString(fClientSettings.Port), fModel, fConnectionSettings.SendTimeout, fConnectionSettings.ReceiveTimeout,
fConnectionSettings.ConnectTimeout);
TSQLHttpClientWinHTTP(fClient).KeepAliveMS := CONNECTION_TIMEOUT;
TSQLHttpClientWinHTTP(fClient).Compression := [hcSynShaAes];
end;
HTTPsys_SSL:
begin
fClient := TSQLHttpClientWinHTTP.Create(AnsiString(fClientSettings.HostOrIP), AnsiString(fClientSettings.Port), fModel, True, '', '', fConnectionSettings.SendTimeout,
fConnectionSettings.ReceiveTimeout, fConnectionSettings.ConnectTimeout);
TSQLHttpClientWinHTTP(fClient).KeepAliveMS := CONNECTION_TIMEOUT;
TSQLHttpClientWinHTTP(fClient).Compression := [hcSynShaAes];
end;
HTTPsys_AES:
begin
fClient := TSQLHttpClientWinHTTP.Create(AnsiString(fClientSettings.HostOrIP), AnsiString(fClientSettings.Port), fModel, fConnectionSettings.SendTimeout, fConnectionSettings.ReceiveTimeout,
fConnectionSettings.ConnectTimeout);
TSQLHttpClientWinHTTP(fClient).KeepAliveMS := CONNECTION_TIMEOUT;
TSQLHttpClientWinHTTP(fClient).Compression := [hcSynShaAes];
end;
HTTP_WebSocket:
begin
fClient := TSQLHttpClientWebsockets.Create(AnsiString(fClientSettings.HostOrIP), AnsiString(fClientSettings.Port), fModel, fConnectionSettings.SendTimeout, fConnectionSettings.ReceiveTimeout,
fConnectionSettings.ConnectTimeout);
TSQLHttpClientWebsockets(fClient).KeepAliveMS := CONNECTION_TIMEOUT;
end;
WebSocketBidir_JSON:
begin
fClient := TSQLHttpClientWebsockets.Create(AnsiString(fClientSettings.HostOrIP), AnsiString(fClientSettings.Port), fModel, fConnectionSettings.SendTimeout, fConnectionSettings.ReceiveTimeout,
fConnectionSettings.ConnectTimeout);
TSQLHttpClientWebsockets(fClient).KeepAliveMS := CONNECTION_TIMEOUT;
(fClient as TSQLHttpClientWebsockets).WebSocketsUpgrade('', True);
end;
WebSocketBidir_Binary:
begin
fClient := TSQLHttpClientWebsockets.Create(AnsiString(fClientSettings.HostOrIP), AnsiString(fClientSettings.Port), fModel, fConnectionSettings.SendTimeout, fConnectionSettings.ReceiveTimeout,
fConnectionSettings.ConnectTimeout);
TSQLHttpClientWebsockets(fClient).KeepAliveMS := CONNECTION_TIMEOUT;
(fClient as TSQLHttpClientWebsockets).WebSocketsUpgrade('', False);
end;
WebSocketBidir_BinaryAES:
begin
fClient := TSQLHttpClientWebsockets.Create(AnsiString(fClientSettings.HostOrIP), AnsiString(fClientSettings.Port), fModel, fConnectionSettings.SendTimeout, fConnectionSettings.ReceiveTimeout,
fConnectionSettings.ConnectTimeout);
TSQLHttpClientWebsockets(fClient).KeepAliveMS := CONNECTION_TIMEOUT;
(fClient as TSQLHttpClientWebsockets).WebSocketsUpgrade('2141D32ADAD54D9A9DB56000CC9A4A70', False);
end;
NamedPipe:
begin
fClient := TSQLRestClientURINamedPipe.Create(fModel, '\\' + fClientSettings.HostOrIP + '\pipe\mORMot_' + NAMED_PIPE_NAME);
end;
else
begin
DeInitialize();
raise Exception.Create('Selected protocol not available in this build.');
end;
end;
case fClientSettings.AuthMode of
// NoAuthentication
NoAuthentication:
begin
// nothing to do here
end;
// TSQLRestServerAuthenticationDefault
Default:
begin
fClient.SetUser(fClientSettings.UserLogin, fClientSettings.UserPassword);
end;
// TSQLRestServerAuthenticationNone
None:
begin
TSQLRestServerAuthenticationNone.ClientSetUser(fClient, fClientSettings.UserLogin, fClientSettings.UserPassword);
end;
// TSQLRestServerAuthenticationHttpBasic
HttpBasic:
begin
TSQLRestServerAuthenticationHttpBasic.ClientSetUser(fClient, fClientSettings.UserLogin, fClientSettings.UserPassword);
end;
// TSQLRestServerAuthenticationSSPI
SSPI:
begin
TSQLRestServerAuthenticationSSPI.ClientSetUser(fClient, fClientSettings.UserLogin, fClientSettings.UserPassword);
end;
else
begin
DeInitialize();
raise Exception.Create('Selected Authentication mode not available in this build.');
end;
end;
// Preparing
if not fClient.ServerTimeStampSynchronize() then
begin
ShowMessage(UTF8ToString(fClient.LastErrorMessage));
exit;
end;
// Service initialization
fClient.ServiceDefine([IRestMethods], SERVICE_INSTANCE_IMPLEMENTATION);
// Result := Client.Services['RestMethods'].Get(RestServerMethods);
Result := fClient.Services.Resolve(IRestMethods, RestMethods); // same result, but no chance to make mistake with service name
fInitialized := Result;
end;
procedure tRestClient.DeInitialize();
begin
RestMethods := nil;
if Assigned(fClient) then
FreeAndNil(fClient);
if Assigned(fModel) then
FreeAndNil(fModel);
fInitialized := False;
end;
initialization
RestClient := tRestClient.Create();
finalization
if Assigned(RestClient) then
FreeAndNil(RestClient);
end.