testv4.pp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. program testv4;
  2. uses classes, sysutils, jsonparser, odatabase, fpjson, fphttpwebclient, custapp, v4sample;
  3. Type
  4. { TODataV4SampleServiceClientApp }
  5. TODataV4SampleServiceClientApp = Class(TCustomApplication)
  6. Private
  7. FShowExtra : Boolean;
  8. FDoDelete : Boolean;
  9. FSavePhoto : Boolean;
  10. FService:TService;
  11. Protected
  12. Procedure RunDemo;
  13. // Unbound action
  14. Procedure DoResetDataSource;
  15. // Entityset
  16. Procedure DoDumpAirLines;
  17. // Contained entity, GetStream
  18. Procedure DoPhoto(APerson: TPerson; DoSave: Boolean);
  19. // Contained entityset
  20. Procedure DoListFriends(P: TPerson; DeleteLast: Boolean);
  21. // Delete
  22. Procedure DoDeletePerson(P: TPerson);
  23. // Bound Function
  24. Procedure ShowFavoriteAirline(P: TPerson);
  25. // Bound Function
  26. Procedure ShowFriendsTrips(P: TPerson);
  27. // Unbound Function
  28. Procedure ShowNearestAirPort(ALat, ALon: Integer);
  29. Public
  30. Constructor Create(AOwner :TComponent); override;
  31. Destructor Destroy; override;
  32. procedure DoServiceLog(Sender: TObject; const Msg: String);
  33. Procedure DumpExtra(A : TODataObject);
  34. Procedure DoRun; override;
  35. Procedure Usage(Const Msg : String);
  36. end;
  37. Constructor TODataV4SampleServiceClientApp.Create(AOwner: TComponent);
  38. begin
  39. inherited Create(AOwner);
  40. FService:=TService.Create(Self);
  41. FService.WebClient:=TFPHTTPWebClient.Create(Self);
  42. StopOnException:=True;
  43. end;
  44. Destructor TODataV4SampleServiceClientApp.Destroy;
  45. begin
  46. FreeAndNil(FService);
  47. inherited Destroy;
  48. end;
  49. procedure TODataV4SampleServiceClientApp.DoServiceLog(Sender: TObject;
  50. const Msg: String);
  51. begin
  52. Writeln(StdErr,'Service log: ',Msg);
  53. end;
  54. Procedure TODataV4SampleServiceClientApp.DumpExtra(A : TODataObject);
  55. Var
  56. I : Integer;
  57. Function PJ(J : TJSONData) : String;
  58. begin
  59. if J.JSONType in [jtArray,jtObject] then
  60. Result:=J.FormatJSON
  61. else
  62. Result:=J.AsString;
  63. end;
  64. begin
  65. if not FShowExtra then
  66. exit;
  67. if Assigned(A.additionalProperties) and (A.additionalProperties.Count>0) then
  68. begin
  69. Writeln(' Additional properties : ');
  70. Writeln(' '+PJ(A.additionalProperties));
  71. end;
  72. if (A.ODataAnnotationCount>0) then
  73. begin
  74. Writeln(' Annotations:');
  75. For I:=0 to A.ODataAnnotationCount-1 do
  76. Writeln(' '+A.ODataAnnotations[i].Key,' : ',PJ(A.ODataAnnotations[i].Value));
  77. end;
  78. end;
  79. Procedure TODataV4SampleServiceClientApp.DoResetDataSource;
  80. begin
  81. Writeln('Resetting data source');
  82. FService.DefaultContainer.ResetDataSource;
  83. end;
  84. Procedure TODataV4SampleServiceClientApp.DoDumpAirLines;
  85. Var
  86. A : TAirline;
  87. AA : TAirlineArray;
  88. I : Integer;
  89. begin
  90. AA:=FService.DefaultContainer.Airlines.ListAll('');
  91. try
  92. Writeln('Number of arlines: ',Length(AA));
  93. For I:=0 to Length(AA)-1 do
  94. begin
  95. A:=AA[i];
  96. // Writeln('Base URL : ',A.BaseURL(FService));
  97. Writeln('Airline ',I+1,' code : ',A.AirlineCode);
  98. Writeln('Airline ',I+1,' name : ',A.Name);
  99. DumpExtra(A);
  100. end;
  101. finally
  102. For I:=0 to Length(AA)-1 do
  103. FreeAndNil(AA[i]);
  104. end;
  105. end;
  106. Procedure TODataV4SampleServiceClientApp.DoDeletePerson(P : TPerson);
  107. begin
  108. Writeln('Attempting to delete person:');
  109. try
  110. Writeln(P.Delete(FService));
  111. except
  112. On EO : EOData do
  113. begin
  114. Writeln('OData error : ',EO.Message);
  115. Writeln('Status code : ',EO.StatusCode,', text : ',EO.StatusText);
  116. If Assigned(EO.Error) then
  117. begin
  118. Writeln('OData error code : ',EO.Error.Code,', message : ',EO.Error.Message);
  119. end;
  120. end;
  121. On E : Exception do
  122. begin
  123. Writeln('General Error : ',E.Message)
  124. end;
  125. end;
  126. end;
  127. Procedure TODataV4SampleServiceClientApp.DoListFriends(P : TPerson; DeleteLast : Boolean);
  128. Var
  129. FES : TPeopleEntitySet;
  130. PA : TPersonArray;
  131. FL : TPerson;
  132. I : integer;
  133. F : TPerson;
  134. begin
  135. FES:=P.Friends(FService);
  136. try
  137. PA:=FES.ListAll('');
  138. I:=0;
  139. for F in PA do
  140. begin
  141. Inc(i);
  142. Writeln('Friend ',I,': FirstName: ',F.FirstName,', LastName: ',F.LastName);
  143. DumpExtra(F);
  144. FL:=F;
  145. end;
  146. If DeleteLast and (FL<>Nil) then
  147. DoDeletePerson(FL);
  148. finally
  149. For I:=0 to Length(PA)-1 do
  150. FreeAndNil(PA[i]);
  151. end;
  152. end;
  153. Procedure TODataV4SampleServiceClientApp.DoPhoto(APerson : TPerson; DoSave: Boolean);
  154. Var
  155. P : TPHoto;
  156. PF : TFileStream;
  157. begin
  158. PF:=Nil;
  159. P:=APerson.Photo(FService);
  160. try
  161. Writeln('Photo ID : ',P.Id,', name : ', P.Name);
  162. DumpExtra(p);
  163. if DoSave then
  164. begin
  165. PF:=TFileStream.Create('photo.jpg',fmCreate);
  166. P.GetStream(FService,'image/jpeg',PF);
  167. Writeln('Saved profile photo to photo.jpg');
  168. end;
  169. finally
  170. P.Free;
  171. PF.Free;
  172. end;
  173. end;
  174. Procedure TODataV4SampleServiceClientApp.ShowFavoriteAirline(P : TPerson);
  175. Var
  176. A : TAirLine;
  177. begin
  178. A:=P.GetFavoriteAirline(FService);
  179. try
  180. Writeln('Favorite Airline:');
  181. Writeln('Code: ',A.AirlineCode);
  182. Writeln('Name: ',A.Name);
  183. DumpExtra(A);
  184. finally
  185. A.Free;
  186. end;
  187. end;
  188. Procedure TODataV4SampleServiceClientApp.ShowFriendsTrips(P : TPerson);
  189. Var
  190. TA : TTripArray;
  191. I : Integer;
  192. begin
  193. TA:=P.GetFriendsTrips(FService,'russellwhyte');
  194. try
  195. For I:=0 to Length(TA)-1 do
  196. Writeln('Trip [',i,'] : ',TA[i].Name);
  197. finally
  198. For I:=0 to Length(TA)-1 do
  199. FreeAndNil(TA[i]);
  200. end;
  201. end;
  202. Procedure TODataV4SampleServiceClientApp.ShowNearestAirPort(ALat,ALon : Integer);
  203. Var
  204. AP : TAirPort;
  205. begin
  206. Writeln('Nearest airport for (',alat,',',alon,') : ');
  207. AP:=FService.DefaultContainer.GetNearestAirPort(Alat,ALon);
  208. try
  209. Writeln('Name : ',AP.Name);
  210. Writeln('IATA code : ',AP.IataCode);
  211. Writeln('ICAO code : ',AP.IcaoCode);
  212. if Assigned(AP.Location) then
  213. begin
  214. Writeln('Address : ',AP.Location.Address);
  215. Writeln('City : ',AP.Location.City.Name,' (Country: ',AP.Location.City.CountryRegion,', Region: ',AP.Location.City.Region,')');
  216. if Assigned(AP.Location.Loc) then
  217. With AP.Location.Loc do
  218. Writeln('Location : ',Coordinates[0],',',Coordinates[1]);
  219. end;
  220. finally
  221. AP.Free;
  222. end;
  223. end;
  224. Procedure TODataV4SampleServiceClientApp.DoRun;
  225. Var
  226. S : string;
  227. begin
  228. S:=CheckOptions('hdepl::u:D',['help','log::','delete','extra','photo','url:','debug']);
  229. if (S<>'') or HasOption('h','help') then
  230. Usage(S);
  231. FShowExtra:=HasOption('e','extra');
  232. FDoDelete:=HasOption('d','delete');
  233. FSavePhoto:=HasOption('p','photo');
  234. if HasOption('l','log') then
  235. begin
  236. S:=GetOptionValue('l','log');
  237. if S='' then
  238. S:='requests.log';
  239. FService.WebClient.LogFile:=S;
  240. end;
  241. S:=GetOptionValue('u','url');
  242. if S='' then
  243. S:='http://services.odata.org/V4/TripPinServiceRW/';
  244. FService.ServiceURL:=S;
  245. if HasOption('D','debug') then
  246. FService.OnLog:=@DoServiceLog;
  247. RunDemo;
  248. Terminate;
  249. end;
  250. Procedure TODataV4SampleServiceClientApp.RunDemo;
  251. Var
  252. Me : TPerson;
  253. begin
  254. Me:=Nil;
  255. try
  256. DoResetDataSource;
  257. DoDumpAirLines;
  258. Me:=FService.DefaultContainer.Me;
  259. Writeln('Me.FirstName: ',Me.FirstName);
  260. Writeln('Me.LastName: ',Me.LastName);
  261. DumpExtra(Me);
  262. DoListFriends(Me,FDoDelete);
  263. DoPhoto(Me,FSavePhoto);
  264. ShowFavoriteAirLine(Me);
  265. ShowFriendsTrips(Me);
  266. ShowNearestAirPort(40,45);
  267. finally
  268. FreeAndNil(Me);
  269. end;
  270. end;
  271. Procedure TODataV4SampleServiceClientApp.Usage(Const Msg: String);
  272. begin
  273. Writeln('Error : ',Msg);
  274. Writeln('Usage : ',ExeName,' [options]');
  275. Writeln('Where options is one or more of:');
  276. Writeln('-h --help This help');
  277. Writeln('-d --delete Execute delete call on friend');
  278. Writeln('-e --extra Show extra OData information');
  279. Writeln('-l --log[=file] Dump requests and return to file (default is requests.log)');
  280. Writeln('-p --photo Save pictore to photo.jpg');
  281. Writeln('-u --url=URL Set Service url.');
  282. Writeln('-D --debug Debug output');
  283. Halt(Ord(Msg<>''));
  284. end;
  285. begin
  286. With TODataV4SampleServiceClientApp.Create(Nil) do
  287. try
  288. Initialize;
  289. Run;
  290. finally
  291. Free;
  292. end;
  293. end.