Skip to content

Commit

Permalink
Added CustomHeaders support at MARS Client Resource level (works prop…
Browse files Browse the repository at this point in the history
…erly also with Async calls with either TNetHttpClient and Indy)
  • Loading branch information
andrea-magni committed Jul 5, 2019
1 parent daef649 commit a8d3235
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Source/MARS.Client.Client.Indy.pas
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ TMARSIndyClient = class(TMARSCustomClient)

function CreateMultipartFormData(AFormData: TArray<TFormParam>): TIdMultiPartFormDataStream;


procedure EndorseAuthorization; override;
procedure ApplyProxyConfig; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;

procedure ApplyCustomHeaders(const AHeaders: TStrings); override;

procedure Delete(const AURL: string; AContent, AResponse: TStream;
const AAuthToken: string; const AAccept: string; const AContentType: string); override;
procedure Get(const AURL: string; AResponseContent: TStream;
Expand Down Expand Up @@ -90,6 +93,12 @@ implementation

{ TMARSIndyClient }

procedure TMARSIndyClient.ApplyCustomHeaders(const AHeaders: TStrings);
begin
inherited;
FHttpClient.Request.CustomHeaders.AddStrings(AHeaders);
end;

procedure TMARSIndyClient.ApplyProxyConfig;
begin
inherited;
Expand Down
11 changes: 11 additions & 0 deletions Source/MARS.Client.Client.Net.pas
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ TMARSNetClient = class(TMARSCustomClient)
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;

procedure ApplyCustomHeaders(const AHeaders: TStrings); override;

procedure Delete(const AURL: string; AContent, AResponse: TStream;
const AAuthToken: string; const AAccept: string; const AContentType: string); override;
procedure Get(const AURL: string; AResponseContent: TStream;
Expand Down Expand Up @@ -84,6 +86,15 @@ implementation

{ TMARSNetClient }

procedure TMARSNetClient.ApplyCustomHeaders(const AHeaders: TStrings);
var
LIndex: Integer;
begin
inherited;
for LIndex := 0 to AHeaders.Count-1 do
HttpClient.CustomHeaders[AHeaders.Names[LIndex]] := AHeaders.ValueFromIndex[LIndex];
end;

procedure TMARSNetClient.ApplyProxyConfig;
begin
inherited;
Expand Down
6 changes: 6 additions & 0 deletions Source/MARS.Client.Client.pas
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ TMARSCustomClient = class(TComponent)
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;

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 @@ -190,6 +191,11 @@ function TMARSHttpVerbToString(const AVerb: TMARSHttpVerb): string;

{ TMARSCustomClient }

procedure TMARSCustomClient.ApplyCustomHeaders(const AHeaders: TStrings);
begin
// to be implemented in inherited classes
end;

procedure TMARSCustomClient.ApplyProxyConfig;
begin
// to be implemented in inherited classes
Expand Down
25 changes: 23 additions & 2 deletions Source/MARS.Client.CustomResource.pas
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ TMARSClientCustomResource = class(TComponent)
FApplication: TMARSClientApplication;
FSpecificClient: TMARSCustomClient;
FSpecificToken: string;
FCustomHeaders: TStrings;
FPathParamsValues: TStrings;
FQueryParams: TStrings;
FSpecificAccept: string;
Expand All @@ -33,7 +34,9 @@ TMARSClientCustomResource = class(TComponent)
FToken: TMARSClientCustomResource;
procedure SetPathParamsValues(const Value: TStrings);
procedure SetQueryParams(const Value: TStrings);
procedure SetCustomHeaders(const Value: TStrings);
protected
procedure ApplyCustomHeaders;
function GetClient: TMARSCustomClient; virtual;
function GetPath: string; virtual;
function GetURL: string; virtual;
Expand Down Expand Up @@ -119,6 +122,7 @@ TMARSClientCustomResource = class(TComponent)
property Application: TMARSClientApplication read GetApplication write FApplication;
property AuthToken: string read GetAuthToken;
property Client: TMARSCustomClient read GetClient;
property CustomHeaders: TStrings read FCustomHeaders write SetCustomHeaders;
property SpecificAccept: string read FSpecificAccept write FSpecificAccept;
property SpecificClient: TMARSCustomClient read FSpecificClient write FSpecificClient;
property SpecificContentType: string read FSpecificContentType write FSpecificContentType;
Expand Down Expand Up @@ -167,6 +171,11 @@ procedure TMARSClientCustomResource.AfterPUT(const AContent: TStream);

end;

procedure TMARSClientCustomResource.ApplyCustomHeaders;
begin
Client.ApplyCustomHeaders(CustomHeaders);
end;

procedure TMARSClientCustomResource.AssignTo(Dest: TPersistent);
var
LDestResource: TMARSClientCustomResource;
Expand All @@ -180,28 +189,33 @@ procedure TMARSClientCustomResource.AssignTo(Dest: TPersistent);
LDestResource.SpecificClient := SpecificClient;
LDestResource.SpecificToken := SpecificToken;
LDestResource.Resource := Resource;
LDestResource.CustomHeaders.Assign(CustomHeaders);
LDestResource.PathParamsValues.Assign(PathParamsValues);
LDestResource.QueryParams.Assign(QueryParams);
LDestResource.Token := Token;
end;

procedure TMARSClientCustomResource.BeforeDELETE(const AContent: TMemoryStream);
begin
ApplyCustomHeaders;

end;

procedure TMARSClientCustomResource.BeforeGET;
begin
ApplyCustomHeaders;

end;

procedure TMARSClientCustomResource.BeforePOST(const AContent: TMemoryStream);
begin
ApplyCustomHeaders;

end;

procedure TMARSClientCustomResource.BeforePUT(const AContent: TMemoryStream);
begin
ApplyCustomHeaders;

end;

Expand All @@ -214,10 +228,11 @@ constructor TMARSClientCustomResource.Create(AOwner: TComponent);
FApplication := TMARSComponentHelper.FindDefault<TMARSClientApplication>(Self);
FToken := TMARSComponentHelper.FindDefault<TMARSClientToken>(Self);
end;
FPathParamsValues := TStringList.Create;
FQueryParams := TStringList.Create;
FSpecificAccept := TMediaType.WILDCARD;
FSpecificContentType := '';
FCustomHeaders := TStringList.Create;
FPathParamsValues := TStringList.Create;
FQueryParams := TStringList.Create;
end;

function TMARSClientCustomResource.GetAuthToken: string;
Expand Down Expand Up @@ -443,6 +458,7 @@ procedure TMARSClientCustomResource.DELETEAsync(

destructor TMARSClientCustomResource.Destroy;
begin
FCustomHeaders.Free;
FQueryParams.Free;
FPathParamsValues.Free;
inherited;
Expand Down Expand Up @@ -884,6 +900,11 @@ procedure TMARSClientCustomResource.PUTAsync(
end;
{$endif}

procedure TMARSClientCustomResource.SetCustomHeaders(const Value: TStrings);
begin
FCustomHeaders.Assign(Value);
end;

procedure TMARSClientCustomResource.SetPathParamsValues(const Value: TStrings);
begin
FPathParamsValues.Assign(Value);
Expand Down
1 change: 1 addition & 0 deletions Source/MARS.Client.Resource.pas
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ TMARSClientResource = class(TMARSClientCustomResource)
property Application;
property AuthToken;
property Client;
property CustomHeaders;
property ContentType;
property SpecificAccept;
property SpecificClient;
Expand Down

0 comments on commit a8d3235

Please sign in to comment.