Client.pas 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. interface
  27. uses
  28. System.SysUtils,
  29. System.Classes,
  30. Data.DB,
  31. System.Net.HTTPClient,
  32. FireDAC.Stan.Intf,
  33. FireDAC.Comp.Client,
  34. FireDAC.Stan.StorageBin;
  35. function NewGuid: string;
  36. function ListPersons(const AURL: string): TDataSet;
  37. procedure SavePersons(const AURL: string; ADataSet: TDataSet);
  38. function CreatePersonsDataSet: TDataSet;
  39. implementation
  40. function NewGuid: string;
  41. begin
  42. Result := TGuid.NewGuid.ToString;
  43. end;
  44. function CreateDataSet: TFDMemTable;
  45. begin
  46. Result := TFDMemTable.Create(nil);
  47. Result.CachedUpdates := True;
  48. end;
  49. function ListPersons(const AURL: string): TDataSet;
  50. var
  51. VClient: THTTPClient;
  52. begin
  53. Result := CreateDataSet;
  54. VClient := THTTPClient.Create;
  55. try
  56. TFDMemTable(Result).LoadFromStream(VClient.Get(AURL).ContentStream, sfBinary);
  57. finally
  58. VClient.Free;
  59. end;
  60. end;
  61. procedure SavePersons(const AURL: string; ADataSet: TDataSet);
  62. var
  63. VClient: THTTPClient;
  64. VData: TStream;
  65. begin
  66. if ADataSet.State in dsEditModes then
  67. ADataSet.Post;
  68. try
  69. VData := TBytesStream.Create;
  70. VClient := THTTPClient.Create;
  71. try
  72. TFDMemTable(ADataSet).SaveToStream(VData, sfBinary);
  73. VData.Seek(0, TSeekOrigin.soBeginning);
  74. VClient.Post(AURL, VData);
  75. finally
  76. VClient.Free;
  77. VData.Free;
  78. end;
  79. finally
  80. FreeAndNil(ADataSet);
  81. end;
  82. end;
  83. function CreatePersonsDataSet: TDataSet;
  84. begin
  85. Result := CreateDataSet;
  86. Result.FieldDefs.Add('name', ftString, 100);
  87. TFDMemTable(Result).CreateDataSet;
  88. end;
  89. end.