-
Notifications
You must be signed in to change notification settings - Fork 123
/
uGitHub.pas
613 lines (518 loc) · 18.2 KB
/
uGitHub.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
unit uGitHub;
// *************************************************
// Generated By: JsonToDelphiClass - 0.64
// Generated On: 2015-01-03 15:32:27
// *************************************************
// Created By : Petar Georgiev - 2014
// Web Site : http://pgeorgiev.com
// *************************************************
interface
uses Generics.Collections, Rest.Json, IdUri, IdHttp,
IdSSLOpenSSL, System.JSON, SysUtils, Classes;
type
// Represents a serializable object with HTTP/REST capabilities (via Indy)
// HTTPS connections require OpenSSL binaries!
// Use the "AOnBeforeRequest" event to setup HTTP client's parameters like timeout, encoding etc.
//
TUGitHubSerializableObject = class abstract
protected
// As per http://www.restapitutorial.com/lessons/httpmethods.html
class procedure EnsureHttpResponseCode(AHttpResponseCode: integer; AUrl: string; AValidValues: array of integer);
class procedure EnsureHttpContentType(AHttp: TIdHttp);
public
// Generic Web Request method
class function WebRequest(AUrl: string; AOnRequest: TProc<TIdHttp>): integer;
// Returns an instance of T from a JSON string via GET request. AArrayProperty is intended for internal use only!
// HttpGet is reintroduced in descendant classes to return concrete instance
class function HttpGet<T: class, constructor>
(AUrl: string; AOnBeforeRequest: TProc<TIdHttp> = nil; AArrayProperty: string = ''): T;
// Performs POST request, sends the current object as JSON string and returns server's response as text.
function HttpPost (AUrl: string; AOnBeforeRequest: TProc<TIdHttp> = nil): string;
// Performs PUT request, sends the current object as JSON string and returns server's response as text.
function HttpPut (AUrl: string; AOnBeforeRequest: TProc<TIdHttp> = nil): string;
// Performs DELETE request and returns server's response as text. This method exists just REST compliance.
function HttpDelete(AUrl: string; AOnBeforeRequest: TProc<TIdHttp> = nil): string;
end;
TUploaderClass = class
private
FAvatar_url: String;
FEvents_url: String;
FFollowers_url: String;
FFollowing_url: String;
FGists_url: String;
FGravatar_id: String;
FHtml_url: String;
FId: Extended;
FLogin: String;
FOrganizations_url: String;
FReceived_events_url: String;
FRepos_url: String;
FSite_admin: Boolean;
FStarred_url: String;
FSubscriptions_url: String;
FType: String;
FUrl: String;
public
property avatar_url: String read FAvatar_url write FAvatar_url;
property events_url: String read FEvents_url write FEvents_url;
property followers_url: String read FFollowers_url write FFollowers_url;
property following_url: String read FFollowing_url write FFollowing_url;
property gists_url: String read FGists_url write FGists_url;
property gravatar_id: String read FGravatar_id write FGravatar_id;
property html_url: String read FHtml_url write FHtml_url;
property id: Extended read FId write FId;
property login: String read FLogin write FLogin;
property organizations_url: String read FOrganizations_url write FOrganizations_url;
property received_events_url: String read FReceived_events_url write FReceived_events_url;
property repos_url: String read FRepos_url write FRepos_url;
property site_admin: Boolean read FSite_admin write FSite_admin;
property starred_url: String read FStarred_url write FStarred_url;
property subscriptions_url: String read FSubscriptions_url write FSubscriptions_url;
property &type: String read FType write FType;
property url: String read FUrl write FUrl;
function ToJsonString: string;
class function FromJsonString(AJsonString: string): TUploaderClass;
end;
TAssetsClass = class
private
FBrowser_download_url: String;
FContent_type: String;
FCreated_at: String;
FDownload_count: Extended;
FId: Extended;
FName: String;
FSize: Extended;
FState: String;
FUpdated_at: String;
FUploader: TUploaderClass;
FUrl: String;
public
property browser_download_url: String read FBrowser_download_url write FBrowser_download_url;
property content_type: String read FContent_type write FContent_type;
property created_at: String read FCreated_at write FCreated_at;
property download_count: Extended read FDownload_count write FDownload_count;
property id: Extended read FId write FId;
property name: String read FName write FName;
property size: Extended read FSize write FSize;
property state: String read FState write FState;
property updated_at: String read FUpdated_at write FUpdated_at;
property uploader: TUploaderClass read FUploader write FUploader;
property url: String read FUrl write FUrl;
constructor Create;
destructor Destroy; override;
function ToJsonString: string;
class function FromJsonString(AJsonString: string): TAssetsClass;
end;
TAuthorClass = class
private
FAvatar_url: String;
FEvents_url: String;
FFollowers_url: String;
FFollowing_url: String;
FGists_url: String;
FGravatar_id: String;
FHtml_url: String;
FId: Extended;
FLogin: String;
FOrganizations_url: String;
FReceived_events_url: String;
FRepos_url: String;
FSite_admin: Boolean;
FStarred_url: String;
FSubscriptions_url: String;
FType: String;
FUrl: String;
public
property avatar_url: String read FAvatar_url write FAvatar_url;
property events_url: String read FEvents_url write FEvents_url;
property followers_url: String read FFollowers_url write FFollowers_url;
property following_url: String read FFollowing_url write FFollowing_url;
property gists_url: String read FGists_url write FGists_url;
property gravatar_id: String read FGravatar_id write FGravatar_id;
property html_url: String read FHtml_url write FHtml_url;
property id: Extended read FId write FId;
property login: String read FLogin write FLogin;
property organizations_url: String read FOrganizations_url write FOrganizations_url;
property received_events_url: String read FReceived_events_url write FReceived_events_url;
property repos_url: String read FRepos_url write FRepos_url;
property site_admin: Boolean read FSite_admin write FSite_admin;
property starred_url: String read FStarred_url write FStarred_url;
property subscriptions_url: String read FSubscriptions_url write FSubscriptions_url;
property &type: String read FType write FType;
property url: String read FUrl write FUrl;
function ToJsonString: string;
class function FromJsonString(AJsonString: string): TAuthorClass;
end;
TReleaseClass = class(TUGitHubSerializableObject)
private
FAssets: TArray<TAssetsClass>;
FAssets_url: String;
FAuthor: TAuthorClass;
FBody: String;
FCreated_at: String;
FDraft: Boolean;
FHtml_url: String;
FId: Extended;
FName: String;
FPrerelease: Boolean;
FPublished_at: String;
FTag_name: String;
FTarball_url: String;
FTarget_commitish: String;
FUpload_url: String;
FUrl: String;
FZipball_url: String;
public
property assets: TArray<TAssetsClass> read FAssets write FAssets;
property assets_url: String read FAssets_url write FAssets_url;
property author: TAuthorClass read FAuthor write FAuthor;
property body: String read FBody write FBody;
property created_at: String read FCreated_at write FCreated_at;
property draft: Boolean read FDraft write FDraft;
property html_url: String read FHtml_url write FHtml_url;
property id: Extended read FId write FId;
property name: String read FName write FName;
property prerelease: Boolean read FPrerelease write FPrerelease;
property published_at: String read FPublished_at write FPublished_at;
property tag_name: String read FTag_name write FTag_name;
property tarball_url: String read FTarball_url write FTarball_url;
property target_commitish: String read FTarget_commitish write FTarget_commitish;
property upload_url: String read FUpload_url write FUpload_url;
property url: String read FUrl write FUrl;
property zipball_url: String read FZipball_url write FZipball_url;
constructor Create;
destructor Destroy; override;
function ToJsonString: string;
class function FromJsonString(AJsonString: string): TReleaseClass;
class function HttpGet(AUrl: string; AOnBeforeRequest: TProc<TIdHttp> = nil): TReleaseClass;
end;
TGitReleasesClass = class
private
FReleases: TArray<TReleaseClass>;
public
property Releases: TArray<TReleaseClass> read FReleases write FReleases;
constructor Create;
destructor Destroy; override;
function ToJsonString: string;
class function FromJsonString(AJsonString: string): TGitReleasesClass;
class function FromUrl(AUrl: string; ATimeout: integer): TGitReleasesClass;
end;
TErrorClass = class
private
FDocumentation_url: String;
FMessage: String;
public
property documentation_url: String read FDocumentation_url write FDocumentation_url;
property message: String read FMessage write FMessage;
function ToJsonString: string;
class function FromJsonString(AJsonString: string): TErrorClass;
end;
implementation
{TUploaderClass}
function TUploaderClass.ToJsonString: string;
begin
result := TJson.ObjectToJsonString(self);
end;
class function TUploaderClass.FromJsonString(AJsonString: string): TUploaderClass;
begin
result := TJson.JsonToObject<TUploaderClass>(AJsonString)
end;
{TAssetsClass}
constructor TAssetsClass.Create;
begin
inherited;
FUploader := TUploaderClass.Create();
end;
destructor TAssetsClass.Destroy;
begin
FUploader.free;
inherited;
end;
function TAssetsClass.ToJsonString: string;
begin
result := TJson.ObjectToJsonString(self);
end;
class function TAssetsClass.FromJsonString(AJsonString: string): TAssetsClass;
begin
result := TJson.JsonToObject<TAssetsClass>(AJsonString)
end;
{TAuthorClass}
function TAuthorClass.ToJsonString: string;
begin
result := TJson.ObjectToJsonString(self);
end;
class function TAuthorClass.FromJsonString(AJsonString: string): TAuthorClass;
begin
result := TJson.JsonToObject<TAuthorClass>(AJsonString)
end;
{TReleaseClass}
constructor TReleaseClass.Create;
begin
inherited;
FAuthor := TAuthorClass.Create();
end;
destructor TReleaseClass.Destroy;
var
LassetsItem: TAssetsClass;
begin
for LassetsItem in FAssets do
LassetsItem.free;
FAuthor.free;
inherited;
end;
function TReleaseClass.ToJsonString: string;
begin
result := TJson.ObjectToJsonString(self);
end;
class function TReleaseClass.FromJsonString(AJsonString: string): TReleaseClass;
begin
result := TJson.JsonToObject<TReleaseClass>(AJsonString)
end;
class function TReleaseClass.HttpGet(AUrl: string; AOnBeforeRequest: TProc<TIdHttp>): TReleaseClass;
begin
result := inherited HttpGet<TReleaseClass>(AUrl, AOnBeforeRequest);
end;
{TGitReleasesClass}
constructor TGitReleasesClass.Create;
begin
inherited;
end;
destructor TGitReleasesClass.Destroy;
var
LReleasesItem: TReleaseClass;
begin
for LReleasesItem in FReleases do
LReleasesItem.free;
inherited;
end;
function TGitReleasesClass.ToJsonString: string;
begin
result := TJson.ObjectToJsonString(self);
end;
class function TGitReleasesClass.FromJsonString(AJsonString: string): TGitReleasesClass;
begin
result := TJson.JsonToObject<TGitReleasesClass>(AJsonString)
end;
class function TGitReleasesClass.FromUrl(AUrl: string;
ATimeout: integer): TGitReleasesClass;
var
LUri: TIdUri;
LHttp: TIdHttp;
LSslIoHandler: TIdSSLIOHandlerSocketOpenSSL;
LString: string;
LJsonValue: TJsonValue;
LJsonObject: TJsonObject;
begin
result := nil;
LUri := TIdURI.Create(AUrl);
try
LHttp := TIdHTTP.Create;
try
LHttp.ConnectTimeout := ATimeout;
LHttp.ReadTimeout := ATimeout;
LHttp.HandleRedirects := true;
if LUri.Protocol.ToLower = 'https' then
begin
LSslIoHandler := TIdSSLIOHandlerSocketOpenSSL.Create(LHttp);
LHttp.IOHandler := LSslIoHandler;
end;
LString := LHttp.Get(AUrl);
if LHttp.ResponseCode <> 200 then
raise Exception.CreateFmt('Error getting JSON string from %s. Http error code: %d', [AUrl, LHttp.ResponseCode]);
LJsonValue := TJSONObject.ParseJSONValue(LString);
if LJsonValue = nil then
raise Exception.Create('Unable to parse JSON string!');
try
LJsonValue.Owned := false;
if LJsonValue is TJSONArray then
begin
LJsonObject := TJSONObject.Create;
try
LJsonObject.AddPair('Releases', LJsonValue);
LString := LJsonObject.ToJSON;
finally
LJsonObject.Free;
end;
end;
result := TGitReleasesClass.FromJsonString(LString);
finally
LJsonValue.Free;
end;
finally
LHttp.Free;
end;
finally
LUri.Free;
end;
end;
{TErrorClass}
function TErrorClass.ToJsonString: string;
begin
result := TJson.ObjectToJsonString(self);
end;
class function TErrorClass.FromJsonString(AJsonString: string): TErrorClass;
begin
result := TJson.JsonToObject<TErrorClass>(AJsonString)
end;
{ TUGitHubSerializableObject }
class procedure TUGitHubSerializableObject.EnsureHttpContentType(
AHttp: TIdHttp);
begin
if AHttp.Response.ContentType <> 'application/json' then
raise Exception.CreateFmt('Invalid content type %s!', [AHttp.Response.ContentType]);
end;
class procedure TUGitHubSerializableObject.EnsureHttpResponseCode(
AHttpResponseCode: integer; AUrl: string; AValidValues: array of integer);
var
LValue: integer;
begin
for LValue in AValidValues do
if LValue = AHttpResponseCode then exit;
raise Exception.CreateFmt('The request to %s has failed with code %d', [AUrl, AHttpResponseCode]);
end;
function TUGitHubSerializableObject.HttpDelete(AUrl: string;
AOnBeforeRequest: TProc<TIdHttp>): string;
var
LResult: string;
begin
WebRequest(AUrl,
procedure(LHttp: TIdHttp)
begin
// Allow HTTP client pre-configuration
if assigned(AOnBeforeRequest) then
AOnBeforeRequest(LHttp);
LResult := LHttp.Delete(AUrl);
EnsureHttpResponseCode(LHttp.ResponseCode, AUrl, [200, 204]);
end
);
result := LResult;
end;
class function TUGitHubSerializableObject.HttpGet<T>(AUrl: string; AOnBeforeRequest: TProc<TIdHttp>; AArrayProperty: string): T;
var
LResult: T;
begin
WebRequest(AUrl,
procedure(LHttp: TIdHttp)
var
LString: string;
LJsonValue: TJsonValue;
LJsonObject: TJsonObject;
begin
// Allow HTTP client pre-configuration
if assigned(AOnBeforeRequest) then
AOnBeforeRequest(LHttp);
LString := LHttp.Get(AUrl);
EnsureHttpResponseCode(LHttp.ResponseCode, AUrl, [200, 304]);
EnsureHttpContentType(LHttp);
LJsonValue := TJSONObject.ParseJSONValue(LString);
if LJsonValue = nil then
raise Exception.Create('Unable to parse JSON string!');
try
LJsonValue.Owned := false;
if LJsonValue is TJSONArray then
if (AArrayProperty <> '') then
begin
LJsonObject := TJSONObject.Create;
try
LJsonObject.AddPair(AArrayProperty, LJsonValue);
LString := LJsonObject.ToJSON;
finally
LJsonObject.Free;
end;
end
else
raise Exception.CreateFmt('The class %s does not accept array values!', [LResult.className]);
finally
LJsonValue.Free;
end;
LResult := TJson.JsonToObject<T>(LString);
end
);
result := LResult;
end;
function TUGitHubSerializableObject.HttpPost(AUrl: string;
AOnBeforeRequest: TProc<TIdHttp>): string;
var
LResult: string;
begin
WebRequest(AUrl,
procedure(LHttp: TIdHttp)
var
LStringStream: TStringStream;
begin
// Allow HTTP client pre-configuration
if assigned(AOnBeforeRequest) then
AOnBeforeRequest(LHttp);
LResult := TJson.ObjectToJsonString(self);
LStringStream := TStringStream.Create(LResult, TEncoding.GetEncoding(LHttp.Request.ContentEncoding));
try
LResult := LHttp.Post(AUrl, LStringStream);
EnsureHttpResponseCode(LHttp.ResponseCode, AUrl, [200, 201, 202, 204]);
EnsureHttpContentType(LHttp);
finally
LStringStream.Free;
end;
end
);
result := LResult;
end;
function TUGitHubSerializableObject.HttpPut(AUrl: string;
AOnBeforeRequest: TProc<TIdHttp>): string;
var
LResult: string;
begin
WebRequest(AUrl,
procedure(LHttp: TIdHttp)
var
LStringStream: TStringStream;
begin
// Allow HTTP client pre-configuration
if assigned(AOnBeforeRequest) then
AOnBeforeRequest(LHttp);
LResult := TJson.ObjectToJsonString(self);
LStringStream := TStringStream.Create(LResult, TEncoding.GetEncoding(LHttp.Request.ContentEncoding));
try
LResult := LHttp.Put(AUrl, LStringStream);
EnsureHttpResponseCode(LHttp.ResponseCode, AUrl, [200, 204]);
EnsureHttpContentType(LHttp);
finally
LStringStream.Free;
end;
end
);
result := LResult;
end;
class function TUGitHubSerializableObject.WebRequest(AUrl: string; AOnRequest: TProc<TIdHttp>): integer;
var
LUri: TIdUri;
LHttp: TIdHttp;
LSslIoHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
LUri := TIdURI.Create(AUrl);
try
LHttp := TIdHTTP.Create;
try
LHttp.HandleRedirects := true;
// Default encoding
LHttp.Request.ContentEncoding := 'utf-8';
// Specify Content-Type header
LHttp.Request.ContentType := 'application/json';
// Replace default IOHandler with TIdSSLIOHandlerSocketOpenSSL if the connection is SSL based
if LUri.Protocol.ToLower = 'https' then
begin
LSslIoHandler := TIdSSLIOHandlerSocketOpenSSL.Create(LHttp);
LHttp.IOHandler := LSslIoHandler;
end;
try
AOnRequest(LHttp);
finally
result := LHttp.ResponseCode;
end;
finally
LHttp.Free;
end;
finally
LUri.Free;
end;
end;
end.