-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathCloudAPI.Client.pas
More file actions
161 lines (142 loc) · 4.59 KB
/
CloudAPI.Client.pas
File metadata and controls
161 lines (142 loc) · 4.59 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
unit CloudAPI.Client;
interface
uses
CloudAPI.Client.Base,
CloudAPI.Response,
CloudAPI.Request,
CloudAPI.Types,
System.Classes,
System.SysUtils;
type
TCloudApiClient = class(TCloudApiClientBase)
public
function Download(const AUrl, AFileName: string; ARequest: IcaRequest = nil): IcaResponseBase; overload;
function Download(const AUrl: string; AStream: TStream; ARequest: IcaRequest = nil): IcaResponseBase; overload;
function Execute(ARequest: IcaRequest): IcaResponseBase; overload;
function Execute<T>(ARequest: IcaRequest): IcaResponse<T>; overload;
function TryExecute(ARequest: IcaRequest; var AResp: IcaResponseBase): Boolean; overload;
function TryExecute<T>(ARequest: IcaRequest; var AResp: IcaResponse<T>): Boolean; overload;
function GroupExecute(ARequests: TArray<IcaRequest>): TArray<IcaResponseBase>; overload;
function GroupExecute<T>(ARequests: TArray<IcaRequest>): TArray<IcaResponse<T>>; overload;
// Async
procedure TryExecuteAsync(ARequest: IcaRequest; AResponse: TProc<Boolean, IcaResponseBase>); overload;
procedure TryExecuteAsync<T>(ARequest: IcaRequest; AOnResponse: TProc < IcaResponse < T >> ); overload;
end;
implementation
uses
CloudAPI.Exceptions;
{ TCloudApiClient }
function TCloudApiClient.Download(const AUrl, AFileName: string; ARequest: IcaRequest = nil): IcaResponseBase;
var
lFileStream: TFileStream;
begin
lFileStream := TFileStream.Create(AFileName, fmCreate);
try
Result := Download(AUrl, lFileStream, ARequest);
finally
lFileStream.Free;
end;
end;
function TCloudApiClient.Download(const AUrl: string; AStream: TStream; ARequest: IcaRequest = nil): IcaResponseBase;
var
lOriginalStream: TStream;
lOriginalUrl: string;
begin
lOriginalStream := ResponseStream;
lOriginalUrl := BaseUrl;
ResponseStream := AStream;
try
BaseUrl := AUrl;
TryInternalExcecute(ARequest, Result);
finally
ResponseStream := lOriginalStream;
BaseUrl := lOriginalUrl;
end;
end;
function TCloudApiClient.Execute(ARequest: IcaRequest): IcaResponseBase;
begin
if not TryInternalExcecute(ARequest, Result) then
raise Result.Exception;
end;
function TCloudApiClient.Execute<T>(ARequest: IcaRequest): IcaResponse<T>;
var
LResult: IcaResponseBase;
begin
LResult := Execute(ARequest);
Result := TcaResponse<T>.Create(ARequest, LResult.HttpRequest, LResult.HttpResponse, GetSerializer,
LResult.Exception);
end;
function TCloudApiClient.GroupExecute(ARequests: TArray<IcaRequest>): TArray<IcaResponseBase>;
var
I: Integer;
begin
SetLength(Result, Length(ARequests));
for I := Low(ARequests) to High(ARequests) do
Result[I] := Execute(ARequests[I]);
end;
function TCloudApiClient.GroupExecute<T>(ARequests: TArray<IcaRequest>): TArray<IcaResponse<T>>;
var
I: Integer;
begin
SetLength(Result, Length(ARequests));
for I := Low(ARequests) to High(ARequests) do
Result[I] := Execute<T>(ARequests[I]);
end;
function TCloudApiClient.TryExecute(ARequest: IcaRequest; var AResp: IcaResponseBase): Boolean;
begin
Result := TryInternalExcecute(ARequest, AResp);
end;
function TCloudApiClient.TryExecute<T>(ARequest: IcaRequest; var AResp: IcaResponse<T>): Boolean;
var
LResult: IcaResponseBase;
begin
if TryExecute(ARequest, LResult) then
AResp := TcaResponse<T>.Create(ARequest, LResult.HttpRequest, LResult.HttpResponse, GetSerializer,
LResult.Exception);
Result := AResp <> nil;
end;
procedure TCloudApiClient.TryExecuteAsync(ARequest: IcaRequest; AResponse: TProc<Boolean, IcaResponseBase>);
var
LThread: TThread;
begin
LThread := TThread.CreateAnonymousThread(
procedure
var
LResult: Boolean;
LResponse: IcaResponseBase;
begin
LResult := TryExecute(ARequest, LResponse);
if IsConsole then
begin
if Assigned(AResponse) then
AResponse(LResult, LResponse);
end
else
begin
TThread.Synchronize(nil,
procedure
begin
if Assigned(AResponse) then
AResponse(LResult, LResponse);
end);
end;
end);
LThread.FreeOnTerminate := True;
LThread.Start;
end;
procedure TCloudApiClient.TryExecuteAsync<T>(ARequest: IcaRequest; AOnResponse: TProc < IcaResponse < T >> );
begin
TryExecuteAsync(ARequest,
procedure(AResult: Boolean; AResponse: IcaResponseBase)
var
LResponse: IcaResponse<T>;
begin
if Assigned(AOnResponse) then
begin
LResponse := TcaResponse<T>.Create(ARequest, AResponse.HttpRequest, AResponse.HttpResponse, GetSerializer,
AResponse.Exception);
AOnResponse(LResponse);
end;
end);
end;
end.