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
|
unit frmmain;
{$mode objfpc}{$H+}
// Define this if you want to use synapse.
{ $DEFINE USESYNAPSE}
// For 2.6.4, synapse is currently the only option.
// You will need to add lazsynapsewebclient to the requires list.
{$IFDEF VER2_6}
{$DEFINE USESYNAPSE}
{$ENDIF}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ComCtrls, ActnList, Menus, ListViewFilterEdit, restbase, googleclient,
googlediscovery, frmgenoptions, frmview;
type
{ TMainForm }
TMainForm = class(TForm)
APreViewRest: TAction;
AGenCode: TAction;
AViewHelp: TAction;
ASaveREST: TAction;
APreferredOnly: TAction;
AQuit: TAction;
AFetch: TAction;
ActionList1: TActionList;
EFilter: TEdit;
ILDiscovery: TImageList;
Label1: TLabel;
LVServices: TListView;
MainMenu1: TMainMenu;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
MAPI: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
MenuItem5: TMenuItem;
MenuItem6: TMenuItem;
MIQuit: TMenuItem;
MIPreferredOnly: TMenuItem;
MServices: TMenuItem;
SDJSON: TSaveDialog;
SBDiscovery: TStatusBar;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton5: TToolButton;
ToolButton6: TToolButton;
procedure APreViewRestExecute(Sender: TObject);
procedure APreViewRestUpdate(Sender: TObject);
procedure ASaveRESTExecute(Sender: TObject);
procedure ASaveRESTUpdate(Sender: TObject);
procedure AGenCodeExecute(Sender: TObject);
procedure AGenCodeUpdate(Sender: TObject);
procedure AQuitExecute(Sender: TObject);
procedure AViewHelpExecute(Sender: TObject);
procedure AViewHelpUpdate(Sender: TObject);
procedure BFetchClick(Sender: TObject);
procedure CBPreferredOnlyChange(Sender: TObject);
procedure EFilterChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
FClient : TGoogleClient;
FDiscoveryAPI : TDiscoveryAPI;
FDirectory : TDirectoryList;
Function CurrentAPI : TDirectoryListTypeitemsItem;
procedure DoFetch;
procedure DownLoadRestAPI(Const AName,AURL: String);
procedure GenerateCode(const AName, AURL: String);
function HttpGetBinary(AURL: String; S: TStream): Boolean;
procedure ShowDiscovery(PreferredOnly: Boolean; FilterOn: String);
procedure UpdateCaption;
procedure ViewFile(AFileName: String);
procedure ViewFile(AStream: TStream; ASyntax: TSyntax; ACaption: String);
procedure ViewRestAPI(const AName, AURL: String);
public
{ public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.lfm}
uses
ssl_openssl,
jsonparser, // needed
fpoauth2, lclintf,
{$IFDEF USESYNAPSE}
synapsewebclient,
httpsend,
{$ELSE}
fphttpclient,
fphttpwebclient,
{$ENDIF}
googlediscoverytopas;
{ TMainForm }
procedure TMainForm.FormCreate(Sender: TObject);
begin
// set up communication.
FClient:=TGoogleClient.Create(Self);
{$IFDEF USESYNAPSE}
FClient.WebClient:=TSynapseWebClient.Create(Self);
{$ELSE}
FClient.WebClient:=TFPHTTPWebClient.Create(Self);
{$ENDIF}
// Register all classes so they can be streamed.
TDiscoveryAPI.RegisterAPIResources;
// create the API and hook it up to the google client.
FDiscoveryAPI:=TDiscoveryAPI.Create(Self);
FDiscoveryAPI.GoogleClient:=FClient;
// The code generator uses it's own objects.
TDiscoveryJSONToPas.RegisterAllObjects;
UpdateCaption;
end;
procedure TMainForm.BFetchClick(Sender: TObject);
begin
DoFetch;
end;
procedure TMainForm.AQuitExecute(Sender: TObject);
begin
Close;
end;
procedure TMainForm.AViewHelpExecute(Sender: TObject);
begin
OpenURL(CurrentAPI.DocumentationLink);
end;
procedure TMainForm.AViewHelpUpdate(Sender: TObject);
begin
(Sender as TAction).Enabled:=Assigned(CurrentAPI) and (CurrentAPI.documentationLink<>'');
end;
procedure TMainForm.ASaveRESTUpdate(Sender: TObject);
begin
(Sender as TAction).Enabled:=Assigned(CurrentAPI) and (CurrentAPI.discoveryRestUrl<>'');
end;
procedure TMainForm.AGenCodeExecute(Sender: TObject);
Var
DLI : TDirectoryListTypeitemsItem;
begin
DLI:=CurrentAPI;
GenerateCode(DLI.Name,DLI.DiscoveryRestUrl);
end;
procedure TMainForm.AGenCodeUpdate(Sender: TObject);
begin
(Sender as TAction).Enabled:=Assigned(CurrentAPI) and (CurrentAPI.discoveryRestUrl<>'')
end;
procedure TMainForm.ViewFile(AFileName : String);
begin
With TViewForm.Create(Nil) do
begin
Caption:='Viewing file: '+AFileName;
FileName:=AFileName;
Show;
end;
end;
procedure TMainForm.ViewFile(AStream : TStream; ASyntax : TSyntax; ACaption : String);
begin
With TViewForm.Create(Nil) do
begin
AStream.POsition:=0;
Caption:='Viewing: '+ACaption;
Stream:=AStream;
Syntax:=ASyntax;
FreeStream:=True;
Show;
end;
end;
Function TMainForm.HttpGetBinary(AURL : String; S : TStream) : Boolean;
begin
{$IFDEF USESYNAPSE}
Result:=httpsend.HttpGetBinary(AURL,S);
{$ELSE}
try
TFPHTTPClient.SimpleGet(AURL,S);
S.Position:=0;
Result:=True;
except
Result:=False;
end;
{$ENDIF}
end;
procedure TMainForm.GenerateCode(const AName, AURL: String);
Var
S : TMemoryStream;
FO : TGenCodeFormOptions;
DP : TDiscoveryJSONToPas;
begin
FO:=Nil;
DP:=Nil;
S:=TMemoryStream.Create;
try
if HttpGetBinary(AURL,S) then
begin
S.Position:=0;
FO:=TGenCodeFormOptions.Create(Self);
FO.UnitName:=AName;
If FO.ShowModal=mrOK then
begin
DP:=TDiscoveryJSONToPas.Create(Self);
DP.LoadFromStream(S);
DP.BaseClassName:=FO.BaseClass;
DP.OutputUnitName:=FO.UnitName;
DP.ExtraUnits:=FO.ExtraUnits;
DP.ClassPrefix:=FO.Prefix;
DP.SaveToFile(FO.FileName);
if FO.DoPreview then
ViewFile(FO.FileName);
end;
end;
Finally
FO.Free;
DP.Free;
S.Free;
end;
end;
procedure TMainForm.DownLoadRestAPI(const AName, AURL: String);
Var
S : TMemoryStream;
begin
S:=TMemoryStream.Create;
try
if HttpGetBinary(AURL,S) then
begin
SDJSON.FileName:='google'+aname+'.json';
If SDJSON.Execute then
With TFileStream.Create(SDJSON.FileName,fmCreate) do
try
CopyFrom(S,0);
finally
Free;
end;
end;
finally
S.Free;
end;
end;
procedure TMainForm.ViewRestAPI(const AName, AURL: String);
Var
S : TMemoryStream;
begin
S:=TMemoryStream.Create;
try
if HttpGetBinary(AURL,S) then
begin
ViewFile(S,sJSON,'REST discovery for '+AName);
S:=Nil;
end;
finally
S.Free;
end;
end;
procedure TMainForm.ASaveRESTExecute(Sender: TObject);
Var
DLI : TDirectoryListTypeitemsItem;
begin
DLI:=CurrentAPI;
DownLoadRestAPI(DLI.Name,DLI.DiscoveryRestUrl);
end;
procedure TMainForm.APreViewRestUpdate(Sender: TObject);
begin
(Sender as TAction).Enabled:=Assigned(CurrentAPI) and (CurrentAPI.discoveryRestUrl<>'');
end;
procedure TMainForm.APreViewRestExecute(Sender: TObject);
Var
DLI : TDirectoryListTypeitemsItem;
begin
DLI:=CurrentAPI;
ViewRestAPI(DLI.Name,DLI.DiscoveryRestUrl);
end;
procedure TMainForm.CBPreferredOnlyChange(Sender: TObject);
begin
if (LVServices.Items.Count>0) then
ShowDiscovery(MIPreferredOnly.Checked,EFilter.Text);
end;
procedure TMainForm.EFilterChange(Sender: TObject);
begin
if (LVServices.Items.Count>0) then
ShowDiscovery(MIPreferredOnly.Checked,EFilter.Text);
end;
procedure TMainForm.UpdateCaption;
Var
C : Integer;
begin
C:=LVServices.Items.Count;
if (C=0) then
Caption:='Google Discovery Service Demo'
else
Caption:=Format('Google Discovery Service Demo (%d services)',[C]);
SBDiscovery.Panels[0].Text:=Format('%d items',[C]);
end;
function TMainForm.CurrentAPI: TDirectoryListTypeitemsItem;
begin
If Assigned(LVServices.Selected) and Assigned(LVServices.Selected.Data) then
Result:=TDirectoryListTypeitemsItem(LVServices.Selected.Data)
else
Result:=Nil;
end;
procedure TMainForm.ShowDiscovery(PreferredOnly : Boolean; FilterOn : String);
Function DoComma(S : TStringArray) : String;
Var
I : String;
begin
Result:='';
For I in S do
begin
if Result<>'' then
Result:=Result+',';
Result:=Result+I;
end;
end;
Function Contains(S: String) : Boolean; inline;
begin
Result:=Pos(FilterOn,LowerCase(S))<>0;
end;
Function ShowItem (DLI : TDirectoryListTypeitemsItem) : Boolean;
begin
Result:=DLI.Preferred or (Not PreferredOnly);
if Result and (FilterOn<>'') then
begin
Result:=Contains(DLI.Name)
or Contains(DLI.Title)
or Contains(DLI.kind)
or Contains(DLI.description)
or Contains(DoComma(DLI.Labels));
end;
end;
Var
DLI : TDirectoryListTypeitemsItem;
LI : TListItem;
begin
FilterOn:=LowerCase(Filteron);
LVServices.Items.BeginUpdate;
try
LVServices.Items.Clear;
LVServices.Column[1].Visible:=Not PreferredOnly;
For DLI in FDirectory.Items do
if ShowItem(DLI) then
begin
LI:=LVServices.Items.Add;
LI.Caption:=DLI.name;
LI.Data:=DLI;
With LI.SubItems,DLI do
begin
Add(BoolToStr(preferred,'True','False'));
Add(id);
Add(title);
Add(version);
Add(description);
Add(discoveryLink);
Add(discoveryRestUrl);
Add(documentationLink);
Add(icons.x16);
Add(icons.x32);
Add(DoComma(labels));
end;
end;
UpdateCaption;
finally
LVServices.Items.EndUpdate;
end;
end;
procedure TMainForm.DoFetch;
begin
// Free any previous list.
FreeAndNil(FDirectory);
// Get the new list using a default ApisResource.
FDirectory:=FDiscoveryAPI.ApisResource.List();
ShowDiscovery(MIPreferredOnly.Checked,EFilter.Text);
end;
end.
|