This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPanamahSDK.Client.pas
842 lines (756 loc) · 27.3 KB
/
PanamahSDK.Client.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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
unit PanamahSDK.Client;
interface
uses
SysUtils, StrUtils, Classes,
{$IFDEF UNICODE}
IdHTTP, IdSSLOpenSSL, IdSSLOpenSSLHeaders, IdCTypes, IdStack,
{$ELSE}
ssl_openssl, ssl_openssl_lib, httpsend, blcksock, synautil,
{$ENDIF}
PanamahSDK.Types, PanamahSDK.Consts, uLkJSON, PanamahSDK.JsonUtils, EncdDecd, DateUtils;
type
IPanamahResponse = interface
['{AF409221-7227-42FA-891B-1380515CE35F}']
procedure SetHeaders(AHeaders: TStrings);
function GetHeaders: TStrings;
procedure SetContent(const AContent: string);
function GetContent: string;
procedure SetStatus(AStatus: Integer);
function GetStatus: Integer;
property Headers: TStrings read GetHeaders write SetHeaders;
property Content: string read GetContent write SetContent;
property Status: Integer read GetStatus write SetStatus;
end;
IPanamahRequest = interface
['{FB10AE9A-3BC1-42AD-8670-2D0E3126C6A3}']
function GetMethod: TMethod;
procedure SetMethod(AMethod: TMethod);
function GetParams: TStrings;
procedure SetParams(AParams: TStrings);
function GetHeaders: TStrings;
procedure SetHeaders(AHeaders: TStrings);
function GetContent: string;
procedure SetContent(const AContent: string);
function GetURL: string;
procedure SetURL(const AURL: string);
property URL: string read GetURL write SetURL;
property Method: TMethod read GetMethod write SetMethod;
property Params: TStrings read GetParams write SetParams;
property Headers: TStrings read GetHeaders write SetHeaders;
property Content: string read GetContent write SetContent;
end;
TPanamahTokenStorage = record
AccessToken: string;
RefreshToken: string;
end;
TPanamahAuthenticatedRequest = function(const AURL: string; AParams, AHeaders: TStrings; AContent: string; ATokens: TPanamahTokenStorage): IPanamahResponse of object;
IPanamahClient = interface
['{0E22616E-90FA-4D19-9727-C54CA2BBB957}']
function MakeRequest(const ARequest: IPanamahRequest): IPanamahResponse;
function Get(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse;
function Put(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse;
function Post(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse;
function Delete(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse;
end;
TPanamahRequest = class(TInterfacedObject, IPanamahRequest)
private
FOwnedObjects: TStrings;
FMethod: TMethod;
FContent: string;
FHeaders: TStrings;
FParams: TStrings;
FURL: string;
FTokens: TPanamahTokenStorage;
procedure SetAuthorizationHeaders;
public
destructor Destroy; override;
function GetContent: string;
function GetHeaders: TStrings;
function GetMethod: TMethod;
function GetParams: TStrings;
function GetURL: string;
procedure SetContent(const AContent: string);
procedure SetHeaders(AHeaders: TStrings);
procedure SetMethod(AMethod: TMethod);
procedure SetParams(AParams: TStrings);
procedure SetURL(const AURL: string);
procedure SetTokens(var ATokens: TPanamahTokenStorage);
property URL: string read GetURL write SetURL;
property Method: TMethod read GetMethod write SetMethod;
property Params: TStrings read GetParams write SetParams;
property Headers: TStrings read GetHeaders write SetHeaders;
property Content: string read GetContent write SetContent;
constructor Create(AURL: string; AMethod: TMethod;
AParams, AHeaders: TStrings; AContent: string; ATokens: TPanamahTokenStorage); overload;
constructor Create(AURL: string; AMethod: TMethod;
AParams, AHeaders: TStrings; AContent: string); overload;
end;
TPanamahResponse = class(TInterfacedObject, IPanamahResponse)
private
FStatus: Integer;
FContent: string;
FHeaders: TStrings;
public
constructor Create(AStatus: Integer; const AHeaders: TStrings; AContent: string);
destructor Destroy; override;
function GetContent: string;
function GetHeaders: TStrings;
function GetStatus: Integer;
procedure SetContent(const Value: string);
procedure SetHeaders(Value: TStrings);
procedure SetStatus(Value: Integer);
property Headers: TStrings read GetHeaders write SetHeaders;
property Content: string read GetContent write SetContent;
property Status: Integer read GetStatus write SetStatus;
end;
{$IFDEF UNICODE}
TPanamahSDKIdHTTP = class(TIdCustomHTTP)
public
constructor Create(AOwner: TComponent);
procedure OnStatusInfoEx(ASender: TObject; const ASslSocket: PSSL; const AWhere, Aret: TIdC_INT; const AType, AMsg: String);
function Delete(const AURL: string; AResponseContent: TStream): string; overload;
function Delete(const AURL: string): string; overload;
end;
{$ENDIF}
TPanamahClient = class(TInterfacedObject, IPanamahClient)
protected
FBaseUrl: string;
public
function Get(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse; virtual;
function Put(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse; virtual;
function Post(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse; virtual;
function Delete(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse; virtual;
function MakeRequest(const ARequest: IPanamahRequest): IPanamahResponse; virtual;
constructor Create(const ABaseURL: string); virtual;
end;
TPanamahAdminClient = class(TPanamahClient)
private
FAuthorizationToken: string;
public
constructor Create(const ABaseURL: string; const AAuthorizationToken: string); reintroduce;
function Get(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse; override;
function Put(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse; override;
function Post(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse; override;
function Delete(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse; override;
end;
TPanamahStreamClient = class(TPanamahClient)
private
FTokens: TPanamahTokenStorage;
FAssinanteId: string;
FSecret: string;
FAuthorizationToken: string;
function CalculateKey(const AAssinanteId, ASecret: string; ATimestamp: TDateTime): string;
public
procedure SetCredentials(const AAuthorizationToken, ASecret, AAssinanteId: string);
function Authenticate(const AURL: string; AParams, AHeaders: TStrings; AContent: string; ARequestFunction: TPanamahAuthenticatedRequest): IPanamahResponse;
function AuthenticatedGet(const AURL: string; AParams, AHeaders: TStrings; AContent: string; ATokens: TPanamahTokenStorage): IPanamahResponse;
function AuthenticatedPut(const AURL: string; AParams, AHeaders: TStrings; AContent: string; ATokens: TPanamahTokenStorage): IPanamahResponse;
function AuthenticatedPost(const AURL: string; AParams, AHeaders: TStrings; AContent: string; ATokens: TPanamahTokenStorage): IPanamahResponse;
function AuthenticatedDelete(const AURL: string; AParams, AHeaders: TStrings; AContent: string; ATokens: TPanamahTokenStorage): IPanamahResponse;
function Get(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse; override;
function Put(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse; override;
function Post(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse; override;
function Delete(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse; override;
constructor Create(const ABaseURL, AAuthorizationToken, ASecret, AAssinanteId: string); reintroduce;
destructor Destroy; override;
end;
procedure PanamahTokenStorageDeserializeResponse(var ATokens: TPanamahTokenStorage; AResponse: IPanamahResponse);
function PanamahTokenStorageClone(var ATokens: TPanamahTokenStorage): TPanamahTokenStorage;
function PanamahTokenStorageIsEmpty(var ATokens: TPanamahTokenStorage): Boolean;
const
DELETE_HTTP_METHOD = 'DELETE';
implementation
{ TPanamahClient }
uses PanamahSDK.Crypto;
{$IFDEF UNICODE}
function TPanamahClient.MakeRequest(const ARequest: IPanamahRequest): IPanamahResponse;
var
ResponseContent: string;
HTTP: TPanamahSDKIdHTTP;
HTTPRequest: TIdHTTPRequest;
HTTPIOHandler: TIdSSLIOHandlerSocketOpenSSL;
RequestContent: TStream;
begin
HTTP := TPanamahSDKIdHTTP.Create(nil);
try
HTTPIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
HTTPIOHandler.OnStatusInfoEx := HTTP.OnStatusInfoEx;
HTTPIOHandler.SSLOptions.Method := sslvSSLv23;
HTTPIOHandler.SSLOptions.SSLVersions := [sslvTLSv1_2, sslvTLSv1_1, sslvTLSv1];
HTTPIOHandler.SSLOptions.Mode := sslmClient;
HTTPIOHandler.LargeStream := True;
HTTPRequest := TIdHTTPRequest.Create(nil);
try
if StartsText(HTTPS_PROTOCOL, ARequest.URL) then
HTTP.IOHandler := HTTPIOHandler;
HTTP.Request := HTTPRequest;
HTTP.ReadTimeout := 60000;
HTTP.ConnectTimeout := 60000;
if Assigned(ARequest.Headers) then
begin
HTTP.Request.CustomHeaders.AddStrings(ARequest.Headers);
HTTP.Request.RawHeaders.AddStrings(ARequest.Headers);
if ARequest.Headers.IndexOfName('Content-Type') > -1 then
HTTP.Request.ContentType := ARequest.Headers.Values['Content-Type'];
if ARequest.Headers.IndexOfName('Accept') > -1 then
HTTP.Request.Accept := ARequest.Headers.Values['Accept'];
end;
if not MatchText(HTTP.Request.ContentType, ['application/xml', 'application/json']) then
HTTP.Request.ContentType := 'application/json';
if not MatchText(HTTP.Request.Accept, ['application/xml', 'application/json']) then
HTTP.Request.Accept := 'application/json';
HTTP.HandleRedirects := True;
HTTP.Request.CustomHeaders.Values['x-sdk-identity'] := SDK_IDENTITY;
try
case ARequest.Method of
mtGET:
begin
ResponseContent := HTTP.Get(ARequest.URL);
end;
mtPOST:
begin
RequestContent := TStringStream.Create(ARequest.Content, TEncoding.UTF8);
try
ResponseContent := HTTP.Post(ARequest.URL, RequestContent);
finally
FreeAndNil(RequestContent);
end;
end;
mtPUT:
begin
RequestContent := TStringStream.Create(ARequest.Content, TEncoding.UTF8);
try
ResponseContent := HTTP.Put(ARequest.URL, RequestContent);
finally
FreeAndNil(RequestContent);
end;
end;
mtDELETE:
begin
ResponseContent := HTTP.Delete(ARequest.URL);
end;
end;
except
on E: EIdHTTPProtocolException do
begin
if HTTP.ResponseCode = 500 then
raise EPanamahSDKServerInternalException.Create(E.Message)
else
if HTTP.ResponseCode = 400 then
raise EPanamahSDKBadRequestException.Create(Format('%s: %s', [E.ErrorMessage, E.Message]))
else
if (HTTP.ResponseCode = 403) or (HTTP.ResponseCode = 404) or (HTTP.ResponseCode = 409) then
else
if HTTP.ResponseCode = 422 then
begin
ResponseContent := E.ErrorMessage;
end
else
raise EPanamahSDKHTTPProtocolException.Create(E.Message);
end;
on E: EIdSocketError do
begin
raise EPanamahSDKConnectionRefusedException.Create(E.Message);
end;
end;
finally
FreeAndNil(HTTPRequest);
end;
finally
HTTPIOHandler.Free;
end;
Result := TPanamahResponse.Create(HTTP.ResponseCode, HTTP.Response.RawHeaders, ResponseContent);
finally
FreeAndNil(HTTP);
end;
end;
{$ELSE}
function TPanamahClient.MakeRequest(const ARequest: IPanamahRequest): IPanamahResponse;
function GetDocumentContent(HTTPClient: THttpSend): string;
var
ResponseDocument: TStrings;
begin
Result := EmptyStr;
if Assigned(HTTPClient) and Assigned(HTTPClient.Document) and (HTTPClient.Document.Size > 0) then
begin
HTTPClient.Document.Position := 0;
ResponseDocument := TStringList.Create;
try
ResponseDocument.LoadFromStream(HTTPClient.Document);
Result := ResponseDocument.Text;
finally
ResponseDocument.Free;
end;
end;
end;
function GetMethodAsString(AMethod: TMethod): string;
begin
Result := EmptyStr;
case AMethod of
mtGET: Result := 'GET';
mtPUT: Result := 'PUT';
mtDELETE: Result := 'DELETE';
mtPOST: Result := 'POST';
end;
end;
function CreateRequestHeaders(ARequest: IPanamahRequest): TStrings;
var
I: Integer;
begin
Result := TStringList.Create;
Result.NameValueSeparator := ':';
Result.Values['Content-Type'] := 'application/json';
Result.Values['Accept'] := 'application/json';
Result.Values['x-sdk-identity'] := SDK_IDENTITY;
if Assigned(ARequest.Headers) then
begin
for I := 0 to ARequest.Headers.Count - 1 do
Result.Values[ARequest.Headers.Names[I]] := ARequest.Headers.Values[ARequest.Headers.Names[I]];
end;
end;
var
HTTPClient: THttpSend;
RequestHeaders: TStrings;
Response: IPanamahResponse;
begin
Result := nil;
HTTPClient := THttpSend.Create;
HTTPClient.Protocol := '1.1';
try
HTTPClient.Timeout := 60000;
RequestHeaders := CreateRequestHeaders(ARequest);
try
HTTPClient.Headers.AddStrings(RequestHeaders);
finally
RequestHeaders.Free;
end;
if ARequest.Content <> EmptyStr then
HTTPClient.Document.Write(Pointer(ARequest.Content)^, Length(ARequest.Content));
HTTPClient.MimeType := CoalesceText(HTTPClient.Headers.Values['Content-Type'], 'application/json');
HTTPClient.HTTPMethod(GetMethodAsString(ARequest.Method), ARequest.URL);
Response := TPanamahResponse.Create(HTTPClient.ResultCode, HTTPClient.Headers, GetDocumentContent(HTTPClient));
if HTTPClient.Sock.LastError > 0 then
begin
case HTTPClient.Sock.LastError of
10061: raise EPanamahSDKConnectionRefusedException.Create(Response.Content);
end;
end;
case Response.Status of
500: raise EPanamahSDKServerInternalException.Create(Response.Content);
400: raise EPanamahSDKBadRequestException.Create(Response.Content);
else
begin
Result := Response;
end;
end;
finally
HTTPClient.Free;
end;
end;
{$ENDIF}
constructor TPanamahClient.Create(const ABaseURL: string);
begin
inherited Create;
FBaseUrl := ABaseUrl;
end;
function TPanamahClient.Delete(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse;
var
Request: IPanamahRequest;
begin
Request := TPanamahRequest.Create(Concat(FBaseURL, AURL), mtDELETE, AParams, AHeaders, EmptyStr);
Result := MakeRequest(Request);
end;
function TPanamahClient.Get(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse;
var
Request: IPanamahRequest;
begin
Request := TPanamahRequest.Create(Concat(FBaseURL, AURL), mtGET, AParams, AHeaders, EmptyStr);
Result := MakeRequest(Request);
end;
function TPanamahClient.Post(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse;
var
Request: IPanamahRequest;
begin
Request := TPanamahRequest.Create(Concat(FBaseURL, AURL), mtPOST, nil, AHeaders, AContent);
Result := MakeRequest(Request);
end;
function TPanamahClient.Put(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse;
var
Request: IPanamahRequest;
begin
Request := TPanamahRequest.Create(Concat(FBaseURL, AURL), mtPUT, nil, AHeaders, AContent);
Result := MakeRequest(Request);
end;
{ TPanamahStreamClient }
function TPanamahStreamClient.CalculateKey(const AAssinanteId, ASecret: string; ATimestamp: TDateTime): string;
begin
Result := SHA1Base64(ASecret + AAssinanteId + IntToStr(DateTimeToUnix(ATimestamp)));
end;
constructor TPanamahStreamClient.Create(const ABaseURL, AAuthorizationToken, ASecret, AAssinanteId: string);
begin
inherited Create(ABaseURL);
SetCredentials(AAuthorizationToken, ASecret, AAssinanteId);
end;
function TPanamahStreamClient.Authenticate(const AURL: string; AParams, AHeaders: TStrings; AContent: string; ARequestFunction: TPanamahAuthenticatedRequest): IPanamahResponse;
var
AuthRequest: IPanamahRequest;
AuthResponse: IPanamahResponse;
AuthHeaders: TStrings;
RefreshRequest: IPanamahRequest;
RefreshResponse: IPanamahResponse;
RefreshHeaders: TStrings;
Timestamp: TDateTime;
begin
if not PanamahTokenStorageIsEmpty(FTokens) then
begin
Result := ARequestFunction(AURL, AParams, AHeaders, AContent, FTokens);
if (Result.Status = 403) then
begin
RefreshHeaders := TStringList.Create;
try
RefreshHeaders.Values['Accept'] := 'application/json';
RefreshHeaders.Values['Authorization'] := FTokens.RefreshToken;
RefreshRequest := TPanamahRequest.Create(Concat(FBaseURL, '/stream/auth/refresh'), mtGET, nil, RefreshHeaders, Format('{"accessToken":"%s"}', [FTokens.AccessToken]));
RefreshResponse := MakeRequest(RefreshRequest);
if RefreshResponse.Status = 200 then
begin
PanamahTokenStorageDeserializeResponse(FTokens, RefreshResponse);
Result := ARequestFunction(AURL, AParams, AHeaders, AContent, FTokens);
end
else
raise Exception.CreateFmt('Refresh falhou com código %d', [RefreshResponse.Status]);
finally
RefreshHeaders.Free;
end;
end;
end
else
begin
AuthHeaders := TStringList.Create;
try
AuthHeaders.Values['Content-Type'] := 'application/json';
AuthHeaders.Values['Accept'] := 'application/json';
AuthHeaders.Values['Authorization'] := FAuthorizationToken;
Timestamp := NowUTC;
AuthRequest := TPanamahRequest.Create(Concat(FBaseURL, '/stream/auth'), mtPOST, nil, AuthHeaders, Format('{"assinanteId":"%s","key":"%s","ts":"%d"}', [
FAssinanteId,
CalculateKey(FAssinanteId, FSecret, Timestamp),
DateTimeToUnix(Timestamp)
]));
AuthResponse := MakeRequest(AuthRequest);
if AuthResponse.Status = 200 then
begin
PanamahTokenStorageDeserializeResponse(FTokens, AuthResponse);
Result := ARequestFunction(AURL, AParams, AHeaders, AContent, FTokens);
end
else
raise Exception.CreateFmt('Autenticação falhou com código %d', [AuthResponse.Status]);
finally
AuthHeaders.Free;
end;
end;
end;
function TPanamahStreamClient.Delete(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse;
begin
Result := Authenticate(AURL, AParams, AHeaders, EmptyStr, AuthenticatedDelete);
end;
destructor TPanamahStreamClient.Destroy;
begin
inherited;
end;
function TPanamahStreamClient.AuthenticatedDelete(const AURL: string; AParams, AHeaders: TStrings; AContent: string; ATokens: TPanamahTokenStorage): IPanamahResponse;
var
Request: IPanamahRequest;
begin
Request := TPanamahRequest.Create(Concat(FBaseURL, AURL), mtDELETE, AParams, AHeaders, EmptyStr, ATokens);
Result := MakeRequest(Request);
end;
function TPanamahStreamClient.AuthenticatedGet(const AURL: string; AParams, AHeaders: TStrings; AContent: string; ATokens: TPanamahTokenStorage): IPanamahResponse;
var
Request: IPanamahRequest;
begin
Request := TPanamahRequest.Create(Concat(FBaseURL, AURL), mtGET, AParams, AHeaders, EmptyStr, ATokens);
Result := MakeRequest(Request);
end;
function TPanamahStreamClient.AuthenticatedPost(const AURL: string; AParams, AHeaders: TStrings; AContent: string; ATokens: TPanamahTokenStorage): IPanamahResponse;
var
Request: IPanamahRequest;
begin
Request := TPanamahRequest.Create(Concat(FBaseURL, AURL), mtPOST, nil, nil, AContent, ATokens);
Result := MakeRequest(Request);
end;
function TPanamahStreamClient.AuthenticatedPut(const AURL: string; AParams, AHeaders: TStrings; AContent: string; ATokens: TPanamahTokenStorage): IPanamahResponse;
var
Request: IPanamahRequest;
begin
Request := TPanamahRequest.Create(Concat(FBaseURL, AURL), mtPUT, nil, nil, AContent, ATokens);
Result := MakeRequest(Request);
end;
function TPanamahStreamClient.Get(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse;
begin
Result := Authenticate(AURL, AParams, AHeaders, EmptyStr, AuthenticatedGet);
end;
function TPanamahStreamClient.Post(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse;
begin
Result := Authenticate(AURL, nil, nil, AContent, AuthenticatedPost);
end;
function TPanamahStreamClient.Put(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse;
begin
Result := Authenticate(AURL, nil, nil, AContent, AuthenticatedPut);
end;
procedure TPanamahStreamClient.SetCredentials(const AAuthorizationToken, ASecret, AAssinanteId: string);
begin
FAuthorizationToken := AAuthorizationToken;
FAssinanteId := AAssinanteId;
FSecret := ASecret;
end;
{$IFDEF UNICODE}
{ TPanamahSDKIdHTTP }
constructor TPanamahSDKIdHTTP.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
procedure TPanamahSDKIdHTTP.OnStatusInfoEx(ASender: TObject; const ASslSocket: PSSL; const AWhere, Aret: TIdC_INT;
const AType, AMsg: String);
begin
SSL_set_tlsext_host_name(AsslSocket, Request.Host);
end;
function TPanamahSDKIdHTTP.Delete(const AURL: string): string;
var
Stream: TMemoryStream;
begin
Result := EmptyStr;
Stream := TMemoryStream.Create;
try
DoRequest(DELETE_HTTP_METHOD, AURL, nil, Stream, []);
Stream.Position := 0;
if Stream.Size > 0 then
begin
SetString(Result, PAnsiChar(Stream.Memory^), Stream.Size);
end;
finally
FreeAndNil(Stream);
end;
end;
function TPanamahSDKIdHTTP.Delete(const AURL: string; AResponseContent: TStream): string;
begin
DoRequest(DELETE_HTTP_METHOD, AURL, nil, AResponseContent, []);
end;
{$ENDIF}
{ TRequest }
constructor TPanamahRequest.Create(AURL: string; AMethod: TMethod; AParams, AHeaders: TStrings; AContent: string; ATokens: TPanamahTokenStorage);
begin
FOwnedObjects := TStringList.Create;
SetTokens(ATokens);
SetURL(AURL);
SetMethod(AMethod);
SetParams(AParams);
SetHeaders(AHeaders);
SetContent(AContent);
end;
constructor TPanamahRequest.Create(AURL: string; AMethod: TMethod; AParams, AHeaders: TStrings; AContent: string);
var
Tokens: TPanamahTokenStorage;
begin
Create(AURL, AMethod, AParams, AHeaders, AContent, Tokens);
end;
destructor TPanamahRequest.Destroy;
var
I: Integer;
begin
for I := 0 to FOwnedObjects.Count - 1 do
FOwnedObjects.Objects[I].Free;
FOwnedObjects.Free;
inherited;
end;
function TPanamahRequest.GetContent: string;
begin
Result := FContent;
end;
function TPanamahRequest.GetHeaders: TStrings;
begin
Result := FHeaders;
end;
function TPanamahRequest.GetMethod: TMethod;
begin
Result := FMethod;
end;
function TPanamahRequest.GetParams: TStrings;
begin
Result := FParams;
end;
function TPanamahRequest.GetURL: string;
begin
Result := FURL;
end;
procedure TPanamahRequest.SetAuthorizationHeaders;
begin
if not PanamahTokenStorageIsEmpty(FTokens) then
begin
if not Assigned(FHeaders) then
begin
FHeaders := TStringList.Create;
FHeaders.NameValueSeparator := ':';
FOwnedObjects.AddObject('Headers', FHeaders);
end;
begin
if FHeaders.IndexOf('authorization') = -1 then
FHeaders.Values['authorization'] := FTokens.AccessToken;
end;
end;
end;
procedure TPanamahRequest.SetContent(const AContent: string);
begin
inherited;
FContent := AContent;
end;
procedure TPanamahRequest.SetHeaders(AHeaders: TStrings);
begin
inherited;
FHeaders := AHeaders;
SetAuthorizationHeaders;
end;
procedure TPanamahRequest.SetMethod(AMethod: TMethod);
begin
inherited;
FMethod := AMethod;
end;
procedure TPanamahRequest.SetParams(AParams: TStrings);
begin
inherited;
FParams := AParams;
end;
procedure TPanamahRequest.SetTokens(var ATokens: TPanamahTokenStorage);
begin
if not PanamahTokenStorageIsEmpty(ATokens) then
FTokens := PanamahTokenStorageClone(ATokens);
end;
procedure TPanamahRequest.SetURL(const AURL: string);
begin
FURL := AURL;
end;
{ TResponse }
constructor TPanamahResponse.Create(AStatus: Integer; const AHeaders: TStrings; AContent: string);
begin
FHeaders := TStringList.Create;
if Assigned(AHeaders) then
FHeaders.AddStrings(AHeaders);
FHeaders.NameValueSeparator := ':';
SetStatus(AStatus);
SetContent(AContent);
end;
destructor TPanamahResponse.Destroy;
begin
FHeaders.Free;
inherited;
end;
function TPanamahResponse.GetContent: string;
begin
Result := FContent;
end;
function TPanamahResponse.GetHeaders: TStrings;
begin
Result := FHeaders;
end;
function TPanamahResponse.GetStatus: Integer;
begin
Result := FStatus;
end;
procedure TPanamahResponse.SetContent(const Value: string);
begin
FContent := Value;
end;
procedure TPanamahResponse.SetHeaders(Value: TStrings);
begin
FHeaders := Value;
end;
procedure TPanamahResponse.SetStatus(Value: Integer);
begin
FStatus := Value;
end;
{ TPanamahAdminClient }
constructor TPanamahAdminClient.Create(const ABaseURL, AAuthorizationToken: string);
begin
inherited Create(ABaseURL);
FAuthorizationToken := AAuthorizationToken;
end;
function TPanamahAdminClient.Delete(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse;
var
Headers: TStrings;
begin
Headers := TStringList.Create;
try
if Assigned(AHeaders) then
Headers.AddStrings(AHeaders);
Headers.Values['Authorization'] := FAuthorizationToken;
Result := inherited Delete(AURL, AParams, Headers);
finally
Headers.Free;
end;
end;
function TPanamahAdminClient.Get(const AURL: string; AParams, AHeaders: TStrings): IPanamahResponse;
var
Headers: TStrings;
begin
Headers := TStringList.Create;
try
if Assigned(AHeaders) then
Headers.AddStrings(AHeaders);
Headers.Values['Authorization'] := FAuthorizationToken;
Result := inherited Get(AURL, AParams, Headers);
finally
Headers.Free;
end;
end;
function TPanamahAdminClient.Post(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse;
var
Headers: TStrings;
begin
Headers := TStringList.Create;
try
if Assigned(AHeaders) then
Headers.AddStrings(AHeaders);
Headers.Values['Authorization'] := FAuthorizationToken;
Result := inherited Post(AURL, AContent, Headers);
finally
Headers.Free;
end;
end;
function TPanamahAdminClient.Put(const AURL, AContent: string; AHeaders: TStrings): IPanamahResponse;
var
Headers: TStrings;
begin
Headers := TStringList.Create;
try
if Assigned(AHeaders) then
Headers.AddStrings(AHeaders);
Headers.Values['Authorization'] := FAuthorizationToken;
Result := inherited Put(AURL, AContent, Headers);
finally
Headers.Free;
end;
end;
{ TTokenStorage }
function PanamahTokenStorageClone(var ATokens: TPanamahTokenStorage): TPanamahTokenStorage;
begin
Result.AccessToken := ATokens.AccessToken;
Result.RefreshToken := ATokens.RefreshToken;
end;
procedure PanamahTokenStorageDeserializeResponse(var ATokens: TPanamahTokenStorage; AResponse: IPanamahResponse);
var
JsonObject: TlkJSONobject;
begin
JsonObject := TlkJSON.ParseText(AResponse.Content) as TlkJSONobject;
try
ATokens.AccessToken := JsonObject.Field['accessToken'].Value;
ATokens.RefreshToken := JsonObject.Field['refreshToken'].Value;
finally
JsonObject.Free;
end;
end;
function PanamahTokenStorageIsEmpty(var ATokens: TPanamahTokenStorage): Boolean;
begin
Result := SameText(ATokens.AccessToken, EmptyStr) and SameText(ATokens.RefreshToken, EmptyStr);
end;
end.