Client.pas 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. unit Client;
  26. {$MODE DELPHI}
  27. interface
  28. uses
  29. SysUtils,
  30. Classes,
  31. DB,
  32. BufDataset,
  33. FPHTTPClient;
  34. function NewGuid: string;
  35. function ListPersons(const AURL: string): TDataSet;
  36. procedure SavePersons(const AURL: string; ADataSet: TDataSet);
  37. function CreatePersonsDataSet: TDataSet;
  38. implementation
  39. function NewGuid: string;
  40. begin
  41. Result := TGuid.NewGuid.ToString(True);
  42. end;
  43. function ListPersons(const AURL: string): TDataSet;
  44. var
  45. VData: TStream;
  46. begin
  47. Result := TBufDataset.Create(nil);
  48. VData := TBytesStream.Create;
  49. try
  50. TFPHTTPClient.SimpleGet(AURL, VData);
  51. TBufDataset(Result).LoadFromStream(VData, dfBinary);
  52. finally
  53. VData.Free;
  54. end;
  55. end;
  56. procedure SavePersons(const AURL: string; ADataSet: TDataSet);
  57. var
  58. VClient: TFPHTTPClient;
  59. begin
  60. if ADataSet.State in dsEditModes then
  61. ADataSet.Post;
  62. try
  63. VClient := TFPHTTPClient.Create(nil);
  64. VClient.RequestBody := TBytesStream.Create;
  65. try
  66. TBufDataset(ADataSet).SaveToStream(VClient.RequestBody, dfBinary);
  67. VClient.RequestBody.Seek(0, TSeekOrigin.soBeginning);
  68. VClient.Post(AURL);
  69. finally
  70. VClient.RequestBody.Free;
  71. VClient.Free;
  72. end;
  73. finally
  74. FreeAndNil(ADataSet);
  75. end;
  76. end;
  77. function CreatePersonsDataSet: TDataSet;
  78. begin
  79. Result := TBufDataset.Create(nil);
  80. Result.FieldDefs.Add('name', ftString, 100);
  81. TBufDataset(Result).CreateDataSet;
  82. end;
  83. end.