Added automatic deserialization to TMVCRESTClient.

Resolves #529
This commit is contained in:
João Antônio Duarte 2022-08-03 14:28:01 -03:00
parent 945f807598
commit 7c6eb51811
5 changed files with 33 additions and 15 deletions

3
.gitignore vendored
View File

@ -101,6 +101,7 @@ unittests/general/Several/Win32/GUI/sqlitetest.db
*.txaPackage
unittests/general/Several/bin/sqlitetest.db
unittests/general/Several/bin/firebirdtest.fdb
unittests/general/Several/bin/firebirdtest2.fdb
samples/winecellarclient_mobile/Android64/
samples/middleware_analytics/Win32/
*.txt
@ -118,3 +119,5 @@ samples/master_details/masterdetailssample.otares
samples/master_details/masterdetailssample_Icon.ico
samples/serversideviews_lua/bin/templates/__compiled/
samples/serversideviews_lua/lua4delphi/unittests/Win32/
/unittests/general/Several/bin/EchoSingleComplexRecord_RESPONSE.json
/unittests/general/Several/bin/TestRequest_Echo_ComplexRecords_RESPONSE.json

View File

@ -824,7 +824,7 @@ begin
'StringProperty = ' + Self.StringProperty + sLineBreak +
'IntegerProperty = ' + Self.IntegerProperty.ToString + sLineBreak +
'FloatProperty = ' + Self.FloatProperty.ToString + sLineBreak +
'CurrencyProperty = ' + Self.CurrencyProperty.ToString + sLineBreak +
'CurrencyProperty = ' + CurrToStr(Self.CurrencyProperty) + sLineBreak +
'DateProperty = ' + DateToStr(Self.DateProperty) + sLineBreak +
'TimeProperty = ' + TimeToStr(Self.TimeProperty) + sLineBreak +
'DateTimeProperty = ' + FormatDateTime('yyyy-mm-dd hh:nn:ss', Self.DateTimeProperty) + sLineBreak +

View File

@ -485,6 +485,8 @@ type
procedure SaveContentToFile(const aFileName: string);
function ToJSONObject: TJDOJsonObject;
function ToJSONArray: TJDOJsonArray;
procedure BodyFor(const aObject: TObject; const aRootNode: string = '');
procedure BodyForListOf(const aObjectList: TObject; const aObjectClass: TClass; const aRootNode: string = '');
end;
implementation

View File

@ -59,6 +59,9 @@ type
IRESTResponse = MVCFramework.RESTClient.Indy.IRESTResponse deprecated
'Moved to the MVCFramework.RESTClient.Indy unit. It is highly recommended to migrate to the TMVCRESTClient implementation.';
IMVCRESTClient = MVCFramework.RESTClient.Intf.IMVCRESTClient;
IMVCRESTResponse = MVCFramework.RESTClient.Intf.IMVCRESTResponse;
TCookie = System.Net.HttpClient.TCookie;
TCookies = System.Net.HttpClient.TCookies;
TURLRequest = System.Net.URLClient.TURLRequest;
@ -502,6 +505,7 @@ type
/// </summary>
TMVCRESTResponse = class(TInterfacedObject, IMVCRESTResponse)
private
fRESTClient: IMVCRESTClient;
fSuccess: Boolean;
fStatusCode: Integer;
fStatusText: string;
@ -514,9 +518,9 @@ type
fContent: string;
fContentRawBytes: TBytes;
procedure FillResponse(aHTTPResponse: IHTTPResponse);
procedure FillResponse(const aHTTPResponse: IHTTPResponse);
public
constructor Create(aHTTPResponse: IHTTPResponse);
constructor Create(const aRESTClient: IMVCRESTClient; const aHTTPResponse: IHTTPResponse);
destructor Destroy; override;
{ IMVCRESTResponse }
@ -537,6 +541,8 @@ type
procedure SaveContentToFile(const aFileName: string);
function ToJSONObject: TJDOJsonObject;
function ToJSONArray: TJDOJsonArray;
procedure BodyFor(const aObject: TObject; const aRootNode: string = '');
procedure BodyForListOf(const aObjectList: TObject; const aObjectClass: TClass; const aRootNode: string = '');
end;
implementation
@ -1446,7 +1452,7 @@ begin
if not lHandled then
begin
Result := TMVCRESTResponse.Create(lResponse);
Result := TMVCRESTResponse.Create(Self, lResponse);
DoResponseCompleted(Result);
end
else
@ -1861,6 +1867,16 @@ begin
Result := StrTOJSONObject(fContent, True);
end;
procedure TMVCRESTResponse.BodyFor(const aObject: TObject; const aRootNode: string);
begin
fRESTClient.Serializer.DeserializeObject(fContent, aObject, TMVCSerializationType.stDefault, [], aRootNode);
end;
procedure TMVCRESTResponse.BodyForListOf(const aObjectList: TObject; const aObjectClass: TClass; const aRootNode: string);
begin
fRESTClient.Serializer.DeserializeCollection(fContent, aObjectList, aObjectClass, TMVCSerializationType.stDefault, [], aRootNode);
end;
function TMVCRESTResponse.Content: string;
begin
Result := fContent;
@ -1898,11 +1914,12 @@ begin
Result := fCookies;
end;
constructor TMVCRESTResponse.Create(aHTTPResponse: IHTTPResponse);
constructor TMVCRESTResponse.Create(const aRESTClient: IMVCRESTClient; const aHTTPResponse: IHTTPResponse);
begin
fHeaders := TStringList.Create;
SetLength(fContentRawBytes, 0);
fCookies := TCookies.Create;
fRESTClient := aRESTClient;
FillResponse(aHTTPResponse);
end;
@ -1915,7 +1932,7 @@ begin
inherited Destroy;
end;
procedure TMVCRESTResponse.FillResponse(aHTTPResponse: IHTTPResponse);
procedure TMVCRESTResponse.FillResponse(const aHTTPResponse: IHTTPResponse);
var
lHeader: TNetHeader;
begin

View File

@ -230,7 +230,7 @@ begin
LUser := TAppUser.Create;
try
GetDefaultSerializer.DeserializeObject(FRESTClient.Get.Content, LUser);
FRESTClient.Get.BodyFor(LUser);
Assert.IsTrue((LUser <> nil) and (LUser.Cod > 0));
finally
FreeAndNil(LUser);
@ -248,23 +248,22 @@ end;
procedure TTestRESTClient.TestGetUsers;
var
LUsers: TObjectList<TAppUser>;
lBody: string;
lResp: IMVCRESTResponse;
begin
FRESTClient.Resource('/users');
// FRESTClient.SetBasicAuthorization('dmvc', '123');
lBody := FRESTClient.Get.Content;
lResp := FRESTClient.Get;
// String
Assert.AreEqual('[{"Cod":0,"Name":"Ezequiel 0","Pass":"0"},{"Cod":1,"Name":"Ezequiel 1","Pass":"1"},' +
'{"Cod":2,"Name":"Ezequiel 2","Pass":"2"},{"Cod":3,"Name":"Ezequiel 3","Pass":"3"},{"Cod":4,"Name":"Ezequiel 4","Pass":"4"},' +
'{"Cod":5,"Name":"Ezequiel 5","Pass":"5"},{"Cod":6,"Name":"Ezequiel 6","Pass":"6"},{"Cod":7,"Name":"Ezequiel 7","Pass":"7"},' +
'{"Cod":8,"Name":"Ezequiel 8","Pass":"8"},{"Cod":9,"Name":"Ezequiel 9","Pass":"9"},{"Cod":10,"Name":"Ezequiel 10","Pass":"10"}]',
lBody);
LResp.Content);
// Objects
LUsers := TObjectList<TAppUser>.Create(True);
try
GetDefaultSerializer.DeserializeCollection(lBody, lUsers, TAppUser); // BodyAsJSONArray.AsObjectList<TAppUser>;
lResp.BodyForListOf(LUsers, TAppUser);
LUsers.OwnsObjects := True;
Assert.IsTrue(LUsers.Count > 0);
finally
@ -284,7 +283,6 @@ end;
procedure TTestRESTClient.TestHelloWorld;
begin
FRESTClient.Resource('/hello');
// FRESTClient.SetBasicAuthorization('dmvc', '123');
// String
Assert.AreEqual('Hello World called with GET', FRESTClient.Get.Content);
@ -334,7 +332,6 @@ var
LResp: IMVCRESTResponse;
begin
FRESTClient.Resource('/user/save');
// FRESTClient.SetBasicAuthorization('dmvc', '123');
LUser := TAppUser.Create;
LUser.Cod := 1;
@ -359,7 +356,6 @@ var
LUser: TAppUser;
begin
FRESTClient.Resource('/users/save');
// FRESTClient.SetBasicAuthorization('dmvc', '123');
FRESTClient.Accept('application/json;charset=utf-8');
LUsers := TObjectList<TAppUser>.Create(True);