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
|
{ **********************************************************************
This file is part of the Free Component Library (FCL)
Copyright (c) 2015 The free pascal team.
Base Google service API classes
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit googleservice;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, contnrs, restbase, googlebase, fpwebclient, fpjson, googleclient;
Type
TGoogleAPI = Class;
TGoogleAPIClass = Class of TGoogleAPI;
TGoogleAPIArray = Array of TGoogleAPI;
TGoogleResource = Class;
TGoogleResourceClass = Class of TGoogleResource;
TGoogleResourceArray = Array of TGoogleResource;
TGoogleAPIFactory = Class;
TGoogleAPIFactoryClass = Class of TGoogleAPIFactory;
{ TGoogleAPI }
TScopeInfo = Record
Name : string;
Description : string;
end;
TScopeInfoArray = Array of TScopeInfo;
TAPIInfo = Record
Name : string;
Version : String;
Revision : string;
id : string;
title : String;
description : string;
ownerDomain : String;
ownerName : String;
icon16 : String;
icon32 : String;
documentationLink : String;
rootUrl : string;
basePath : string;
baseURL : String;
Protocol : string;
servicePath : string;
batchPath : String;
AuthScopes : TScopeInfoArray;
end;
TGoogleAPI = Class(TComponent)
private
FGoogleClient: TGoogleClient;
procedure SetGoogleClient(AValue: TGoogleClient);
Protected
Procedure Notification(AComponent: TComponent; Operation: TOperation); override;
Function API : TGoogleAPI;// Used when creating resources.
Public
// All info in one fell swoop
Class Function APIInfo : TAPIInfo; virtual;
Class Function APIName : String; virtual;
Class Function APIVersion : String; virtual; abstract;
Class Function APIRevision : String; virtual; abstract;
Class Function APIID : String; virtual; abstract;
Class Function APITitle : String; virtual; abstract;
Class Function APIDescription : String; virtual; abstract;
Class Function APIOwnerDomain : String; virtual; abstract;
Class Function APIOwnerName : String; virtual; abstract;
Class Function APIIcon16 : String; virtual; abstract;
Class Function APIIcon32 : String; virtual; abstract;
Class Function APIdocumentationLink : String; virtual; abstract;
Class Function APIrootUrl : string; virtual; abstract;
Class Function APIbasePath : string;virtual; abstract;
Class Function APIbaseURL : String;virtual; abstract;
Class Function APIProtocol : string;virtual; abstract;
Class Function APIservicePath : string;virtual; abstract;
Class Function APIbatchPath : String;virtual; abstract;
Class Function APIAuthScopes : TScopeInfoArray;virtual; abstract;
Class Function APINeedsAuth : Boolean ;virtual;
Class Procedure RegisterAPI; virtual;
Class Procedure RegisterAPIResources; virtual;
Function ServiceCall(Const AResource : TGoogleResource; AMethod, APath, AQuery : String; AInput : TGoogleBaseObject; AReturnClass : TGoogleBaseObjectClass) : TGoogleBaseObject; virtual;
Function ServiceCall(Const AResource : TGoogleResource; AMethod, APath, AQuery, AInput : String) : String; virtual;
Function SubstitutePath(Const AResource,APath : String; Const Args : Array of const) : String;virtual;
Function CreateResource(AClass : TGoogleResourceClass) : TGoogleResource; virtual;
Function CreateResource(const Resource : String) : TGoogleResource; virtual;
Published
Property GoogleClient : TGoogleClient Read FGoogleClient Write SetGoogleClient;
end;
{ TGoogleResource }
TGoogleResource = Class(TComponent)
private
FAPI: TGoogleAPI;
Procedure SetAPI(AAPI : TGoogleAPI);
Protected
Procedure CheckAPI;
Procedure Notification(AComponent: TComponent; Operation: TOperation); override;
Public
Class Procedure AddToQuery(Var Q : String; Const AName , AValue : String);virtual;
Class Procedure AddToQuery(Var Q : String; Const AName : String; AValue : Int64);virtual;
Class Procedure AddToQuery(Var Q : String; Const AName : String; AValue : TDateTime);virtual;
Class Procedure AddToQuery(Var Q : String; Const AName : String; AValue : Boolean);virtual;
Function SubstitutePath(Const APath : String; Const Args : Array of const): String;
Function ServiceCall(Const AMethod, APath, AQuery: String; AInput: TGoogleBaseObject; AReturnClass: TGoogleBaseObjectClass): TGoogleBaseObject; virtual;
PubliC
Class Function ResourceName : String; virtual;
Class Function DefaultAPI : TGoogleAPIClass; virtual;
Property API : TGoogleAPI Read FAPI Write SetAPI;
end;
{ TGoogleAPIFactory }
TGoogleAPIFactory = Class(TComponent)
Private
FAPIs : TClassList;
FResources : TClassList;
function GetA(AIndex : Integer): TGoogleAPIClass;
function GetACount: Integer;
function GetR(AIndex : Integer): TGoogleResourceClass;
function GetRCount: Integer;
Public
Class var
DefaultFactoryClass : TGoogleAPIFactoryClass;
DefaultFactory : TGoogleAPIFactory;
Public
Constructor Create(Aowner : TComponent); override;
Destructor Destroy; override;
// Resource methods
Procedure RegisterResource(Resource : TGoogleResourceClass); virtual;
Function IndexOfResource(Const Resource : String) : Integer;
Function FindResourceClass(Const Resource : String) : TGoogleResourceClass; virtual;
Function GetResourceClass(Const Resource : String) : TGoogleResourceClass;
Property ResourceClass[AIndex : Integer] : TGoogleResourceClass Read GetR;
Property ResourceCount : Integer Read GetRCount;
// API methods
Procedure RegisterAPI(AAPI : TGoogleAPIClass); virtual;
Function IndexOfAPI(Const API : String) : Integer;
Function FindAPIClass(Const API : String) : TGoogleAPIClass; virtual;
Function GetAPIClass(Const API : String) : TGoogleAPIClass;
Property APIClass[AIndex : Integer] :TGoogleAPIClass Read GetA;
Property APICount : Integer Read GetACount;
end;
Function APIFactory : TGoogleAPIFactory;
implementation
uses httpdefs;
Function APIFactory : TGoogleAPIFactory;
Var
AClass : TGoogleAPIFactoryClass;
begin
If TGoogleAPIFactory.DefaultFactory=Nil then
begin
AClass:=TGoogleAPIFactory.DefaultFactoryClass;
If AClass=Nil then
AClass:=TGoogleAPIFactory;
TGoogleAPIFactory.DefaultFactory:=AClass.Create(Nil);
end;
Result:=TGoogleAPIFactory.DefaultFactory;
end;
{ TGoogleAPIFactory }
function TGoogleAPIFactory.GetR(AIndex : Integer): TGoogleResourceClass;
begin
Result:=TGoogleResourceClass(FResources[Aindex]);
end;
function TGoogleAPIFactory.GetA(AIndex : Integer): TGoogleAPIClass;
begin
Result:=TGoogleAPIClass(FAPIs[AIndex])
end;
function TGoogleAPIFactory.GetACount: Integer;
begin
Result:=FAPIS.Count;
end;
function TGoogleAPIFactory.GetRCount: Integer;
begin
Result:=FResources.Count;
end;
Constructor TGoogleAPIFactory.Create(Aowner: TComponent);
begin
inherited Create(Aowner);
FAPIs:=TClassList.Create;
FResources:=TClassList.Create;
end;
Destructor TGoogleAPIFactory.Destroy;
begin
FreeAndNil(FAPIs);
FreeAndNil(FResources);
inherited Destroy;
end;
Procedure TGoogleAPIFactory.RegisterAPI(AAPI: TGoogleAPIClass);
begin
FAPIs.Add(AAPI);
end;
Function TGoogleAPIFactory.IndexOfAPI(Const API: String): Integer;
begin
Result:=FAPIs.Count-1;
While (Result>=0) and (CompareText(TGoogleAPIClass(FAPIs[Result]).APIName,API)<>0) do
Dec(Result);
end;
Function TGoogleAPIFactory.FindAPIClass(Const API: String): TGoogleAPIClass;
Var
I : Integer;
begin
I:=IndexOfAPI(API);
if I=-1 then
Result:=Nil
else
Result:=GetA(I);
end;
Function TGoogleAPIFactory.GetAPIClass(Const API: String): TGoogleAPIClass;
begin
Result:=FindAPIClass(API);
if Result=Nil then
Raise EGoogleAPI.CreateFmt('Unknown API : "%s"',[API]);
end;
Procedure TGoogleAPIFactory.RegisterResource(Resource: TGoogleResourceClass);
begin
FResourceS.Add(Resource);
end;
Function TGoogleAPIFactory.IndexOfResource(Const Resource: String): Integer;
begin
Result:=FResources.Count-1;
While (Result>=0) and (CompareText(TGoogleResourceClass(FResources[Result]).ResourceName,Resource)<>0) do
Dec(Result);
end;
Function TGoogleAPIFactory.FindResourceClass(Const Resource: String): TGoogleResourceClass;
Var
I : Integer;
begin
I:=IndexOfResource(Resource);
if I=-1 then
Result:=Nil
else
Result:=GetR(I);
end;
Function TGoogleAPIFactory.GetResourceClass(Const Resource: String): TGoogleResourceClass;
begin
Result:=FindResourceClass(Resource);
if Result=Nil then
Raise EGoogleAPI.CreateFmt('Unknown resource : "%s"',[Resource]);
end;
{ TGoogleResource }
Procedure TGoogleResource.CheckAPI;
begin
If (API=nil) then
Raise EGoogleAPI.Create('Cannot perform this method, API is not assigned');
end;
Procedure TGoogleResource.Notification(AComponent: TComponent; Operation: TOperation
);
begin
inherited Notification(AComponent, Operation);
If Operation=opRemove then
if FAPI=AComponent then
FAPI:=Nil;
end;
Procedure TGoogleResource.SetAPI(AAPI: TGoogleAPI);
begin
If Assigned(FAPI) then
FAPI.RemoveFreeNotification(Self);
FAPI:=AAPI;
If Assigned(FAPI) then
FAPI.FreeNotification(Self);
end;
Class Procedure TGoogleResource.AddToQuery(Var Q: String; Const AName,AValue: String);
begin
If AValue='' then
exit;
if (Q<>'') then
Q:=Q+'&';
Q:=Q+Aname+'='+HTTPEncode(AValue);
end;
Class Procedure TGoogleResource.AddToQuery(Var Q: String; Const AName : String; AValue: Int64);
begin
if AValue=0 then exit;
if (Q<>'') then
Q:=Q+'&';
Q:=Q+Aname+'='+IntToStr(AValue);
end;
Class Procedure TGoogleResource.AddToQuery(Var Q: String; Const AName : String; AValue: TDateTime);
begin
if AValue=0 then exit;
if (Q<>'') then
Q:=Q+'&';
Q:=Q+Aname+'='+DateTimeToRFC3339(AValue);
end;
Class Procedure TGoogleResource.AddToQuery(Var Q: String; Const AName : String; AValue: Boolean);
begin
if (Q<>'') then
Q:=Q+'&';
Q:=Q+Aname+'='+BoolToStr(AValue,'true','false');
end;
Function TGoogleResource.SubstitutePath(Const APath: String;
Const Args: Array of const): String;
begin
CheckAPI;
Result:=API.SubstitutePath(ResourceName,APath,Args);
end;
Function TGoogleResource.ServiceCall(Const AMethod, APath, AQuery: String;
AInput: TGoogleBaseObject; AReturnClass: TGoogleBaseObjectClass): TGoogleBaseObject;
begin
CheckAPI;
Result:=API.ServiceCall(Self,AMethod,APath,AQuery,AInput,AReturnClass);
end;
Class Function TGoogleResource.ResourceName: String;
begin
Result:=ClassName;
if UpCase(Result[1])='T' then
Delete(Result,1,1);
If CompareText(Copy(Result,Length(Result)-7,8),'Resource')=0 then
Result:=Copy(Result,1,Length(Result)-8);
end;
Class Function TGoogleResource.DefaultAPI: TGoogleAPIClass;
begin
Result:=Nil;
end;
{ TGoogleAPI }
Class Function TGoogleAPI.APIName: String;
begin
Result:=ClassName;
if UpCase(Result[1])='T' then
Delete(Result,1,1);
If CompareText(Copy(Result,Length(Result)-6,7),'API')=0 then
Result:=Copy(Result,1,Length(Result)-7);
end;
Class Function TGoogleAPI.APINeedsAuth: Boolean;
begin
Result:=Length(APIAuthScopes)<>0;
end;
procedure TGoogleAPI.SetGoogleClient(AValue: TGoogleClient);
begin
if FGoogleClient=AValue then Exit;
If Assigned(FGoogleClient) then
FGoogleClient.RemoveFreeNotification(Self);
FGoogleClient:=AValue;
If Assigned(FGoogleClient) then
FGoogleClient.FreeNotification(Self);
end;
Procedure TGoogleAPI.Notification(AComponent: TComponent; Operation: TOperation
);
begin
inherited Notification(AComponent, Operation);
if (Operation=opRemove) and (AComponent=FGoogleClient) then
FGoogleClient:=Nil;
end;
Function TGoogleAPI.API: TGoogleAPI;
begin
Result:=Self;
end;
Class Function TGoogleAPI.APIInfo: TAPIInfo;
begin
Result.Name:=APIName;
Result.Version:=APIVersion;
Result.Revision:=APIRevision;
Result.ID:=APIID;
Result.Title:=APITitle;
Result.Description:=APIDescription;
Result.OwnerDomain:=APIOwnerDomain;
Result.OwnerName:=APIOwnerName;
Result.Icon16:=APIIcon16;
Result.Icon32:=APIIcon32;
Result.documentationLink:=APIdocumentationLink;
Result.rootUrl:=APIrootUrl;
Result.basePath:=APIbasePath;
Result.baseURL:=APIbaseURL;
Result.Protocol:=APIProtocol;
Result.servicePath:=APIservicePath;
Result.batchPath:=APIbatchPath;
Result.AuthScopes:=APIAuthScopes;
end;
Class Procedure TGoogleAPI.RegisterAPI;
begin
APIFactory.RegisterAPI(Self);
end;
Class Procedure TGoogleAPI.RegisterAPIResources;
begin
// needs to be implemented in descendents
end;
Function TGoogleAPI.ServiceCall(Const AResource: TGoogleResource; AMethod,
APath, AQuery: String; AInput: TGoogleBaseObject;
AReturnClass: TGoogleBaseObjectClass): TGoogleBaseObject;
Var
D : TJSONData;
R,S : String;
C : TGoogleBaseObjectClass;
BC : TBaseObjectClass;
begin
Result:=Nil;
if Assigned(AInput) then
begin
D:=TJSONObject.Create;
AInput.SaveToJSON(TJSONObject(D));
try
S:=D.AsJSON;
finally
D.Free;
end;
end
else
S:='';
R:=ServiceCall(AResource,AMethod,APAth,AQuery,S);
if (R<>'') then
begin
D:=GetJSON(R);
try
C:=Nil;
if Assigned(D) and (D.JSONType=jtObject) then
begin
S:=TJSONObject(D).Get('kind','');
if (S<>'') then
begin
BC:=GoogleFactory.GetObjectClass(s);
If BC.InheritsFrom(TGoogleBaseObject) then
C:=TGoogleBaseObjectClass(BC)
else
C:=Nil;
end;
end;
if C=Nil then
C:=AReturnClass;
Result:=C.Create;
try
Result.LoadFromJSON(D as TJSONObject);
except
FreeAndNil(Result);
Raise;
end;
finally
D.Free;
end;
end;
end;
Function TGoogleAPI.ServiceCall(Const AResource: TGoogleResource; AMethod,
APath, AQuery, AInput: String): String;
Var
URL : String;
Req : TWebClientRequest;
Resp : TWebClientResponse;
begin
URL:=APIBaseURL+APath;
if AQuery<>'' then
URL:=URL+'?'+AQuery;
Result:='';
Req:=Nil;
Resp:=Nil;
try
Req:=googleclient.WebClient.CreateRequest;
if (AInput<>'') then
begin
Req.Headers.Values['Content-type']:='application/json';
Req.SetContentFromString(AInput);
end;
If Not APINeedsAuth then
Resp:=googleclient.WebClient.ExecuteRequest(AMethod,URL,Req)
else
Resp:=googleclient.WebClient.ExecuteSignedRequest(AMethod,URL,Req);
If (Resp.StatusCode div 100)<>2 then
Raise EGoogleAPI.CreateFmt('%d error executing request : %s',[Resp.StatusCode,Resp.StatusText]);
Result:=Resp.GetContentAsString;
finally
Req.Free;
Resp.Free;
end;
end;
Function TGoogleAPI.SubstitutePath(Const AResource, APath: String;
Const Args: Array of const): String;
Var
N,V : String;
I : Integer;
begin
Result:=APath;
I:=0;
While I<High(Args) do
begin
if Args[i].VType<>vtAnsiString then
Raise EGoogleAPI.CreateFmt('Expected name argument at position %d',[i]);
N:=ansistring(Args[i].VAnsiString);
Inc(I);
With Args[i] do
Case VType of
vtInteger : V:=IntToStr(VInteger);
vtBoolean : V:=BoolToStr(VBoolean,'true','false');
vtChar : V:=VChar;
{$ifndef FPUNONE}
vtExtended : system.Str(VExtended^,V);
{$endif}
vtString : V:=VString^;
vtPChar : V:=VPChar;
vtWideChar : V:=VWideChar;
vtPWideChar : V:=VPWideChar;
vtAnsiString : V:=ansistring(VAnsiString);
vtCurrency : Str(VCurrency^,V);
vtVariant : V:=VVariant^;
vtWideString : V:=Widestring(VWideString);
vtInt64 : V:=IntToStr(vInt64^);
vtQWord : V:=IntToStr(vQWord^);
vtUnicodeString : V:=UnicodeString(VUnicodeString);
end;
Inc(i);
Result:=StringReplace(Result,'{'+N+'}',V,[]);
end;
end;
Function TGoogleAPI.CreateResource(AClass: TGoogleResourceClass
): TGoogleResource;
begin
Result:=AClass.Create(Self);
Result.SetAPI(Self);
end;
Function TGoogleAPI.CreateResource(const Resource: String): TGoogleResource;
begin
Result:=CreateResource(APIFactory.GetResourceClass(Resource));
end;
finalization
FreeAndNil(TGoogleAPIFactory.DefaultFactory);
end.
|