Skip to content

Commit

Permalink
Added OnSendData callback in TMVCRESTClient
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Apr 23, 2024
1 parent d7bbd34 commit 2a7a840
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 7 additions & 1 deletion sources/MVCFramework.RESTClient.Intf.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ***************************************************************************
// ***************************************************************************
//
// Delphi MVC Framework
//
Expand Down Expand Up @@ -53,6 +53,7 @@ interface
TBeforeRequestProc = reference to procedure (aRequest: IHTTPRequest);
TRequestCompletedProc = reference to procedure (aResponse: IHTTPResponse; var aHandled: Boolean);
TResponseCompletedProc = reference to procedure(aResponse: IMVCRESTResponse);
TSendDataProc = reference to procedure(AContentLength, AWriteCount: Int64; var AAbort: Boolean);

IMVCRESTClient = interface
['{592BC90F-B825-4B3B-84A7-6CA3927BAD69}']
Expand Down Expand Up @@ -103,6 +104,11 @@ interface
/// </summary>
function SetResponseCompletedProc(aResponseCompletedProc: TResponseCompletedProc): IMVCRESTClient;

/// <summary>
/// Executes while sending data
/// </summary>
function SetSendDataProc(aSendDataProc: TSendDataProc): IMVCRESTClient;

///<summary>
/// Set the client certificate for the request</summary>
/// </summary>
Expand Down
26 changes: 25 additions & 1 deletion sources/MVCFramework.RESTClient.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ***************************************************************************
// ***************************************************************************
//
// Delphi MVC Framework
//
Expand Down Expand Up @@ -99,6 +99,7 @@ TMVCRESTClient = class(TInterfacedObject, IMVCRESTClient)
fBeforeRequestProc: TBeforeRequestProc;
fRequestCompletedProc: TRequestCompletedProc;
fResponseCompletedProc: TResponseCompletedProc;
fSendDataProc: TSendDataProc;
[Weak] fClientCertificate: TStream;
fClientCertPassword: string;
fClientCertPath: string;
Expand All @@ -109,6 +110,7 @@ TMVCRESTClient = class(TInterfacedObject, IMVCRESTClient)
procedure DoBeforeRequest(aRequest: IHTTPRequest);
procedure DoRequestCompleted(aResponse: IHTTPResponse; var aHandled: Boolean);
procedure DoResponseCompleted(aMVCRESTResponse: IMVCRESTResponse);
procedure DoOnSendDataEvent(const Sender: TObject; AContentLength, AWriteCount: Int64; var AAbort: Boolean);
function GetBodyFormData: TMultipartFormData;
function ObjectIsList(aObject: TObject): Boolean;
function SerializeObject(aObject: TObject): string;
Expand Down Expand Up @@ -185,6 +187,11 @@ TMVCRESTClient = class(TInterfacedObject, IMVCRESTClient)
/// </summary>
function SetResponseCompletedProc(aResponseCompletedProc: TResponseCompletedProc): IMVCRESTClient;

/// <summary>
/// Executes while sending data
/// </summary>
function SetSendDataProc(aSendDataProc: TSendDataProc): IMVCRESTClient;

///<summary>
/// Set the client certificate for the request</summary>
/// </summary>
Expand Down Expand Up @@ -951,6 +958,7 @@ constructor TMVCRESTClient.Create;
fHTTPClient.OnValidateServerCertificate := DoValidateServerCertificate;
fHTTPClient.HandleRedirects := True;
fHTTPClient.MaxRedirects := TMVCRESTClientConsts.DEFAULT_MAX_REDIRECTS;
fHTTPClient.OnSendData := DoOnSendDataEvent;
{$IF defined(TOKYOORBETTER)}
fHTTPClient.SecureProtocols := CHTTPDefSecureProtocols;
{$ENDIF}
Expand All @@ -959,6 +967,7 @@ constructor TMVCRESTClient.Create;
fBeforeRequestProc := nil;
fRequestCompletedProc := nil;
fResponseCompletedProc := nil;
fSendDataProc := nil;
fParameters := TList<TMVCRESTParam>.Create;
fRawBody := TMemoryStream.Create;
fBodyFormData := nil;
Expand Down Expand Up @@ -1511,6 +1520,15 @@ function TMVCRESTClient.ObjectIsList(aObject: TObject): Boolean;
Result := fRttiContext.GetType(aObject.ClassType).GetMethod('GetEnumerator') <> nil;
end;

procedure TMVCRESTClient.DoOnSendDataEvent(const Sender: TObject; AContentLength,
AWriteCount: Int64; var AAbort: Boolean);
begin
if Assigned(fSendDataProc) then
begin
fSendDataProc(AContentLength, AWriteCount, AAbort);
end;
end;

function TMVCRESTClient.Options: IMVCRESTResponse;
begin
Result := ExecuteRequest(TMVCHTTPMethodType.httpOPTIONS);
Expand Down Expand Up @@ -1878,6 +1896,12 @@ function TMVCRESTClient.SetResponseCompletedProc(aResponseCompletedProc: TRespon
fResponseCompletedProc := aResponseCompletedProc;
end;

function TMVCRESTClient.SetSendDataProc(
aSendDataProc: TSendDataProc): IMVCRESTClient;
begin
fSendDataProc := aSendDataProc;
end;

function TMVCRESTClient.SetValidateServerCertificateProc(
aValidateCertificateProc: TValidateServerCertificateProc): IMVCRESTClient;
begin
Expand Down

0 comments on commit 2a7a840

Please sign in to comment.