apiclient.lpr 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. program apiclient;
  2. {$mode objfpc}
  3. uses
  4. browserapp, JS, Classes, SysUtils, Web, fpjson, fpjsonjs, fprpccodegen;
  5. type
  6. { TMyApplication }
  7. TMyApplication = class(TBrowserApplication)
  8. edtResult : TJSHTMLTextAreaElement;
  9. edtURL : TJSHTMLInputElement;
  10. edtUnit : TJSHTMLInputElement;
  11. cbPreferNativeInt : TJSHTMLInputElement;
  12. cbForceJSValueResult : TJSHTMLInputElement;
  13. btnGenerate : TJSHTMLButtonElement;
  14. procedure BindElements;
  15. procedure doRun; override;
  16. private
  17. function DoGenerateCode(aEvent: TJSMouseEvent): boolean;
  18. procedure GenerateAPI(const aJSON: String);
  19. end;
  20. procedure TMyApplication.BindElements;
  21. begin
  22. edtResult:=TJSHTMLTextAreaElement(GetHTMLElement('edtResult'));
  23. edtURL:=TJSHTMLInputElement(GetHTMLElement('edtURL'));
  24. edtUnit:=TJSHTMLInputElement(GetHTMLElement('edtUnit'));
  25. cbPreferNativeInt:=TJSHTMLInputElement(GetHTMLElement('cbPreferNativeInt'));
  26. cbForceJSValueResult:=TJSHTMLInputElement(GetHTMLElement('cbForceJSValueResult'));
  27. btnGenerate:=TJSHTMLButtonElement(GetHTMLElement('btnGenerate'));
  28. btnGenerate.OnClick:=@DoGenerateCode;
  29. end;
  30. procedure TMyApplication.doRun;
  31. begin
  32. BindElements;
  33. Terminate;
  34. end;
  35. Procedure TMyApplication.GenerateAPI(const aJSON: String);
  36. Var
  37. API : TJSONObject;
  38. Gen : TAPIClientCodeGen;
  39. Opts : TClientCodeOptions;
  40. begin
  41. API:=GetJSON(aJSON) as TJSONObject;
  42. Opts:=[];
  43. if cbForceJSValueResult.checked then
  44. Include(Opts,ccoForceJSValueResult);
  45. if cbPreferNativeInt.Checked then
  46. Include(Opts,ccoPreferNativeInt);
  47. Gen:=TAPIClientCodeGen.Create(Self);
  48. try
  49. Gen.API:=API;
  50. Gen.Options:=Opts;
  51. Gen.OutputUnitName:=edtUnit.Value;
  52. Gen.Execute;
  53. edtResult.value:=Gen.Source.Text;
  54. finally
  55. Gen.Free;
  56. end;
  57. end;
  58. function TMyApplication.DoGenerateCode(aEvent: TJSMouseEvent): boolean;
  59. procedure GenAPI(Resp : TJSResponse); async;
  60. begin
  61. GenerateAPI(Await(Resp.text()));
  62. end;
  63. function DoOK(aValue: JSValue): JSValue;
  64. var
  65. Resp : TJSResponse absolute aValue;
  66. begin
  67. Result:=undefined;
  68. GenAPI(Resp)
  69. end;
  70. function DoFail(aValue: JSValue): JSValue;
  71. begin
  72. Result:=undefined;
  73. window.alert('Failed to fetch API description at URL '+edtURL.value)
  74. end;
  75. begin
  76. Result:=True;
  77. window.fetch(edtURL.Value,TJSObject.New)._then(@DoOK,@DoFail);
  78. end;
  79. var
  80. Application : TMyApplication;
  81. begin
  82. Application:=TMyApplication.Create(nil);
  83. Application.Initialize;
  84. Application.Run;
  85. end.