Repository navigation
Expand file tree
/
Copy pathuGetItRestService.pas
More file actions
735 lines (667 loc) · 26 KB
/
Copy pathuGetItRestService.pas
File metadata and controls
735 lines (667 loc) · 26 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
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
unit uGetItRestService;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections, System.JSON,
System.Net.HttpClient, uGetItTypes;
type
EGetItDownloadError = class(Exception);
EGetItCatalogError = class(Exception);
TGetItRestRequest = record
ServiceUrl: string;
CatalogVersion: string;
ProductId: string;
ProductSku: string;
IDEVersion: string;
BuildNumber: string;
InstalledPersonalities: TGetItPersonalities;
SearchText: string;
end;
TGetItRestLogProc = reference to procedure(const Msg: string);
TGetItRestService = record
public
class function FormatCatalogDetails(const Json: string): string; static;
class function FetchResource(const Url: string): IHTTPResponse; static;
class function DownloadFileName(const Url: string): string; static;
class function DownloadLocalPackage(const Request: TGetItRestRequest;
const PackageId, ParentDirectory: string): string; static;
class procedure DownloadFile(const Url, TargetFile: string; const ReplaceExisting: Boolean); static;
class function CatalogInfoUrl(const ServiceUrl: string): string; static;
class function CatalogInfoByIdsUrl(const ServiceUrl: string): string; static;
class procedure FetchPackages(const Request: TGetItRestRequest;
const Target: TObjectDictionary<string, TGetItPackageInfo>;
const OnLog: TGetItRestLogProc); static;
class procedure FetchPackagesByIds(const Request: TGetItRestRequest; const Ids: TStrings;
const Target: TObjectDictionary<string, TGetItPackageInfo>;
const OnLog: TGetItRestLogProc); static;
private
class procedure FetchCatalog(const Request: TGetItRestRequest; const BuildNumber: string;
const Target: TObjectDictionary<string, TGetItPackageInfo>; const OnLog: TGetItRestLogProc); static;
class function BuildRestBody(const Request: TGetItRestRequest; const BuildNumber: string; const StartIndex, EndIndex: Integer): string; static;
class function BuildInfoByIdsBody(const Request: TGetItRestRequest; const IdList: string): string; static;
class function JsonValueToString(const Obj: TJSONObject; const Name: string): string; static;
class function ReadPackage(const Obj: TJSONObject): TGetItPackageInfo; static;
class function ReadTargets(const Obj: TJSONObject; const Field: string): TArray<TGetItNamedId>; static;
class procedure AddJsonPackages(const Arr: TJSONArray;
const Target: TObjectDictionary<string, TGetItPackageInfo>; const CatalogUnavailable: Boolean); 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
Winapi.Windows, System.IOUtils, System.Net.URLClient, System.NetEncoding, System.StrUtils,
uGetItCore;
class function TGetItRestService.FormatCatalogDetails(const Json: string): string;
var
Root: TJSONValue;
Lines: TStringList;
procedure AppendValue(const Name: string; const Value: TJSONValue; const Indent: string);
var
Item, IdValue, NameValue: TJSONValue;
begin
if Value is TJSONObject then
begin
if Name <> '' then
Lines.Add(Indent + Name + ':');
for var Pair in TJSONObject(Value) do
AppendValue(Pair.JsonString.Value, Pair.JsonValue, Indent + ' ');
end
else if Value is TJSONArray then
begin
Lines.Add(Indent + Name + ':');
for var i := 0 to TJSONArray(Value).Count - 1 do
begin
Item := TJSONArray(Value).Items[i];
if MatchText(Name, ['Categories', 'LibPlatforms', 'LibOSes']) and
(Item is TJSONObject) and (TJSONObject(Item).Count = 2) then
begin
IdValue := TJSONObject(Item).GetValue('Id');
NameValue := TJSONObject(Item).GetValue('Name');
if (IdValue <> nil) and (NameValue <> nil) then
begin
Lines.Add(IdValue.Value + ', ' + NameValue.Value);
Continue;
end;
end;
AppendValue(IntToStr(i + 1), Item, Indent + ' ');
end;
end
else
Lines.Add(Indent + Name + ': ' + Value.Value);
end;
begin
Result := '';
if Json = '' then
Exit;
Root := TJSONObject.ParseJSONValue(Json, False, True);
try
Lines := TStringList.Create;
try
for var Pair in Root as TJSONObject do
if not MatchText(Pair.JsonString.Value,
['Image', 'Icon', 'LibLicense', 'LibUrl', 'LibProjectUrl']) then
AppendValue(Pair.JsonString.Value, Pair.JsonValue, '');
Result := Lines.Text;
finally
Lines.Free;
end;
finally
Root.Free;
end;
end;
class function TGetItRestService.FetchResource(const Url: string): IHTTPResponse;
var
Client: THTTPClient;
begin
if not (Url.StartsWith('https://', True) or Url.StartsWith('http://', True)) then
raise EGetItDownloadError.Create('Only HTTP/HTTPS resource links are supported.');
Client := THTTPClient.Create;
try
Client.ConnectionTimeout := 2000;
Client.ResponseTimeout := 5000;
Result := Client.Get(Url);
if Result.StatusCode <> 200 then
raise EGetItDownloadError.CreateFmt('Resource download failed with HTTP status %d.', [Result.StatusCode]);
if (Result.ContentLength >= 0) and (Result.ContentStream.Size <> Result.ContentLength) then
raise EGetItDownloadError.Create('The downloaded resource is incomplete.');
finally
Client.Free;
end;
end;
class function TGetItRestService.DownloadFileName(const Url: string): string;
begin
Result := TNetEncoding.URL.Decode(TPath.GetFileName(TURI.Create(Url).Path).Replace('+', '%2B'));
if (Result = '') or Result.EndsWith('.') or Result.EndsWith(' ') then
raise EArgumentException.Create('The download URL has no valid file name.');
for var Ch in Result do
if CharInSet(Ch, [#0..#31, '<', '>', ':', '"', '/', '\', '|', '?', '*']) then
raise EArgumentException.Create('The download URL contains an invalid file name.');
end;
class procedure TGetItRestService.DownloadFile(const Url, TargetFile: string; const ReplaceExisting: Boolean);
var
HttpClient: THTTPClient;
Response: IHTTPResponse;
Stream: THandleStream;
TempFile: string;
FileId: TGUID;
FileHandle: THandle;
MoveFlags: DWORD;
begin
CreateGUID(FileId);
TempFile := TPath.Combine(ExtractFilePath(TargetFile), '.getit-' + GUIDToString(FileId) + '.part');
HttpClient := THTTPClient.Create;
try
FileHandle := CreateFile(PChar(TempFile), GENERIC_WRITE, 0, nil, CREATE_NEW,
FILE_ATTRIBUTE_NORMAL, 0);
if FileHandle = INVALID_HANDLE_VALUE then
RaiseLastOSError;
// Only this newly created file is removed on failure. The destination is untouched.
try
try
Stream := THandleStream.Create(FileHandle);
try
Response := HttpClient.Get(Url, Stream);
if Response.StatusCode <> 200 then
raise EGetItDownloadError.CreateFmt('Download failed with HTTP status %d.', [Response.StatusCode]);
if (Response.ContentLength >= 0) and (Stream.Size <> Response.ContentLength) then
raise EGetItDownloadError.Create('The downloaded file is incomplete.');
finally
Stream.Free;
end;
finally
CloseHandle(FileHandle);
end;
MoveFlags := MOVEFILE_WRITE_THROUGH;
if ReplaceExisting then
MoveFlags := MoveFlags or MOVEFILE_REPLACE_EXISTING;
if not MoveFileEx(PChar(TempFile), PChar(TargetFile), MoveFlags) then
RaiseLastOSError;
finally
if FileExists(TempFile) then
TFile.Delete(TempFile);
end;
finally
HttpClient.Free;
end;
end;
class function TGetItRestService.DownloadLocalPackage(const Request: TGetItRestRequest;
const PackageId, ParentDirectory: string): string;
var
Packages: TObjectDictionary<string, TGetItPackageInfo>;
Ids: TStringList;
Pkg: TGetItPackageInfo;
Manifest: TJSONObject;
Root: TJSONValue;
StageDir, FileName, ArchiveExt: string;
StageId: TGUID;
function FieldText(const Name: string): string;
var
Value: TJSONValue;
begin
Value := Manifest.GetValue(Name);
if (Value = nil) or (Value is TJSONNull) then
Exit('');
if not (Value is TJSONString) then
raise EGetItCatalogError.Create('Invalid package field: ' + Name);
Result := Value.Value;
end;
procedure SetText(const Name, Value: string);
begin
Manifest.RemovePair(Name).Free;
Manifest.AddPair(Name, Value);
end;
procedure DownloadResource(const SourceField, LocalField, Prefix: string;
const Required: Boolean);
var
Url, LocalName: string;
begin
Url := FieldText(SourceField);
if Url = '' then
begin
if Required then
raise EGetItCatalogError.Create('Missing package field: ' + SourceField);
Exit;
end;
if not (StartsText('https://', Url) or StartsText('http://', Url)) then
raise EGetItCatalogError.Create('Expected HTTP resource: ' + SourceField);
LocalName := Prefix + DownloadFileName(Url);
DownloadFile(Url, TPath.Combine(StageDir, LocalName), False);
SetText(LocalField, LocalName);
end;
begin
if (PackageId = '') or (PackageId <> Trim(PackageId)) then
raise EArgumentException.Create('Invalid package directory name.');
for var Ch in PackageId do
if CharInSet(Ch, ['<', '>', ':', '"', '/', '\', '|', '?', '*']) or (Ord(Ch) < 32) then
raise EArgumentException.Create('Invalid package directory name.');
if PackageId.EndsWith('.') then
raise EArgumentException.Create('Invalid package directory name.');
Result := TPath.Combine(TPath.GetFullPath(ParentDirectory), 'GetIt-' + PackageId);
if TDirectory.Exists(Result) or TFile.Exists(Result) then
raise EGetItDownloadError.Create('Destination already exists: ' + Result);
if not TDirectory.Exists(ParentDirectory) then
raise EGetItDownloadError.Create('Destination parent directory does not exist.');
Packages := TObjectDictionary<string, TGetItPackageInfo>.Create([doOwnsValues]);
try
Ids := TStringList.Create;
try
Ids.Add(PackageId);
FetchPackagesByIds(Request, Ids, Packages, nil);
finally
Ids.Free;
end;
if not Packages.TryGetValue(PackageId, Pkg) then
raise EGetItCatalogError.Create('Package not returned by infobyids: ' + PackageId);
ArchiveExt := TPath.GetExtension(DownloadFileName(Pkg.LibUrl));
if not MatchText(ArchiveExt, ['.zip', '.7z']) then
raise EGetItCatalogError.Create('Local GetIt import requires a ZIP or 7z archive.');
Root := TJSONObject.ParseJSONValue(Pkg.CatalogDetailsJson);
try
if not (Root is TJSONObject) then
raise EGetItCatalogError.Create('Invalid package JSON.');
Manifest := TJSONObject(Root);
if not (Manifest.GetValue('Actions') is TJSONArray) then
raise EGetItCatalogError.Create('The server did not provide an Actions array.');
CreateGUID(StageId);
StageDir := TPath.Combine(TPath.GetFullPath(ParentDirectory),
'.getit-' + GUIDToString(StageId) + '.partial');
if not CreateDirectory(PChar(StageDir), nil) then
RaiseLastOSError;
try
// Preserve all metadata and actions in the manifest, mapping local resource fields.
DownloadResource('LibUrl', 'Url', 'archive-', True);
DownloadResource('LibLicense', 'License', 'license-', True);
DownloadResource('Image', 'Image', 'image-', False);
DownloadResource('Icon', 'Icon', 'icon-', False);
SetText('LicenseName', FieldText('LibLicenseName'));
SetText('ProjectUrl', FieldText('LibProjectUrl'));
TFile.WriteAllText(TPath.Combine(StageDir, 'package.json'),
Manifest.ToJSON, TEncoding.UTF8);
TFile.WriteAllText(TPath.Combine(StageDir, 'README.txt'),
'Open package.json using GetIt / Load Local Package in the matching RAD Studio version.' +
sLineBreak + 'The archive and actions are unchanged. Actions or dependencies may require' +
sLineBreak + 'additional downloads; this bundle does not guarantee a fully offline installation.' +
sLineBreak + 'GetIt may show 1 Jan 2000 for a local installation despite the original timestamp.' +
sLineBreak, TEncoding.UTF8);
TDirectory.Move(StageDir, Result);
finally
// Only the newly created, flat staging directory belongs to this operation.
if TDirectory.Exists(StageDir) then
begin
for FileName in TDirectory.GetFiles(StageDir) do
TFile.Delete(FileName);
TDirectory.Delete(StageDir, False);
end;
end;
finally
Root.Free;
end;
finally
Packages.Free;
end;
end;
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.CatalogInfoByIdsUrl(const ServiceUrl: string): string;
var
BaseUrl: string;
begin
BaseUrl := Trim(ServiceUrl);
if BaseUrl.EndsWith('/') then
Result := BaseUrl + 'catalog/infobyids'
else
Result := BaseUrl + '/catalog/infobyids';
end;
class function TGetItRestService.BuildRestBody(const Request: TGetItRestRequest;
const BuildNumber: string; const StartIndex, EndIndex: Integer): string;
begin
Result := Format(
'Start=%d&End=%d&Categories=-1&CalculateSize=0&FilterByLicense=0&Order=0' +
'&Version=%s&Identity=RADSTUDIO&Personalities=1%%3B2%%3B3%%3B4%%3B5%%3B6%%3B7' +
'&Platforms=0&Frameworks=0&Language=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(Request.IDEVersion),
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)]);
if BuildNumber <> '' then
Result := Result + '&BuildNumber=' + TNetEncoding.URL.Encode(BuildNumber);
end;
class function TGetItRestService.BuildInfoByIdsBody(const Request: TGetItRestRequest; const IdList: string): string;
begin
Result := Format('CatalogVersion=%s&Id=%s',
[TNetEncoding.URL.Encode(Request.CatalogVersion),
TNetEncoding.URL.Encode(IdList)]);
end;
class function TGetItRestService.JsonValueToString(const Obj: TJSONObject; const Name: string): string;
begin
Result := '';
Obj.TryGetValue<string>(Name, Result);
end;
class function TGetItRestService.ReadTargets(const Obj: TJSONObject;
const Field: string): TArray<TGetItNamedId>;
var
Value: TJSONValue;
Item: TJSONObject;
begin
Result := nil;
Value := Obj.GetValue(Field);
if (Value = nil) or (Value is TJSONNull) then
Exit;
if not (Value is TJSONArray) then
raise EGetItCatalogError.Create(Field + ' must be an array.');
SetLength(Result, TJSONArray(Value).Count);
for var i := 0 to High(Result) do
begin
if not (TJSONArray(Value).Items[i] is TJSONObject) then
raise EGetItCatalogError.Create(Field + ' contains a non-object entry.');
Item := TJSONObject(TJSONArray(Value).Items[i]);
Result[i].Id := JsonValueToString(Item, 'Id');
Result[i].Name := JsonValueToString(Item, 'Name');
if Result[i].Id = '' then
raise EGetItCatalogError.Create(Field + ' contains an entry without an ID.');
end;
end;
class function TGetItRestService.ReadPackage(const Obj: TJSONObject): TGetItPackageInfo;
var
Platforms, OSes: TArray<TGetItNamedId>;
begin
Platforms := ReadTargets(Obj, 'LibPlatforms');
OSes := ReadTargets(Obj, 'LibOSes');
Result := TGetItPackageInfo.Create;
Result.Platforms := Platforms;
Result.OSes := OSes;
Result.Id := JsonValueToString(Obj, 'Id');
Result.PackageId := JsonValueToString(Obj, 'PackageId');
Result.Version := JsonValueToString(Obj, 'Version');
Result.Name := JsonValueToString(Obj, 'Name');
Result.Vendor := JsonValueToString(Obj, 'Vendor');
Result.Description := JsonValueToString(Obj, 'Description');
Result.Tags := JsonValueToString(Obj, 'Tags');
Result.TypeDescription := JsonValueToString(Obj, 'TypeDescription');
Result.State := JsonValueToString(Obj, 'State');
Result.Subscription := JsonValueToString(Obj, 'Subscription');
ReadCategories(Obj, Result.CategoryIds, Result.CategoryNames);
Result.LibCode := JsonValueToString(Obj, 'LibCode');
Result.LibCodeName := JsonValueToString(Obj, 'LibCodeName');
Result.LibSize := JsonValueToString(Obj, 'LibSize');
Result.LibUrl := JsonValueToString(Obj, 'LibUrl');
Result.LibMD5 := JsonValueToString(Obj, 'LibMD5');
Result.VendorUrl := JsonValueToString(Obj, 'VendorUrl');
Result.VersionTimestamp := JsonValueToString(Obj, 'VersionTimestamp');
Result.Modified := JsonValueToString(Obj, 'Modified');
Result.CatalogDetailsJson := Obj.ToJSON;
Result.ImageUrl := JsonValueToString(Obj, 'Image');
Result.IconUrl := JsonValueToString(Obj, 'Icon');
Result.LicenseUrl := JsonValueToString(Obj, 'LibLicense');
Result.LicenseName := JsonValueToString(Obj, 'LibLicenseName');
Result.ProjectUrl := JsonValueToString(Obj, 'LibProjectUrl');
if Result.Name.IsEmpty then
Result.Name := TGetItCore.ExtractNameFromId(Result.Id);
end;
class procedure TGetItRestService.AddJsonPackages(const Arr: TJSONArray;
const Target: TObjectDictionary<string, TGetItPackageInfo>; const CatalogUnavailable: Boolean);
begin
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 := ReadPackage(Obj);
Pkg.CatalogUnavailable := CatalogUnavailable;
if Pkg.Id <> '' then
Target.AddOrSetValue(Pkg.Id, Pkg)
else
Pkg.Free;
end;
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
Compatible: TObjectDictionary<string, TGetItPackageInfo>;
begin
if Request.BuildNumber = '' then
raise EArgumentException.Create('REST catalog requires the selected IDE executable build number.');
if Request.InstalledPersonalities = [] then
raise EArgumentException.Create('REST catalog requires the installed IDE personalities.');
Compatible := TObjectDictionary<string, TGetItPackageInfo>.Create([doOwnsValues]);
try
OnLog('REST full catalog: all builds and application targets.');
FetchCatalog(Request, '', Target, OnLog);
OnLog('REST compatible catalog: build ' + Request.BuildNumber);
FetchCatalog(Request, Request.BuildNumber, Compatible, OnLog);
for var Pkg in Target.Values do
begin
Pkg.IncompatibilityReasons := [];
if not Compatible.ContainsKey(Pkg.Id) then
Include(Pkg.IncompatibilityReasons, irBuild);
if not TGetItCore.SupportsPackagePersonality(Pkg.LibCode, Request.InstalledPersonalities) then
Include(Pkg.IncompatibilityReasons, irPersonality);
end;
finally
Compatible.Free;
end;
end;
class procedure TGetItRestService.FetchCatalog(const Request: TGetItRestRequest; const BuildNumber: string;
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, BuildNumber, 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 EGetItCatalogError.CreateFmt('REST call failed with status %d', [Resp.StatusCode]);
ContentText := Resp.ContentAsString(TEncoding.UTF8);
JsonRoot := TJSONObject.ParseJSONValue(ContentText);
try
if JsonRoot = nil then
raise EGetItCatalogError.Create('Invalid JSON payload. Preview: ' + ResponsePreview(ContentText));
if not TryGetArrayFromPayload(JsonRoot, Arr) then
raise EGetItCatalogError.CreateFmt('Unexpected REST payload (%s). Preview: %s',
[JsonRoot.ClassName, ResponsePreview(ContentText)]);
ItemsInPage := Arr.Count;
AddJsonPackages(Arr, Target, False);
finally
JsonRoot.Free;
end;
Inc(PageStart, PageSize);
until ItemsInPage < PageSize;
finally
HttpClient.Free;
end;
end;
class procedure TGetItRestService.FetchPackagesByIds(const Request: TGetItRestRequest; const Ids: TStrings;
const Target: TObjectDictionary<string, TGetItPackageInfo>; const OnLog: TGetItRestLogProc);
const
IDS_PER_REQUEST = 40;
var
HttpClient: THTTPClient;
ReqStream: TStringStream;
ReqHeaders: TNetHeaders;
Resp: IHTTPResponse;
RestUrl: string;
Body: string;
ContentText: string;
JsonRoot: TJSONValue;
Arr: TJSONArray;
StartIndex: Integer;
EndIndex: Integer;
IdList: string;
begin
if (Ids = nil) or (Ids.Count = 0) then
Exit;
RestUrl := CatalogInfoByIdsUrl(Request.ServiceUrl);
ReqHeaders := [TNameValuePair.Create('Content-Type', 'application/x-www-form-urlencoded')];
HttpClient := THTTPClient.Create;
try
HttpClient.ConnectionTimeout := 5000;
HttpClient.ResponseTimeout := 15000;
StartIndex := 0;
while StartIndex < Ids.Count do
begin
EndIndex := StartIndex + IDS_PER_REQUEST - 1;
if EndIndex >= Ids.Count then
EndIndex := Ids.Count - 1;
IdList := '';
for var i := StartIndex to EndIndex do
begin
if IdList <> '' then
IdList := IdList + ';';
IdList := IdList + Ids[i];
end;
if Assigned(OnLog) then
OnLog(Format('REST infobyids: %d installed ids (%d..%d).',
[EndIndex - StartIndex + 1, StartIndex + 1, EndIndex + 1]));
Body := BuildInfoByIdsBody(Request, IdList);
ReqStream := TStringStream.Create(Body, TEncoding.UTF8);
try
Resp := HttpClient.Post(RestUrl, ReqStream, nil, ReqHeaders);
finally
ReqStream.Free;
end;
if Resp.StatusCode <> 200 then
raise EGetItCatalogError.CreateFmt('REST infobyids failed with status %d', [Resp.StatusCode]);
ContentText := Resp.ContentAsString(TEncoding.UTF8);
JsonRoot := TJSONObject.ParseJSONValue(ContentText);
try
if JsonRoot = nil then
raise EGetItCatalogError.Create('Invalid infobyids JSON payload. Preview: ' + ResponsePreview(ContentText));
if not TryGetArrayFromPayload(JsonRoot, Arr) then
raise EGetItCatalogError.CreateFmt('Unexpected infobyids payload (%s). Preview: %s',
[JsonRoot.ClassName, ResponsePreview(ContentText)]);
AddJsonPackages(Arr, Target, True);
finally
JsonRoot.Free;
end;
Inc(StartIndex, IDS_PER_REQUEST);
end;
finally
HttpClient.Free;
end;
end;
end.