Skip to content

Commit

Permalink
Tentative fix #98 POSTAsync should now work as expected. Minor other …
Browse files Browse the repository at this point in the history
…fixes included.
  • Loading branch information
andrea-magni committed Dec 9, 2020
1 parent fa497f3 commit 07623c0
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 21 deletions.
25 changes: 20 additions & 5 deletions Source/MARS.Client.Application.pas
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ TMARSClientApplication = class(TComponent)
constructor Create(AOwner: TComponent); override;
procedure DoError(const AResource: TObject; const AException: Exception; const AVerb: TMARSHttpVerb; const AAfterExecute: TMARSClientResponseProc); virtual;

procedure CloneSetup(const ASource: TMARSClientApplication); virtual;
// procedure CloneStatus(const ASource: TMARSClientApplication); virtual;

const DEFAULT_APPNAME = 'default';
published
property DefaultMediaType: string read FDefaultMediaType write FDefaultMediaType;
Expand All @@ -59,11 +62,23 @@ procedure TMARSClientApplication.AssignTo(Dest: TPersistent);
// inherited;
LDestApp := Dest as TMARSClientApplication;

LDestApp.DefaultMediaType := DefaultMediaType;
LDestApp.DefaultContentType := DefaultContentType;
LDestApp.AppName := AppName;
LDestApp.Client := Client;
LDestApp.OnError := OnError;
if Assigned(LDestApp) then
begin
LDestApp.DefaultMediaType := DefaultMediaType;
LDestApp.DefaultContentType := DefaultContentType;
LDestApp.AppName := AppName;
LDestApp.Client := Client;
LDestApp.OnError := OnError;
end;
end;

procedure TMARSClientApplication.CloneSetup(
const ASource: TMARSClientApplication);
begin
if not Assigned(ASource) then
Exit;

Assign(ASource);
end;

constructor TMARSClientApplication.Create(AOwner: TComponent);
Expand Down
14 changes: 13 additions & 1 deletion Source/MARS.Client.Client.pas
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ TMARSCustomClient = class(TComponent)
procedure EndorseAuthorization; virtual;
procedure AuthEndorsementChanged; virtual;
procedure BeforeExecute; virtual;

property AuthToken: string read FAuthToken;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;

procedure CloneSetup(const ASource: TMARSCustomClient); virtual;
// procedure CloneStatus(const ASource: TMARSClientCustomClient); virtual;

procedure ApplyCustomHeaders(const AHeaders: TStrings); virtual;
procedure DoError(const AResource: TObject; const AException: Exception;
const AVerb: TMARSHttpVerb; const AAfterExecute: TMARSClientResponseProc); virtual;
Expand Down Expand Up @@ -219,12 +223,12 @@ procedure TMARSCustomClient.AssignTo(Dest: TPersistent);

if Assigned(LDestClient) then
begin
LDestClient.AuthCookieName := AuthCookieName;
LDestClient.AuthEndorsement := AuthEndorsement;
LDestClient.MARSEngineURL := MARSEngineURL;
LDestClient.ConnectTimeout := ConnectTimeout;
LDestClient.ReadTimeout := ReadTimeout;
LDestClient.OnError := OnError;

LDestClient.ProxyConfig.Assign(ProxyConfig);
end;
end;
Expand All @@ -246,6 +250,14 @@ class procedure TMARSCustomClient.ClearBeforeExecute;
FBeforeExecuteProcs := [];
end;

procedure TMARSCustomClient.CloneSetup(const ASource: TMARSCustomClient);
begin
if not Assigned(ASource) then
Exit;

Assign(ASource);
end;

constructor TMARSCustomClient.Create(AOwner: TComponent);
begin
inherited;
Expand Down
28 changes: 24 additions & 4 deletions Source/MARS.Client.CustomResource.pas
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ TMARSClientCustomResource = class(TComponent)
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;

procedure CloneSetup(const ASource: TMARSClientCustomResource); virtual;
procedure CloneStatus(const ASource: TMARSClientCustomResource); virtual;

// http verbs
procedure GET(const ABeforeExecute: TMARSClientProc{$ifdef DelphiXE2_UP} = nil{$endif};
const AAfterExecute: TMARSClientResponseProc{$ifdef DelphiXE2_UP} = nil{$endif};
Expand Down Expand Up @@ -258,6 +261,7 @@ procedure TMARSClientCustomResource.AssignTo(Dest: TPersistent);
LDestResource.SpecificContentType := SpecificContentType;
LDestResource.SpecificClient := SpecificClient;
LDestResource.SpecificToken := SpecificToken;
LDestResource.SpecificURL := SpecificURL;
LDestResource.Resource := Resource;
LDestResource.CustomHeaders.Assign(CustomHeaders);
LDestResource.PathParamsValues.Assign(PathParamsValues);
Expand Down Expand Up @@ -289,6 +293,20 @@ procedure TMARSClientCustomResource.BeforePUT(const AContent: TMemoryStream);

end;

procedure TMARSClientCustomResource.CloneSetup(
const ASource: TMARSClientCustomResource);
begin
if not Assigned(ASource) then
Exit;

Assign(ASource);
end;

procedure TMARSClientCustomResource.CloneStatus(
const ASource: TMARSClientCustomResource);
begin
end;

constructor TMARSClientCustomResource.Create(AOwner: TComponent);
begin
inherited;
Expand Down Expand Up @@ -766,14 +784,16 @@ procedure TMARSClientCustomResource.POSTAsync(
begin
LClient := TMARSCustomClientClass(Client.ClassType).Create(nil);
try
LClient.Assign(Client);
LClient.CloneSetup(Client);

LApplication := TMARSClientApplication.Create(nil);
try
LApplication.Assign(Application);
LApplication.CloneSetup(Application);
LApplication.Client := LClient;

LResource := TMARSClientCustomResourceClass(ClassType).Create(nil);
try
LResource.Assign(Self);
LResource.CloneSetup(Self);
LResource.SpecificClient := nil;
LResource.Application := LApplication;

Expand Down Expand Up @@ -806,7 +826,7 @@ procedure TMARSClientCustomResource.POSTAsync(
ABeforeExecute
, procedure (AStream: TStream)
begin
Assign(LResource);
CloneStatus(LResource);

if Assigned(ACompletionHandler) then
begin
Expand Down
17 changes: 16 additions & 1 deletion Source/MARS.Client.Resource.FormData.pas
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ TMARSClientResourceFormData = class(TMARSClientResource)
function ResponseAsJSON: TJSONValue;
function ResponseAs<T: record>: T;
function ResponseAsArray<T: record>: TArray<T>;
procedure CloneStatus(const ASource: TMARSClientCustomResource); override;
published
property FormData: TArray<TFormParam> read FFormData write FFormData;
property Response: TMemoryStream read FResponse;
Expand Down Expand Up @@ -105,6 +106,20 @@ procedure TMARSClientResourceFormData.AssignTo(Dest: TPersistent);
TMARSClientResourceFormData(Dest).FormData := FFormData;
end;

procedure TMARSClientResourceFormData.CloneStatus(
const ASource: TMARSClientCustomResource);
var
LSource: TMARSClientResourceFormData;
begin
inherited;
LSource := ASource as TMARSClientResourceFormData;
if Assigned(LSource) then
begin
Response.Size := 0; // empty
Response.CopyFrom(LSource.Response);
end;
end;

constructor TMARSClientResourceFormData.Create(AOwner: TComponent);
begin
inherited;
Expand All @@ -115,7 +130,7 @@ constructor TMARSClientResourceFormData.Create(AOwner: TComponent);

destructor TMARSClientResourceFormData.Destroy;
begin
FResponse.Free;
FreeAndNil(FResponse);
FFormData := [];
inherited;
end;
Expand Down
15 changes: 15 additions & 0 deletions Source/MARS.Client.Resource.FormUrlEncoded.pas
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ TMARSClientResourceFormUrlEncoded = class(TMARSClientResource)
function ResponseAsJSON: TJSONValue;
function ResponseAs<T: record>: T;
function ResponseAsArray<T: record>: TArray<T>;
procedure CloneStatus(const ASource: TMARSClientCustomResource); override;
published
property FormUrlEncoded: TMARSParameters read FFormUrlEncoded write FFormUrlEncoded;
property Response: TMemoryStream read FResponse;
Expand Down Expand Up @@ -102,6 +103,20 @@ procedure TMARSClientResourceFormUrlEncoded.AssignTo(Dest: TPersistent);
TMARSClientResourceFormUrlEncoded(Dest).FFormUrlEncoded := FFormUrlEncoded;
end;

procedure TMARSClientResourceFormUrlEncoded.CloneStatus(
const ASource: TMARSClientCustomResource);
var
LSource: TMARSClientResourceFormData;
begin
inherited;
LSource := ASource as TMARSClientResourceFormData;
if Assigned(LSource) then
begin
Response.Size := 0; // empty
Response.CopyFrom(LSource.Response);
end;
end;

constructor TMARSClientResourceFormUrlEncoded.Create(AOwner: TComponent);
begin
inherited;
Expand Down
36 changes: 32 additions & 4 deletions Source/MARS.Client.Resource.JSON.pas
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ TMARSClientResourceJSON = class(TMARSClientResource, IRESTResponseJSON)

function ResponseAs<T: record>: T;
function ResponseAsArray<T: record>: TArray<T>;
procedure CloneStatus(const ASource: TMARSClientCustomResource); override;
published
property Response: TJSONValue read FResponse write FResponse;
property ResponseAsString;
Expand Down Expand Up @@ -233,13 +234,37 @@ procedure TMARSClientResourceJSON.AssignTo(Dest: TPersistent);
begin
inherited AssignTo(Dest);
LDest := Dest as TMARSClientResourceJSON;
LDest.FNotifyList.Clear;
for LNotifyEvent in FNotifyList do
LDest.FNotifyList.Add(LNotifyEvent);
if Assigned(LDest) then
begin
LDest.FNotifyList.Clear;
for LNotifyEvent in FNotifyList do
LDest.FNotifyList.Add(LNotifyEvent);

LDest.RefreshResponse(FResponse);
LDest.RefreshResponse(FResponse);
end;
end;

procedure TMARSClientResourceJSON.CloneStatus(
const ASource: TMARSClientCustomResource);
var
LSource: TMARSClientResourceJSON;
begin
inherited;
LSource := ASource as TMARSClientResourceJSON;
if Assigned(LSource) then
begin
if Assigned(LSource.Response) then
begin
FResponse := LSource.Response.Clone as TJSONValue;
FResponse.Owned := False;
// RefreshResponse(LSource.Response.Clone as TJSONValue)
end
else
RefreshResponse(TJSONValue(nil));
end;
end;


constructor TMARSClientResourceJSON.Create(AOwner: TComponent);
begin
inherited;
Expand Down Expand Up @@ -587,7 +612,10 @@ procedure TMARSClientResourceJSON.RefreshResponse(const AContent: TJSONValue);
begin
FreeAndNil(FResponse);
if Assigned(AContent) then
begin
FResponse := AContent;
FResponse.Owned := False;
end;

for LSubscriber in FNotifyList do
LSubscriber(Self);
Expand Down
31 changes: 25 additions & 6 deletions Source/MARS.Client.Token.pas
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ interface

uses
SysUtils, Classes
, MARS.Core.JSON, MARS.Client.Resource, MARS.Client.Utils
, MARS.Core.JSON, MARS.Client.Resource, MARS.Client.CustomResource
, MARS.Client.Utils
, MARS.Utils.Parameters, MARS.Utils.Parameters.JSON
;

Expand Down Expand Up @@ -49,6 +50,7 @@ TMARSClientToken = class(TMARSClientResource)
procedure LoadFromStream(const AStream: TStream); virtual;
procedure SaveToFile(const AFilename: string); virtual;
procedure LoadFromFile(const AFilename: string); virtual;
procedure CloneStatus(const ASource: TMARSClientCustomResource); override;
published
property Data: TJSONObject read FData;

Expand Down Expand Up @@ -98,11 +100,12 @@ procedure TMARSClientToken.AssignTo(Dest: TPersistent);
begin
inherited AssignTo(Dest);
LDest := Dest as TMARSClientToken;

LDest.LoadFromBytes(SaveToBytes);

LDest.UserName := UserName;
LDest.Password := Password;
if Assigned(LDest) then
begin
LDest.LoadFromBytes(SaveToBytes);
LDest.UserName := UserName;
LDest.Password := Password;
end;
end;

procedure TMARSClientToken.BeforePOST(const AContent: TMemoryStream);
Expand All @@ -124,6 +127,22 @@ procedure TMARSClientToken.Clear;
FUserRoles.Clear;
end;

procedure TMARSClientToken.CloneStatus(
const ASource: TMARSClientCustomResource);
var
LSource: TMARSClientToken;
begin
inherited;
LSource := ASource as TMARSClientToken;
if Assigned(LSource) then
begin
if Assigned(LSource.Data) then
SetData(LSource.Data.Clone as TJSONObject)
else
SetData(nil);
end;
end;

constructor TMARSClientToken.Create(AOwner: TComponent);
begin
inherited;
Expand Down

0 comments on commit 07623c0

Please sign in to comment.