-
Notifications
You must be signed in to change notification settings - Fork 186
/
Quick.HttpClient.pas
437 lines (400 loc) · 12 KB
/
Quick.HttpClient.pas
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
{ ***************************************************************************
Copyright (c) 2016-2021 Kike Pérez
Unit : Quick.HttpClient
Description : Json Http Client
Author : Kike Pérez
Version : 1.1
Created : 22/05/2018
Modified : 02/08/2021
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
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 Quick.HttpClient;
{$i QuickLib.inc}
interface
uses
Classes,
SysUtils,
Quick.Commons,
{$IFDEF DELPHIXE8_UP}
System.Net.HttpClient,
System.Net.URLClient,
System.NetConsts,
System.JSON;
{$ELSE}
{$IFDEF DELPHIXE7_UP}
System.JSON,
{$ENDIF}
IdHTTP,
IdException,
{$IFDEF FPC}
fpjson;
{$ELSE}
Data.DBXJSON;
{$ENDIF}
{$ENDIF}
type
IHttpRequestResponse = interface
['{64DC58F7-B551-4619-85E9-D13E781529CD}']
function StatusCode : Integer;
function StatusText : string;
function Response : TJSONObject;
end;
THttpRequestResponse = class(TInterfacedObject,IHttpRequestResponse)
private
fStatusCode : Integer;
fStatusText : string;
fResponse : TJSONObject;
public
{$IFDEF DELPHIXE8_UP}
constructor Create(aResponse : IHTTPResponse; const aContent : string);
{$ELSE}
constructor Create(aResponse : TIdHTTPResponse; const aContent : string);
{$ENDIF}
destructor Destroy; override;
function StatusCode : Integer;
function StatusText : string;
function Response : TJSONObject;
end;
TJsonHttpClient = class
private
{$IFDEF DELPHIXE8_UP}
fHTTPClient : System.Net.HttpClient.THTTPClient;
{$ELSE}
fHTTPClient : TIdHTTP;
{$ENDIF}
fUserAgent : string;
fContentType : string;
fResponseTimeout : Integer;
fConnectionTimeout : Integer;
fHandleRedirects : Boolean;
procedure SetContentType(const aValue: string);
procedure SetUserAgent(const aValue: string);
procedure SetResponseTimeout(const aValue: Integer);
procedure SetConnectionTimeout(const aValue: Integer);
procedure SetHandleRedirects(const aValue: Boolean);
public
constructor Create;
destructor Destroy; override;
property UserAgent : string read fUserAgent write SetUserAgent;
property ContentType : string read fContentType write SetContentType;
property ResponseTimeout : Integer read fResponseTimeout write SetResponseTimeout;
property ConnectionTimeout : Integer read fConnectionTimeout write SetConnectionTimeout;
property HandleRedirects : Boolean read fHandleRedirects write SetHandleRedirects;
function Get(const aURL : string) : IHttpRequestResponse;
function Post(const aURL, aInContent : string; aHeaders : TPairList = nil) : IHttpRequestResponse; overload;
function Post(const aURL : string; aInContent : TStream) : IHttpRequestResponse; overload;
function Post(const aURL : string; aJsonContent : TJsonObject) : IHttpRequestResponse; overload;
function Put(const aURL, aInContent : string) : IHttpRequestResponse;
end;
implementation
const
DEF_USER_AGENT = 'XLHttpClient';
constructor TJsonHttpClient.Create;
begin
{$IFDEF DELPHIXE8_UP}
fHTTPClient := THTTPClient.Create;
fHTTPClient.ContentType := 'application/json';
fHTTPClient.UserAgent := DEF_USER_AGENT;
{$ELSE}
fHTTPClient := TIdHTTP.Create(nil);
fHTTPClient.Request.ContentType := 'application/json';
fHTTPClient.Request.UserAgent := DEF_USER_AGENT;
{$ENDIF}
end;
destructor TJsonHttpClient.Destroy;
begin
fHTTPClient.Free;
inherited;
end;
function TJsonHttpClient.Get(const aURL : string) : IHttpRequestResponse;
var
{$IFDEF DELPHIXE8_UP}
resp : IHTTPResponse;
{$ELSE}
resp : TIdHTTPResponse;
{$ENDIF}
bodycontent : TStringStream;
responsecontent : TStringStream;
begin
bodycontent := TStringStream.Create('',TEncoding.UTF8);
try
responsecontent := TStringStream.Create('',TEncoding.UTF8);
try
{$IFDEF DELPHIXE8_UP}
resp := fHTTPClient.Get(aURL,responsecontent,nil);
{$ELSE}
{$If Defined(FPC) OR Not Defined(DELPHIXE8_UP)}
fHTTPClient.Get(aURL,responsecontent);
{$ELSE}
fHTTPClient.Get(aURL,responsecontent,nil);
{$ENDIF}
resp := fHTTPClient.Response;
{$ENDIF}
Result := THttpRequestResponse.Create(resp,responsecontent.DataString);
finally
responsecontent.Free;
end;
finally
bodycontent.Free;
end;
end;
function TJsonHttpClient.Post(const aURL, aInContent : string; aHeaders : TPairList = nil) : IHttpRequestResponse;
var
pair : TPairItem;
{$IFDEF DELPHIXE8_UP}
resp : IHTTPResponse;
headers : TArray<TNameValuePair>;
{$ELSE}
resp : TIdHTTPResponse;
{$ENDIF}
responsecontent : TStringStream;
postcontent : TStringStream;
begin
postcontent := TStringStream.Create(Utf8Encode(aInContent));
try
//postcontent.WriteString(aInContent);
responsecontent := TStringStream.Create('',TEncoding.UTF8);
try
{$IFDEF DELPHIXE8_UP}
if aHeaders <> nil then
begin
for pair in aHeaders do
begin
headers := headers + [TNameValuePair.Create(pair.Name,pair.Value)];
end;
end;
resp := fHTTPClient.Post(aURL,postcontent,responsecontent,headers);
{$ELSE}
if aHeaders <> nil then
begin
for pair in aHeaders do
begin
fHttpClient.Request.CustomHeaders.Values[pair.Name] := pair.Value;
end;
end;
{$IFDEF FPC}
try
fHTTPClient.Post(aURL,postcontent,responsecontent);
fHTTPClient.Disconnect(False);
except
on E : Exception do
begin
if e.ClassType <> EIdConnClosedGracefully then raise;
end;
end;
{$ELSE}
fHTTPClient.Post(aURL,postcontent,responsecontent);
{$ENDIF}
resp := fHTTPClient.Response;
{$ENDIF}
Result := THttpRequestResponse.Create(resp,responsecontent.DataString);
finally
responsecontent.Free;
end;
finally
postcontent.Free;
end;
end;
function TJsonHttpClient.Post(const aURL : string; aInContent : TStream) : IHttpRequestResponse;
var
{$IFDEF DELPHIXE8_UP}
resp : IHTTPResponse;
{$ELSE}
resp : TIdHTTPResponse;
{$ENDIF}
responsecontent : TStringStream;
begin
//postcontent.WriteString(aInContent);
responsecontent := TStringStream.Create('',TEncoding.UTF8);
try
{$IFDEF DELPHIXE8_UP}
resp := fHTTPClient.Post(aURL,aInContent,responsecontent);
{$ELSE}
{$IFDEF FPC}
try
fHTTPClient.Post(aURL,aInContent,responsecontent);
fHTTPClient.Disconnect(False);
except
on E : Exception do
begin
if e.ClassType <> EIdConnClosedGracefully then raise;
end;
end;
{$ELSE}
fHTTPClient.Post(aURL,aInContent,responsecontent);
{$ENDIF}
resp := fHTTPClient.Response;
{$ENDIF}
Result := THttpRequestResponse.Create(resp,responsecontent.DataString);
finally
responsecontent.Free;
end;
end;
function TJsonHttpClient.Post(const aURL : string; aJsonContent : TJsonObject) : IHttpRequestResponse;
begin
{$IFDEF DELPHIXE8_UP}
Result := Self.Post(aURL,aJsonContent.ToJSON);
{$ELSE}
{$IFDEF FPC}
Result := Self.Post(aURL,aJsonContent.AsJson);
{$ELSE}
Result := Self.Post(aURL,aJsonContent.ToString);
{$ENDIF}
{$ENDIF}
end;
function TJsonHttpClient.Put(const aURL, aInContent : string) : IHttpRequestResponse;
var
{$IFDEF DELPHIXE8_UP}
resp : IHTTPResponse;
{$ELSE}
resp : TIdHTTPResponse;
{$ENDIF}
responsecontent : TStringStream;
postcontent : TStringStream;
begin
postcontent := TStringStream.Create(Utf8Encode(aInContent));
try
//postcontent.WriteString(aInContent);
responsecontent := TStringStream.Create('',TEncoding.UTF8);
try
{$IFDEF DELPHIXE8_UP}
resp := fHTTPClient.Put(aURL,postcontent,responsecontent);
{$ELSE}
{$IFDEF FPC}
try
fHTTPClient.Put(aURL,postcontent,responsecontent);
fHTTPClient.Disconnect(False);
except
on E : Exception do
begin
if e.ClassType <> EIdConnClosedGracefully then raise;
end;
end;
{$ELSE}
fHTTPClient.Post(aURL,postcontent,responsecontent);
{$ENDIF}
resp := fHTTPClient.Response;
{$ENDIF}
Result := THttpRequestResponse.Create(resp,responsecontent.DataString);
finally
responsecontent.Free;
end;
finally
postcontent.Free;
end;
end;
procedure TJsonHttpClient.SetConnectionTimeout(const aValue: Integer);
begin
fConnectionTimeout := aValue;
{$IFDEF DELPHIXE8_UP}
{$IFDEF DELPHIRX102_UP} //in previous versions don't exists ConnectionTimeout property
fHTTPClient.ConnectionTimeout := aValue;
{$ENDIF}
{$ELSE}
fHTTPClient.ConnectTimeout := aValue;
{$ENDIF}
end;
procedure TJsonHttpClient.SetContentType(const aValue: string);
begin
fContentType := aValue;
{$IFDEF DELPHIXE8_UP}
fHTTPClient.ContentType := aValue;
{$ELSE}
fHTTPClient.Request.ContentType := aValue;
{$ENDIF}
end;
procedure TJsonHttpClient.SetHandleRedirects(const aValue: Boolean);
begin
fHandleRedirects := aValue;
{$IFDEF DELPHIXE8_UP}
fHTTPClient.HandleRedirects := aValue;
{$ELSE}
fHTTPClient.HandleRedirects := aValue;
{$ENDIF}
end;
procedure TJsonHttpClient.SetResponseTimeout(const aValue: Integer);
begin
fResponseTimeout := aValue;
{$IFDEF DELPHIXE8_UP}
{$IFDEF DELPHIRX102_UP} //in previous versions don't exist ResponseTimeout property
fHTTPClient.ResponseTimeout := aValue;
{$ENDIF}
{$ELSE}
fHTTPClient.ReadTimeout := aValue;
{$ENDIF}
end;
procedure TJsonHttpClient.SetUserAgent(const aValue: string);
begin
fUserAgent := aValue;
{$IFDEF DELPHIXE8_UP}
fHTTPClient.UserAgent := aValue;
{$ELSE}
fHTTPClient.Request.UserAgent := aValue;
{$ENDIF}
end;
{ THttpRequestResponse }
{$IFDEF DELPHIXE8_UP}
constructor THttpRequestResponse.Create(aResponse: IHTTPResponse; const aContent : string);
begin
fStatusCode := aResponse.StatusCode;
fStatusText := aResponse.StatusText;
if ((aContent <> '') and
(aContent.StartsWith('{') or (aContent.StartsWith('[')))
) then fResponse := TJSONObject.ParseJSONValue(aContent) as TJSONObject;
//if response is not json, get as json result
if fResponse = nil then
begin
fResponse := TJSONObject.Create;
fResponse.AddPair('Result',aContent);
end;
end;
{$ELSE}
constructor THttpRequestResponse.Create(aResponse : TIdHTTPResponse; const aContent : string);
begin
fStatusCode := aResponse.ResponseCode;
fStatusText := aResponse.ResponseText;
{$If Defined(FPC) OR Defined(DELPHIXE8_UP)}
if (aContent.Contains('{')) and (aContent.Contains('}')) then fResponse := GetJSON(aContent) as TJsonObject;
{$ELSE}
if (aContent.Contains('{')) and (aContent.Contains('}')) then fResponse:= TJsonObject.ParseJSONValue(aContent) as TJsonObject;
{$ENDIF}
//if response is not json, get as json result
if fResponse = nil then
begin
fResponse := TJSONObject.Create;
{$IFDEF DELPHIXE4_UP}
fResponse.AddPair('Result',aContent);
{$ELSE}
fResponse.Add('Result',aContent);
{$ENDIF}
end;
end;
{$ENDIF}
destructor THttpRequestResponse.Destroy;
begin
if Assigned(fResponse) then fResponse.Free;
inherited;
end;
function THttpRequestResponse.Response: TJSONObject;
begin
Result := fResponse;
end;
function THttpRequestResponse.StatusCode: Integer;
begin
Result := fStatusCode;
end;
function THttpRequestResponse.StatusText: string;
begin
Result := fStatusText;
end;
end.