-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathCloudAPI.Response.Printer.pas
More file actions
185 lines (163 loc) · 4.23 KB
/
CloudAPI.Response.Printer.pas
File metadata and controls
185 lines (163 loc) · 4.23 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
unit CloudAPI.Response.Printer;
interface
uses
System.Json.Serializers,
System.Net.HttpClient,
System.Net.URLClient,
CloudAPI.Response;
type
TrpRequest = class
private
[JsonName('Url')]
FUrl: string;
[JsonName('Method')]
FMethod: string;
[JsonName('Headers')]
FHeaders: TArray<string>;
[JsonName('Content')]
FContent: string;
protected
public
constructor Create(AHttpRequest: IHTTPRequest);
class function TNetHeadersToStrings(AHeaders: TNetHeaders): TArray<string>;
public
property Url: string read FUrl;
property Method: string read FMethod;
property Headers: TArray<string> read FHeaders;
property Content: string read FContent;
end;
TrpResponse = class
private
[JsonName('StatusCode')]
FStatusCode: Integer;
[JsonName('StatusText')]
FStatusText: string;
[JsonName('Headers')]
FHeaders: TArray<string>;
[JsonName('Content')]
FContent: string;
public
constructor Create(AHttpResponse: IHTTPResponse);
public
property StatusCode: Integer read FStatusCode;
property StatusText: string read FStatusText;
property Content: string read FContent;
end;
TcaResponsePrinter = class
private
[JsonName('Request')]
FRequest: TrpRequest;
[JsonName('Response')]
FResponse: TrpResponse;
protected
public
function AsJson: string;
constructor Create();
procedure ParseResponse(AResponse: TcaResponseBase);
procedure FreeData;
destructor Destroy; override;
property Request: TrpRequest read FRequest;
property Response: TrpResponse read FResponse;
class procedure ToConsole(AResponse: TcaResponseBase);
end;
implementation
uses
System.Json.Types,
System.SysUtils,
System.Classes;
function StreamToString(aStream: TStream): string;
var
SS: TStringStream;
begin
if aStream <> nil then
begin
SS := TStringStream.Create('');
try
SS.CopyFrom(aStream, 0); // No need to position at 0 nor provide size
Result := SS.DataString;
finally
SS.Free;
end;
end
else
begin
Result := '';
end;
end;
{ TcaPrintResponse }
function TcaResponsePrinter.AsJson: string;
var
lSerializer: TJsonSerializer;
begin
lSerializer := TJsonSerializer.Create;
try
lSerializer.Formatting := TJsonFormatting.Indented;
lSerializer.StringEscapeHandling := TJsonStringEscapeHandling.EscapeNonAscii;
Result := lSerializer.Serialize<TcaResponsePrinter>(Self);
finally
lSerializer.Free;
end;
end;
constructor TcaResponsePrinter.Create();
begin
FRequest := nil;
FResponse := nil;
end;
destructor TcaResponsePrinter.Destroy;
begin
FreeData;
inherited;
end;
procedure TcaResponsePrinter.FreeData;
begin
if Assigned(FRequest) then
FreeAndNil(FRequest);
if Assigned(FResponse) then
FreeAndNil(FResponse);
end;
procedure TcaResponsePrinter.ParseResponse(AResponse: TcaResponseBase);
begin
FreeData;
if Assigned(AResponse.HttpRequest) then
FRequest := TrpRequest.Create(AResponse.HttpRequest);
if Assigned(AResponse.HttpResponse) then
FResponse := TrpResponse.Create(AResponse.HttpResponse);
end;
class procedure TcaResponsePrinter.ToConsole(AResponse: TcaResponseBase);
var
lSelf: TcaResponsePrinter;
begin
lSelf := TcaResponsePrinter.Create();
try
lSelf.ParseResponse(AResponse);
Writeln(lSelf.AsJson);
finally
lSelf.Free;
end;
end;
{ TrpRequest }
constructor TrpRequest.Create(AHttpRequest: IHTTPRequest);
begin
FUrl := AHttpRequest.Url.ToString;
FMethod := AHttpRequest.MethodString;
FHeaders := TNetHeadersToStrings(AHttpRequest.Headers);
FContent := StreamToString(AHttpRequest.SourceStream);
end;
class function TrpRequest.TNetHeadersToStrings(AHeaders: TNetHeaders): TArray<string>;
var
i: Integer;
begin
SetLength(Result, length(AHeaders));
for i := Low(AHeaders) to High(AHeaders) do
Result[i] := AHeaders[i].Name + ' = ' + AHeaders[i].Value;
end;
{ TrpResponse }
constructor TrpResponse.Create(AHttpResponse: IHTTPResponse);
begin
FStatusCode := AHttpResponse.StatusCode;
FStatusText := AHttpResponse.StatusText;
FHeaders := TrpRequest.TNetHeadersToStrings(AHttpResponse.Headers);
if AHttpResponse.HeaderValue['Content-Type'] = 'application/json' then
FContent := AHttpResponse.ContentAsString();
end;
end.