-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathFB4D.Request.pas
720 lines (688 loc) · 27.5 KB
/
FB4D.Request.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
{******************************************************************************}
{ }
{ Delphi FB4D Library }
{ Copyright (c) 2018-2024 Christoph Schneider }
{ Schneider Infosystems AG, Switzerland }
{ https://github.com/SchneiderInfosystems/FB4D }
{ }
{******************************************************************************}
{ }
{ 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 FB4D.Request;
interface
uses
System.Classes, System.JSON, System.SysUtils,
System.Net.URLClient, System.Net.HttpClient,
System.Generics.Collections,
REST.Client, REST.Types,
IPPeerClient,
FB4D.Interfaces, FB4D.Response;
type
TResendRequest = class;
TFirebaseRequest = class(TInterfacedObject, IFirebaseRequest)
private
class var fResendRequest: TResendRequest;
procedure RESTRequestAfterExecute(Sender: TCustomRESTRequest);
procedure RESTRequestHTTPProtocolError(Sender: TCustomRESTRequest);
function EncodeToken: string;
function InternalSendRequestSynchronous(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TStream; ContentType: TRESTContentType;
QueryParams: TQueryParams = nil;
TokenMode: TTokenMode = tmBearer): IFirebaseResponse; overload;
procedure InternalSendRequest(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TJSONValue;
QueryParams: TQueryParams; TokenMode: TTokenMode;
OnResponse: TOnFirebaseResp; OnRequestError: TOnRequestError;
OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess;
OnSuccess: TOnSuccess); overload;
procedure InternalSendRequest(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TStream; ContentType: TRESTContentType;
QueryParams: TQueryParams; TokenMode: TTokenMode;
OnResponse: TOnFirebaseResp; OnRequestError: TOnRequestError;
OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess;
OnSuccess: TOnSuccess); overload;
procedure SendRequestAfterTokenRefresh(TokenRefreshed: boolean);
procedure SendStreamRequestAfterTokenRefresh(TokenRefreshed: boolean);
protected
fBaseURI: string;
fRequestID: string;
fAuth: IFirebaseAuthentication;
public
constructor Create(const BaseURI: string; const RequestID: string = '';
Auth: IFirebaseAuthentication = nil);
procedure SendRequest(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TJSONValue;
QueryParams: TQueryParams; TokenMode: TTokenMode;
OnResponse: TOnFirebaseResp; OnRequestError: TOnRequestError;
OnSuccess: TOnSuccess); overload;
procedure SendRequest(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TJSONValue;
QueryParams: TQueryParams; TokenMode: TTokenMode;
OnResponse: TOnFirebaseResp;
OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess;
OnSuccess: TOnSuccess); overload;
procedure SendRequest(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TStream; ContentType: TRESTContentType;
QueryParams: TQueryParams; TokenMode: TTokenMode;
OnResponse: TOnFirebaseResp; OnRequestError: TOnRequestError;
OnSuccess: TOnSuccess); overload;
procedure SendRequest(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TStream; ContentType: TRESTContentType;
QueryParams: TQueryParams; TokenMode: TTokenMode;
OnResponse: TOnFirebaseResp;
OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess;
OnSuccess: TOnSuccess); overload;
function SendRequestSynchronous(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TJSONValue = nil;
QueryParams: TQueryParams = nil;
TokenMode: TTokenMode = tmBearer): IFirebaseResponse; overload;
function SendRequestSynchronous(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TStream; ContentType: TRESTContentType;
QueryParams: TQueryParams = nil;
TokenMode: TTokenMode = tmBearer): IFirebaseResponse; overload;
end;
TResendRequest = class
strict private
fBaseURI: string;
fRequestID: string;
fAuth: IFirebaseAuthentication;
fResParams: TRequestResourceParam;
fMethod: TRESTRequestMethod;
fData: TJSONValue;
fDataStream: TStream;
fContentType: TRESTContentType;
fQueryParams: TQueryParams;
fTokenMode: TTokenMode;
fOnResponse: TOnFirebaseResp;
fOnSuccess: TOnSuccess;
fOnRequestError: TOnRequestError;
fOnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess;
public
constructor Create(Req: TFirebaseRequest; ResParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TJSONValue; QueryParams: TQueryParams;
TokenMode: TTokenMode; OnResponse: TOnFirebaseResp;
OnRequestError: TOnRequestError; OnSuccess: TOnSuccess); overload;
constructor Create(Req: TFirebaseRequest; ResParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TJSONValue; QueryParams: TQueryParams;
TokenMode: TTokenMode; OnResponse: TOnFirebaseResp;
OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess; OnSuccess: TOnSuccess);
overload;
constructor Create(Req: TFirebaseRequest; ResParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TStream; ContentType: TRESTContentType;
QueryParams: TQueryParams; TokenMode: TTokenMode;
OnResponse: TOnFirebaseResp; OnRequestError: TOnRequestError;
OnSuccess: TOnSuccess); overload;
constructor Create(Req: TFirebaseRequest; ResParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TStream; ContentType: TRESTContentType;
QueryParams: TQueryParams; TokenMode: TTokenMode; OnResponse: TOnFirebaseResp;
OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess;
OnSuccess: TOnSuccess); overload;
destructor Destroy; override;
property BaseURI: string read fBaseURI;
property RequestID: string read fRequestID;
property Auth: IFirebaseAuthentication read fAuth;
property ResParams: TRequestResourceParam read fResParams;
property Method: TRESTRequestMethod read fMethod;
property Data: TJSONValue read fData;
property DataStream: TStream read fDataStream;
property ContentType: TRESTContentType read fContentType;
property QueryParams: TQueryParams read fQueryParams;
property TokenMode: TTokenMode read fTokenMode;
property OnResponse: TOnFirebaseResp read fOnResponse;
property OnSuccess: TOnSuccess read fOnSuccess;
property OnRequestError: TOnRequestError read fOnRequestError;
property OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess
read fOnRequestErrorWithOnSuccess;
end;
implementation
uses
System.NetConsts, System.NetEncoding, System.StrUtils, System.TypInfo,
FB4D.Helpers;
resourcestring
rsTokenRefreshFailed = 'Authorisation token refresh failed';
rsConnectionToServerBroken = 'Connection to server broken';
{ TResendRequest }
constructor TResendRequest.Create(Req: TFirebaseRequest;
ResParams: TRequestResourceParam; Method: TRESTRequestMethod;
Data: TJSONValue; QueryParams: TQueryParams; TokenMode: TTokenMode;
OnResponse: TOnFirebaseResp; OnRequestError: TOnRequestError;
OnSuccess: TOnSuccess);
var
c: integer;
key: string;
begin
fBaseURI := Req.fBaseURI;
fRequestID := Req.fRequestID;
fAuth := Req.fAuth;
SetLength(fResParams, length(ResParams));
for c := low(ResParams) to high(ResParams) do
fResParams[c] := ResParams[c];
fMethod := Method;
if assigned(Data) then
fData := Data.Clone as TJSONValue;
if assigned(QueryParams) then
begin
fQueryParams := TQueryParams.Create;
for key in QueryParams.Keys do
fQueryParams.Add(key, QueryParams.Items[key]);
end;
fTokenMode := TokenMode;
fOnResponse := OnResponse;
fOnRequestError := OnRequestError;
fOnSuccess := OnSuccess;
end;
constructor TResendRequest.Create(Req: TFirebaseRequest; ResParams: TRequestResourceParam; Method: TRESTRequestMethod;
Data: TJSONValue; QueryParams: TQueryParams; TokenMode: TTokenMode; OnResponse: TOnFirebaseResp;
OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess; OnSuccess: TOnSuccess);
var
c: integer;
key: string;
begin
fBaseURI := Req.fBaseURI;
fRequestID := Req.fRequestID;
fAuth := Req.fAuth;
SetLength(fResParams, length(ResParams));
for c := low(ResParams) to high(ResParams) do
fResParams[c] := ResParams[c];
fMethod := Method;
if assigned(Data) then
fData := Data.Clone as TJSONValue;
if assigned(QueryParams) then
begin
fQueryParams := TQueryParams.Create;
for key in QueryParams.Keys do
fQueryParams.Add(key, QueryParams.Items[key]);
end;
fTokenMode := TokenMode;
fOnResponse := OnResponse;
fOnRequestErrorWithOnSuccess := OnRequestErrorWithOnSuccess;
fOnSuccess := OnSuccess;
end;
constructor TResendRequest.Create(Req: TFirebaseRequest;
ResParams: TRequestResourceParam; Method: TRESTRequestMethod; Data: TStream;
ContentType: TRESTContentType; QueryParams: TQueryParams;
TokenMode: TTokenMode; OnResponse: TOnFirebaseResp;
OnRequestError: TOnRequestError; OnSuccess: TOnSuccess);
var
c: integer;
key: string;
begin
fBaseURI := Req.fBaseURI;
fRequestID := Req.fRequestID;
fAuth := Req.fAuth;
SetLength(fResParams, length(ResParams));
for c := low(ResParams) to high(ResParams) do
fResParams[c] := ResParams[c];
fMethod := Method;
fDataStream := Data;
fContentType := ContentType;
if assigned(QueryParams) then
begin
fQueryParams := TQueryParams.Create;
for key in QueryParams.Keys do
fQueryParams.Add(key, QueryParams.Items[key]);
end;
fTokenMode := TokenMode;
fOnResponse := OnResponse;
fOnRequestError := OnRequestError;
fOnSuccess := OnSuccess;
end;
constructor TResendRequest.Create(Req: TFirebaseRequest; ResParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TStream; ContentType: TRESTContentType; QueryParams: TQueryParams;
TokenMode: TTokenMode; OnResponse: TOnFirebaseResp; OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess;
OnSuccess: TOnSuccess);
var
c: integer;
key: string;
begin
fBaseURI := Req.fBaseURI;
fRequestID := Req.fRequestID;
fAuth := Req.fAuth;
SetLength(fResParams, length(ResParams));
for c := low(ResParams) to high(ResParams) do
fResParams[c] := ResParams[c];
fMethod := Method;
fDataStream := Data;
fContentType := ContentType;
if assigned(QueryParams) then
begin
fQueryParams := TQueryParams.Create;
for key in QueryParams.Keys do
fQueryParams.Add(key, QueryParams.Items[key]);
end;
fTokenMode := TokenMode;
fOnResponse := OnResponse;
fOnRequestErrorWithOnSuccess := OnRequestErrorWithOnSuccess;
fOnSuccess := OnSuccess;
end;
destructor TResendRequest.Destroy;
begin
if assigned(fData) then
fData.Free;
if assigned(fQueryParams) then
fQueryParams.Free;
inherited;
end;
{ TFirestoreRequest }
constructor TFirebaseRequest.Create(const BaseURI, RequestID: string;
Auth: IFirebaseAuthentication);
begin
inherited Create;
fBaseURI := BaseURI;
fRequestID := RequestID;
fAuth := Auth;
end;
function TFirebaseRequest.InternalSendRequestSynchronous(
ResourceParams: TRequestResourceParam; Method: TRESTRequestMethod;
Data: TStream; ContentType: TRESTContentType;
QueryParams: TQueryParams;
TokenMode: TTokenMode): IFirebaseResponse;
var
Client: THTTPClient;
LResp: IHTTPResponse;
URL: string;
begin
Client := THTTPClient.Create;
try
Client.ContentType := ContentTypeToString(ContentType);
if assigned(fAuth) and (TokenMode = tmBearer) and fAuth.Authenticated then
Client.CustomHeaders['Authorization'] := 'Bearer ' + EncodeToken;
URL := fBaseURI + TFirebaseHelpers.EncodeResourceParams(ResourceParams);
if TokenMode = tmAuthParam then
URL := URL + TFirebaseHelpers.EncodeQueryParamsWithToken(QueryParams,
EncodeToken)
else
URL := URL + TFirebaseHelpers.EncodeQueryParams(QueryParams);
{$IFDEF DEBUG}
TFirebasehelpers.Log('FirebaseRequest.InternalSendRequestSynchronous ' +
GetEnumName(TypeInfo(TRESTRequestMethod), ord(Method)) + ': ' + URL);
{$ENDIF}
case Method of
rmPut:
LResp := Client.Put(URL, Data);
rmPost:
LResp := Client.Post(URL, Data);
rmPatch:
LResp := Client.Patch(URL, Data);
rmGet:
LResp := Client.Get(URL);
rmDelete:
LResp := Client.Delete(URL);
end;
result := TFirebaseResponse.Create(LResp);
{$IFDEF DEBUG}
if not result.StatusOk then
begin
TFirebaseHelpers.Log(
'FirebaseRequest.InternalSendRequestSynchronous failed: ' +
IntToStr(result.StatusCode) + ' / ' + result.StatusText);
TFirebaseHelpers.Log(' Content from ' + URL);
TFirebaseHelpers.Log(' > ' + result.ContentAsString);
end;
{$ENDIF}
finally
Client.Free;
end;
end;
procedure TFirebaseRequest.InternalSendRequest(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TJSONValue; QueryParams: TQueryParams;
TokenMode: TTokenMode; OnResponse: TOnFirebaseResp; OnRequestError: TOnRequestError;
OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess; OnSuccess: TOnSuccess);
var
Client: TRestClient;
Request: TRestRequest;
Response: TRESTResponse;
RequestID: string;
begin
Client := TRestClient.Create(fBaseURI);
Request := TRestRequest.Create(Client);
Response := TRESTResponse.Create(Client);
Request.OnAfterExecute := RESTRequestAfterExecute;
Request.OnHTTPProtocolError := RESTRequestHTTPProtocolError;
Request.Client := Client;
Request.Method := Method;
Request.Response := Response;
if assigned(fAuth) and (TokenMode = tmBearer) and fAuth.Authenticated then
Request.AddAuthParameter('Authorization', 'Bearer ' + EncodeToken,
pkHTTPHEADER, [poDoNotEncode]);
if Data <> nil then
Request.Body.Add(Data.ToJSON, TRESTContentType.ctAPPLICATION_JSON);
if assigned(fAuth) and (TokenMode = tmAuthParam) and fAuth.Authenticated then
Request.Resource := TFirebaseHelpers.EncodeResourceParams(ResourceParams) +
TFirebaseHelpers.EncodeQueryParamsWithToken(QueryParams, EncodeToken)
else
Request.Resource := TFirebaseHelpers.EncodeResourceParams(ResourceParams) +
TFirebasehelpers.EncodeQueryParams(QueryParams);
Request.URLAlreadyEncoded := true;
{$IFDEF DEBUG}
TFirebasehelpers.Log('FirebaseRequest.InternalSendRequest ' +
GetEnumName(TypeInfo(TRESTRequestMethod), ord(Request.Method)) + ': ' +
Request.GetFullRequestURL(true));
{$ENDIF}
RequestID := fRequestID;
Request.ExecuteAsync(
procedure // Completion Handler
begin
try
try
if assigned(OnResponse) and not TFirebaseHelpers.AppIsTerminated then
if assigned(OnRequestError) then
OnResponse(RequestID, TFirebaseResponse.Create(Response,
OnRequestError, OnSuccess))
else if assigned(OnRequestErrorWithOnSuccess) then
OnResponse(RequestID, TFirebaseResponse.Create(Response,
OnRequestErrorWithOnSuccess, OnSuccess))
except
on e: exception do
if assigned(OnRequestError) then
OnRequestError(RequestID, e.Message)
else if assigned(OnRequestErrorWithOnSuccess) then
OnRequestErrorWithOnSuccess(RequestID, e.Message, OnSuccess)
else
TFirebaseHelpers.LogFmt(
'Exception in FirebaseRequest.InternalSendRequest %s: %s',
[RequestID, e.Message]);
end;
finally
FreeAndNil(Response);
FreeAndNil(Request);
FreeAndNil(Client);
end;
end,
true, true,
procedure(Obj: TObject) // Error Handler
begin
try
if Assigned(Obj) and (Obj is Exception) and assigned(OnRequestError) then
begin
{$IFDEF MSWINDOWS}
if (Pos('(12030)', Exception(Obj).Message) > 1) or
(Pos('(12007)', Exception(Obj).Message) > 1) then
// Misleading error message from WinAPI
// 12030: ERROR_INTERNET_CONNECTION_ABORTED
// "The connection with the server has been terminated."
// German "Die Serververbindung wurde aufgrund eines Fehlers beendet."
// 12007: ERROR_INTERNET_NAME_NOT_RESOLVED
OnRequestError(RequestID, rsConnectionToServerBroken)
else
{$ENDIF}
OnRequestError(RequestID, Exception(Obj).Message);
end;
finally
FreeAndNil(Response);
FreeAndNil(Request);
FreeAndNil(Client);
end;
end
);
end;
procedure TFirebaseRequest.InternalSendRequest(
ResourceParams: TRequestResourceParam; Method: TRESTRequestMethod;
Data: TStream; ContentType: TRESTContentType; QueryParams:TQueryParams;
TokenMode: TTokenMode; OnResponse: TOnFirebaseResp;
OnRequestError: TOnRequestError;
OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess;
OnSuccess: TOnSuccess);
var
Client: TRestClient;
Request: TRestRequest;
Response: TRESTResponse;
RequestID: string;
begin
Client := TRestClient.Create(fBaseURI);
Request := TRestRequest.Create(Client);
Response := TRESTResponse.Create(Client);
Request.OnAfterExecute := RESTRequestAfterExecute;
Request.OnHTTPProtocolError := RESTRequestHTTPProtocolError;
Request.Client := Client;
Request.Method := Method;
Request.Response := Response;
if assigned(fAuth) and (TokenMode = tmBearer) and fAuth.Authenticated then
Request.AddAuthParameter('Authorization', 'Bearer ' + EncodeToken,
pkHTTPHEADER, [poDoNotEncode]);
if Data <> nil then
Request.AddBody(Data, ContentType);
if TokenMode = tmAuthParam then
Request.Resource := TFirebaseHelpers.EncodeResourceParams(ResourceParams) +
TFirebaseHelpers.EncodeQueryParamsWithToken(QueryParams, EncodeToken)
else
Request.Resource := TFirebaseHelpers.EncodeResourceParams(ResourceParams) +
TFirebasehelpers.EncodeQueryParams(QueryParams);
Request.URLAlreadyEncoded := true;
{$IFDEF DEBUG}
TFirebasehelpers.Log('FirebaseRequest.InternalSendRequest ' +
GetEnumName(TypeInfo(TRESTRequestMethod), ord(Request.Method)) + ': ' +
Request.GetFullRequestURL(true));
{$ENDIF}
RequestID := fRequestID;
Request.ExecuteAsync(
procedure // Completion Handler
begin
try
try
if assigned(OnResponse) and not TFirebaseHelpers.AppIsTerminated then
OnResponse(RequestID, TFirebaseResponse.Create(Response,
OnRequestError, OnSuccess));
except
on e: exception do
if assigned(OnRequestError) then
OnRequestError(RequestID, e.Message)
else
TFirebaseHelpers.LogFmt(
'Exception in FirebaseRequest.InternalSendRequest %s: %s',
[RequestID, e.Message]);
end;
finally
FreeAndNil(Response);
FreeAndNil(Request);
FreeAndNil(Client);
end;
end,
true, true,
procedure(Obj: TObject) // Error Handler
begin
try
if Assigned(Obj) and (Obj is Exception) and assigned(OnRequestError) then
OnRequestError(RequestID, Exception(Obj).Message);
finally
FreeAndNil(Response);
FreeAndNil(Request);
FreeAndNil(Client);
end;
end
);
end;
function TFirebaseRequest.SendRequestSynchronous(
ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TJSONValue;
QueryParams: TQueryParams;
TokenMode: TTokenMode): IFirebaseResponse;
var
SourceStr: TStringStream;
begin
SourceStr := nil;
if Data <> nil then
SourceStr := TStringStream.Create(Data.ToJSON);
try
result := SendRequestSynchronous(ResourceParams, Method, SourceStr,
TRESTContentType.ctAPPLICATION_JSON, QueryParams, TokenMode);
finally
if Assigned(SourceStr) then
SourceStr.Free;
end;
end;
function TFirebaseRequest.SendRequestSynchronous(
ResourceParams: TRequestResourceParam; Method: TRESTRequestMethod;
Data: TStream; ContentType: TRESTContentType;
QueryParams: TQueryParams;
TokenMode: TTokenMode): IFirebaseResponse;
var
StartPos: Int64;
begin
if assigned(Data) then
StartPos := Data.Position
else
StartPos := -1;
result := InternalSendRequestSynchronous(ResourceParams, Method, Data,
ContentType, QueryParams, TokenMode);
if (TokenMode > tmNoToken) and result.StatusIsUnauthorized then
if assigned(fAuth) and fAuth.CheckAndRefreshTokenSynchronous then
begin
if assigned(Data) and (StartPos >= 0) then
Data.Position := StartPos;
// Repeat once with fresh token
result := InternalSendRequestSynchronous(ResourceParams, Method, Data,
ContentType, QueryParams, TokenMode);
end;
end;
procedure TFirebaseRequest.RESTRequestAfterExecute(Sender: TCustomRESTRequest);
begin
{$IFNDEF VER320} // In DE 10.1 and former this leads to an AV
{$IFDEF DEBUG}
TFirebaseHelpers.Log('FirebaseRequest.RESTRequestAfterExecute ' +
GetEnumName(TypeInfo(TRESTRequestMethod), ord(Sender.Method)) + ': ' +
Sender.GetFullRequestURL(true));
{$ENDIF}
{$ENDIF}
end;
procedure TFirebaseRequest.RESTRequestHTTPProtocolError(
Sender: TCustomRESTRequest);
begin
TFirebaseHelpers.Log('FirebaseRequest.RESTRequestHTTPProtocolError ' +
Sender.Response.StatusText);
{$IFDEF DEBUG}
TFirebaseHelpers.Log(' Details: ' +
Sender.Response.Content.Replace(#13, '').Replace(#10, '').Replace(' ', ''));
TFirebaseHelpers.Log(' Request: ' +
GetEnumName(TypeInfo(TRESTRequestMethod), ord(Sender.Method)) + ': ' +
Sender.GetFullRequestURL);
{$ENDIF}
end;
procedure TFirebaseRequest.SendRequest(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TJSONValue; QueryParams: TQueryParams;
TokenMode: TTokenMode; OnResponse: TOnFirebaseResp;
OnRequestError: TOnRequestError; OnSuccess: TOnSuccess);
begin
if (TokenMode > tmNoToken) and assigned(fAuth) and fAuth.NeedTokenRefresh then
begin
// Captured entire parameters for later send request
fResendRequest := TResendRequest.Create(self, ResourceParams,
Method, Data, QueryParams, TokenMode, OnResponse, OnRequestError,
OnSuccess);
fAuth.RefreshToken(SendRequestAfterTokenRefresh, OnRequestError);
end else
InternalSendRequest(ResourceParams, Method, Data, QueryParams, TokenMode,
OnResponse, OnRequestError, nil, OnSuccess);
end;
procedure TFirebaseRequest.SendRequest(ResourceParams: TRequestResourceParam; Method: TRESTRequestMethod;
Data: TJSONValue; QueryParams: TQueryParams; TokenMode: TTokenMode; OnResponse: TOnFirebaseResp;
OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess; OnSuccess: TOnSuccess);
begin
if (TokenMode > tmNoToken) and assigned(fAuth) and fAuth.NeedTokenRefresh then
begin
// Captured entire parameters for later send request
fResendRequest := TResendRequest.Create(self, ResourceParams, Method, Data, QueryParams, TokenMode,
OnResponse, OnRequestErrorWithOnSuccess, OnSuccess);
fAuth.RefreshToken(SendRequestAfterTokenRefresh, OnRequestErrorWithOnSuccess);
end else
InternalSendRequest(ResourceParams, Method, Data, QueryParams, TokenMode,
OnResponse, nil, OnRequestErrorWithOnSuccess, OnSuccess);
end;
procedure TFirebaseRequest.SendRequest(ResourceParams: TRequestResourceParam;
Method: TRESTRequestMethod; Data: TStream; ContentType: TRESTContentType;
QueryParams: TQueryParams; TokenMode: TTokenMode;
OnResponse: TOnFirebaseResp; OnRequestError: TOnRequestError;
OnSuccess: TOnSuccess);
begin
if (TokenMode > tmNoToken) and assigned(fAuth) and fAuth.NeedTokenRefresh then
begin
// Captured entire parameters for later send request
fResendRequest := TResendRequest.Create(self, ResourceParams,
Method, Data, ContentType, QueryParams, TokenMode, OnResponse,
OnRequestError, OnSuccess);
fAuth.RefreshToken(SendStreamRequestAfterTokenRefresh, OnRequestError);
end else
InternalSendRequest(ResourceParams, Method, Data, ContentType, QueryParams,
TokenMode, OnResponse, OnRequestError, nil, OnSuccess);
end;
procedure TFirebaseRequest.SendRequest(ResourceParams: TRequestResourceParam; Method: TRESTRequestMethod;
Data: TStream; ContentType: TRESTContentType; QueryParams: TQueryParams; TokenMode: TTokenMode;
OnResponse: TOnFirebaseResp; OnRequestErrorWithOnSuccess: TOnRequestErrorWithOnSuccess; OnSuccess: TOnSuccess);
begin
if (TokenMode > tmNoToken) and assigned(fAuth) and fAuth.NeedTokenRefresh then
begin
// Captured entire parameters for later send request
fResendRequest := TResendRequest.Create(self, ResourceParams,
Method, Data, ContentType, QueryParams, TokenMode, OnResponse,
OnRequestErrorWithOnSuccess, OnSuccess);
fAuth.RefreshToken(SendStreamRequestAfterTokenRefresh, OnRequestErrorWithOnSuccess);
end else
InternalSendRequest(ResourceParams, Method, Data, ContentType, QueryParams,
TokenMode, OnResponse, nil, OnRequestErrorWithOnSuccess, OnSuccess);
end;
procedure TFirebaseRequest.SendRequestAfterTokenRefresh(
TokenRefreshed: boolean);
var
Request: TFirebaseRequest;
begin
if TokenRefreshed and assigned(fResendRequest) then
begin
with fResendRequest do
begin
Request := TFirebaseRequest.Create(BaseURI, RequestID, Auth);
try
Request.InternalSendRequest(ResParams, Method, Data, QueryParams,
TokenMode, OnResponse, OnRequestError, OnRequestErrorWithOnSuccess,
OnSuccess);
finally
Request.Free;
end;
end;
FreeAndNil(fResendRequest);
end
else if assigned(fResendRequest.OnRequestError) then
fResendRequest.OnRequestError(fRequestID, rsTokenRefreshFailed);
end;
procedure TFirebaseRequest.SendStreamRequestAfterTokenRefresh(
TokenRefreshed: boolean);
var
Request: TFirebaseRequest;
begin
if TokenRefreshed and assigned(fResendRequest) then
begin
with fResendRequest do
begin
Request := TFirebaseRequest.Create(BaseURI, RequestID, Auth);
try
Request.InternalSendRequest(ResParams, Method, DataStream, ContentType,
QueryParams, TokenMode, OnResponse, OnRequestError, OnRequestErrorWithOnSuccess,
OnSuccess);
finally
Request.Free;
end;
end;
FreeAndNil(fResendRequest);
end
else if assigned(fResendRequest.OnRequestError) then
fResendRequest.OnRequestError(fRequestID, rsTokenRefreshFailed);
end;
function TFirebaseRequest.EncodeToken: string;
begin
if assigned(fAuth) then
result := TNetEncoding.URL.Encode(fAuth.Token)
else
result := '';
end;
end.