-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuGetItRestService.pas
More file actions
295 lines (255 loc) · 9.08 KB
/
Copy pathuGetItRestService.pas
File metadata and controls
295 lines (255 loc) · 9.08 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
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
unit uGetItRestService;
interface
uses
System.Generics.Collections, System.JSON,
uGetItTypes;
type
TGetItRestRequest = record
ServiceUrl: string;
CatalogVersion: string;
ProductId: string;
ProductSku: string;
IDEVersion: string;
SearchText: string;
end;
TGetItRestLogProc = reference to procedure(const Msg: string);
TGetItRestService = record
public
class function CatalogInfoUrl(const ServiceUrl: string): string; static;
class procedure FetchPackages(const Request: TGetItRestRequest;
const Target: TObjectDictionary<string, TGetItPackageInfo>;
const OnLog: TGetItRestLogProc); static;
private
class function BuildRestBody(const Request: TGetItRestRequest; const StartIndex, EndIndex: Integer): string; static;
class function JsonValueToString(const Obj: TJSONObject; const Name: string): string; static;
class procedure ReadCategories(const Obj: TJSONObject; out CategoryIds, CategoryNames: string); static;
class function TryGetArrayFromPayload(const JsonRoot: TJSONValue; out Arr: TJSONArray): Boolean; static;
class function ResponsePreview(const Content: string; const MaxLen: Integer = 240): string; static;
end;
implementation
uses
System.SysUtils, System.Classes, System.Net.HttpClient, System.Net.URLClient, System.NetEncoding,
uGetItCore;
class function TGetItRestService.CatalogInfoUrl(const ServiceUrl: string): string;
var
BaseUrl: string;
begin
BaseUrl := Trim(ServiceUrl);
if BaseUrl.EndsWith('/') then
Result := BaseUrl + 'catalog/info'
else
Result := BaseUrl + '/catalog/info';
end;
class function TGetItRestService.BuildRestBody(const Request: TGetItRestRequest; const StartIndex, EndIndex: Integer): string;
var
VersionText: string;
FilterByLicenseText: string;
PersonalitiesText: string;
begin
VersionText := Request.IDEVersion;
if VersionText = '' then
VersionText := '0';
FilterByLicenseText := '0';
PersonalitiesText := '1';
if SameText(VersionText, '20.0') then
begin
FilterByLicenseText := '1';
PersonalitiesText := '0';
end;
Result := Format(
'Start=%d&End=%d&Categories=-1&CalculateSize=0&FilterByLicense=%s&Order=%d' +
'&Version=%s&Identity=RADSTUDIO&Personalities=%s&Platforms=0&Frameworks=0' +
'&ProductSKU=%s&IsTrial=0&CatalogVersion=%s&ClientVersion=7.0&LanguageCode=EN&ProductId=%s' +
'&ProductIds=%s&ProductSKUs=%s&IsTrials=0&Search=%s',
[StartIndex, EndIndex,
TNetEncoding.URL.Encode(FilterByLicenseText),
0,
TNetEncoding.URL.Encode(VersionText),
TNetEncoding.URL.Encode(PersonalitiesText),
TNetEncoding.URL.Encode(Request.ProductSku),
TNetEncoding.URL.Encode(Request.CatalogVersion),
TNetEncoding.URL.Encode(Request.ProductId),
TNetEncoding.URL.Encode(Request.ProductId),
TNetEncoding.URL.Encode(Request.ProductSku),
TNetEncoding.URL.Encode(Request.SearchText)]);
end;
class function TGetItRestService.JsonValueToString(const Obj: TJSONObject; const Name: string): string;
begin
Result := '';
Obj.TryGetValue<string>(Name, Result);
end;
class procedure TGetItRestService.ReadCategories(const Obj: TJSONObject; out CategoryIds, CategoryNames: string);
var
CategoriesValue: TJSONValue;
CategoriesArray: TJSONArray;
CategoryObj: TJSONObject;
CategoryId: string;
CategoryName: string;
begin
CategoryIds := '';
CategoryNames := '';
CategoriesValue := Obj.GetValue('Categories');
if not (CategoriesValue is TJSONArray) then
Exit;
CategoriesArray := TJSONArray(CategoriesValue);
for var i := 0 to CategoriesArray.Count - 1 do
begin
if not (CategoriesArray.Items[i] is TJSONObject) then
Continue;
CategoryObj := CategoriesArray.Items[i] as TJSONObject;
CategoryId := JsonValueToString(CategoryObj, 'Id');
CategoryName := JsonValueToString(CategoryObj, 'Name');
if CategoryId <> '' then
begin
if CategoryIds <> '' then
CategoryIds := CategoryIds + ', ';
CategoryIds := CategoryIds + CategoryId;
end;
if CategoryName <> '' then
begin
if CategoryNames <> '' then
CategoryNames := CategoryNames + ', ';
CategoryNames := CategoryNames + CategoryName;
end;
end;
end;
class function TGetItRestService.ResponsePreview(const Content: string; const MaxLen: Integer): string;
var
Normalized: string;
begin
Normalized := Content.Replace(#13, ' ').Replace(#10, ' ').Trim;
if Length(Normalized) > MaxLen then
Result := Copy(Normalized, 1, MaxLen) + '...'
else
Result := Normalized;
end;
class function TGetItRestService.TryGetArrayFromPayload(const JsonRoot: TJSONValue; out Arr: TJSONArray): Boolean;
const
TopLevelArrayKeys: array[0..11] of string = (
'Items', 'items', 'Data', 'data', 'Result', 'result',
'Value', 'value', 'CatalogItems', 'catalogItems', 'Packages', 'packages');
NestedObjectKeys: array[0..5] of string = ('Data', 'data', 'Result', 'result', 'Value', 'value');
NestedArrayKeys: array[0..3] of string = ('Items', 'items', 'Packages', 'packages');
var
Obj: TJSONObject;
NestedObj: TJSONObject;
V: TJSONValue;
begin
Arr := nil;
if JsonRoot is TJSONArray then
begin
Arr := TJSONArray(JsonRoot);
Exit(True);
end;
if not (JsonRoot is TJSONObject) then
Exit(False);
Obj := TJSONObject(JsonRoot);
for var Key in TopLevelArrayKeys do
begin
V := Obj.GetValue(Key);
if V is TJSONArray then
begin
Arr := TJSONArray(V);
Exit(True);
end;
end;
for var ContainerKey in NestedObjectKeys do
begin
V := Obj.GetValue(ContainerKey);
if not (V is TJSONObject) then
Continue;
NestedObj := TJSONObject(V);
for var ArrayKey in NestedArrayKeys do
begin
V := NestedObj.GetValue(ArrayKey);
if V is TJSONArray then
begin
Arr := TJSONArray(V);
Exit(True);
end;
end;
end;
Result := False;
end;
class procedure TGetItRestService.FetchPackages(const Request: TGetItRestRequest;
const Target: TObjectDictionary<string, TGetItPackageInfo>; const OnLog: TGetItRestLogProc);
var
HttpClient: THTTPClient;
ReqStream: TStringStream;
ReqHeaders: TNetHeaders;
Resp: IHTTPResponse;
RestUrl: string;
Body: string;
ContentText: string;
JsonRoot: TJSONValue;
Arr: TJSONArray;
PageStart: Integer;
PageSize: Integer;
ItemsInPage: Integer;
begin
RestUrl := CatalogInfoUrl(Request.ServiceUrl);
ReqHeaders := [TNameValuePair.Create('Content-Type', 'application/x-www-form-urlencoded')];
PageStart := 0;
PageSize := 300;
HttpClient := THTTPClient.Create;
try
repeat
Body := BuildRestBody(Request, PageStart, PageStart + PageSize);
OnLog(Format('REST body page: Start=%d End=%d', [PageStart, PageStart + PageSize]));
ReqStream := TStringStream.Create(Body, TEncoding.UTF8);
try
Resp := HttpClient.Post(RestUrl, ReqStream, nil, ReqHeaders);
finally
ReqStream.Free;
end;
if Resp.StatusCode <> 200 then
raise Exception.CreateFmt('REST call failed with status %d', [Resp.StatusCode]);
ContentText := Resp.ContentAsString(TEncoding.UTF8);
JsonRoot := TJSONObject.ParseJSONValue(ContentText);
try
if JsonRoot = nil then
raise Exception.Create('Invalid JSON payload. Preview: ' + ResponsePreview(ContentText));
if not TryGetArrayFromPayload(JsonRoot, Arr) then
raise Exception.CreateFmt('Unexpected REST payload (%s). Preview: %s',
[JsonRoot.ClassName, ResponsePreview(ContentText)]);
ItemsInPage := Arr.Count;
for var i := 0 to Arr.Count - 1 do
begin
if not (Arr.Items[i] is TJSONObject) then
Continue;
var Obj := Arr.Items[i] as TJSONObject;
var Pkg := TGetItPackageInfo.Create;
Pkg.Id := JsonValueToString(Obj, 'Id');
Pkg.PackageId := JsonValueToString(Obj, 'PackageId');
Pkg.Version := JsonValueToString(Obj, 'Version');
Pkg.Name := JsonValueToString(Obj, 'Name');
Pkg.Vendor := JsonValueToString(Obj, 'Vendor');
Pkg.Description := JsonValueToString(Obj, 'Description');
Pkg.TypeDescription := JsonValueToString(Obj, 'TypeDescription');
Pkg.State := JsonValueToString(Obj, 'State');
Pkg.Subscription := JsonValueToString(Obj, 'Subscription');
ReadCategories(Obj, Pkg.CategoryIds, Pkg.CategoryNames);
Pkg.LibCode := JsonValueToString(Obj, 'LibCode');
Pkg.LibCodeName := JsonValueToString(Obj, 'LibCodeName');
Pkg.LibSize := JsonValueToString(Obj, 'LibSize');
Pkg.LibUrl := JsonValueToString(Obj, 'LibUrl');
Pkg.VendorUrl := JsonValueToString(Obj, 'VendorUrl');
Pkg.VersionTimestamp := JsonValueToString(Obj, 'VersionTimestamp');
Pkg.Modified := JsonValueToString(Obj, 'Modified');
if Pkg.Name.IsEmpty then
Pkg.Name := TGetItCore.ExtractNameFromId(Pkg.Id);
if Pkg.Id <> '' then
Target.AddOrSetValue(Pkg.Id, Pkg)
else
Pkg.Free;
end;
finally
JsonRoot.Free;
end;
Inc(PageStart, PageSize);
until ItemsInPage < PageSize;
finally
HttpClient.Free;
end;
end;
end.