mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-15 15:55:54 +01:00
Merge pull request #469 from joaoduarte19/improvement_restclient
Improvements TMVCRESTClient
This commit is contained in:
commit
bc425ebfa4
@ -146,6 +146,7 @@ type
|
||||
/// </param>
|
||||
function AddHeader(const aName, aValue: string): IMVCRESTClient; overload;
|
||||
function HeaderValue(const aName: string): string;
|
||||
function Headers: TNameValueArray;
|
||||
|
||||
/// <summary>
|
||||
/// Clears all headers.
|
||||
|
@ -63,6 +63,8 @@ type
|
||||
TCookies = System.Net.HttpClient.TCookies;
|
||||
TURLRequest = System.Net.URLClient.TURLRequest;
|
||||
TCertificate = System.Net.URLClient.TCertificate;
|
||||
TNameValuePair = System.Net.URLClient.TNameValuePair;
|
||||
TNameValueArray = System.Net.URLClient.TNameValueArray;
|
||||
IHTTPRequest = System.Net.HttpClient.IHTTPRequest;
|
||||
IHTTPResponse = System.Net.HttpClient.IHTTPResponse;
|
||||
|
||||
@ -76,12 +78,6 @@ type
|
||||
fHTTPClient: THTTPClient;
|
||||
fBaseURL: string;
|
||||
fResource: string;
|
||||
fAccept: string;
|
||||
fAcceptCharset: string;
|
||||
fAcceptEncoding: string;
|
||||
fUserAgent: string;
|
||||
fContentType: string;
|
||||
fAuthorization: string;
|
||||
fProxySettings: TProxySettings;
|
||||
fParameters: TList<TMVCRESTParam>;
|
||||
fRawBody: TStringStream;
|
||||
@ -225,6 +221,7 @@ type
|
||||
/// </param>
|
||||
function AddHeader(const aName, aValue: string): IMVCRESTClient; overload;
|
||||
function HeaderValue(const aName: string): string;
|
||||
function Headers: TNameValueArray;
|
||||
|
||||
/// <summary>
|
||||
/// Clears all headers.
|
||||
@ -522,35 +519,32 @@ type
|
||||
|
||||
function TMVCRESTClient.Accept: string;
|
||||
begin
|
||||
Result := fAccept;
|
||||
Result := HeaderValue(sAccept);
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.Accept(const aAccept: string): IMVCRESTClient;
|
||||
begin
|
||||
Result := Self;
|
||||
fAccept := aAccept;
|
||||
Result := AddHeader(sAccept, aAccept);
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.AcceptCharset: string;
|
||||
begin
|
||||
Result := fAcceptCharset;
|
||||
Result := HeaderValue(sAcceptCharset);
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.AcceptCharset(const aAcceptCharset: string): IMVCRESTClient;
|
||||
begin
|
||||
Result := Self;
|
||||
fAcceptCharset := aAcceptCharset;
|
||||
Result := AddHeader(sAcceptCharset, aAcceptCharset);
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.AcceptEncoding(const aAcceptEncoding: string): IMVCRESTClient;
|
||||
begin
|
||||
Result := Self;
|
||||
fAcceptEncoding := aAcceptEncoding;
|
||||
Result := AddHeader(sAcceptEncoding, aAcceptEncoding);
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.AcceptEncoding: string;
|
||||
begin
|
||||
Result := fAcceptEncoding;
|
||||
Result := HeaderValue(sAcceptEncoding);
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.AddBody(const aBody: string; const aContentType: string): IMVCRESTClient;
|
||||
@ -767,7 +761,7 @@ end;
|
||||
|
||||
function TMVCRESTClient.Authorization: string;
|
||||
begin
|
||||
Result := fAuthorization;
|
||||
Result := HeaderValue(sAuthorization);
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.BaseURL(const aBaseURL: string): IMVCRESTClient;
|
||||
@ -792,11 +786,20 @@ begin
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.ClearAllParams: IMVCRESTClient;
|
||||
var
|
||||
lAuthorization: string;
|
||||
begin
|
||||
Result := Self;
|
||||
|
||||
lAuthorization := HeaderValue(sAuthorization);
|
||||
fParameters.Clear;
|
||||
ClearBody;
|
||||
|
||||
AddHeader(sAccept, TMVCRESTClientConsts.DEFAULT_ACCEPT);
|
||||
AddHeader(sAcceptEncoding, TMVCRESTClientConsts.DEFAULT_ACCEPT_ENCODING);
|
||||
AddHeader(sUserAgent, TMVCRESTClientConsts.DEFAULT_USER_AGENT);
|
||||
AddHeader(sAuthorization, lAuthorization);
|
||||
|
||||
fNextRequestIsAsync := False;
|
||||
fAsyncCompletionHandler := nil;
|
||||
fAsyncCompletionHandlerWithError := nil;
|
||||
@ -806,7 +809,7 @@ end;
|
||||
function TMVCRESTClient.ClearAuthorization: IMVCRESTClient;
|
||||
begin
|
||||
Result := Self;
|
||||
fAuthorization := '';
|
||||
AddHeader(sAuthorization, '');
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.ClearBody: IMVCRESTClient;
|
||||
@ -817,7 +820,7 @@ begin
|
||||
|
||||
Result := Self;
|
||||
ClearParameters(TMVCRESTParamType.FormURLEncoded);
|
||||
fContentType := '';
|
||||
AddHeader(sContentType, '');
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.ClearCookies: IMVCRESTClient;
|
||||
@ -896,13 +899,6 @@ begin
|
||||
fBaseURL := '';
|
||||
fResource := '';
|
||||
|
||||
fAccept := TMVCRESTClientConsts.DEFAULT_ACCEPT;
|
||||
fAcceptCharset := '';
|
||||
fAcceptEncoding := TMVCRESTClientConsts.DEFAULT_ACCEPT_ENCODING;
|
||||
fUserAgent := TMVCRESTClientConsts.DEFAULT_USER_AGENT;
|
||||
fContentType := '';
|
||||
fAuthorization := '';
|
||||
|
||||
ClearAllParams;
|
||||
end;
|
||||
|
||||
@ -1003,14 +999,6 @@ begin
|
||||
fHTTPClient.CustomHeaders[lParam.Name] := lParam.Value;
|
||||
end;
|
||||
end;
|
||||
if not fAuthorization.IsEmpty then
|
||||
begin
|
||||
fHTTPClient.CustomHeaders[TMVCRESTClientConsts.AUTHORIZATION_HEADER] := fAuthorization;
|
||||
end;
|
||||
fHTTPClient.Accept := fAccept;
|
||||
fHTTPClient.AcceptCharSet := fAcceptCharset;
|
||||
fHTTPClient.AcceptEncoding := fAcceptEncoding;
|
||||
fHTTPClient.ContentType := fContentType;
|
||||
end;
|
||||
|
||||
procedure TMVCRESTClient.DoApplyPathParams(var aURL: string);
|
||||
@ -1083,6 +1071,7 @@ end;
|
||||
|
||||
procedure TMVCRESTClient.DoPrepareBodyRequest(var aBodyStream: TStream);
|
||||
var
|
||||
lCurrentContentType: string;
|
||||
lContentType: string;
|
||||
lContentCharset: string;
|
||||
lParam: TMVCRESTParam;
|
||||
@ -1090,7 +1079,8 @@ var
|
||||
lValue: string;
|
||||
lBody: string;
|
||||
begin
|
||||
SplitContentMediaTypeAndCharset(fContentType, lContentType, lContentCharset);
|
||||
lCurrentContentType := HeaderValue(sContentType);
|
||||
SplitContentMediaTypeAndCharset(lCurrentContentType, lContentType, lContentCharset);
|
||||
|
||||
if SameText(lContentType, TMVCMediaType.MULTIPART_FORM_DATA) then
|
||||
begin
|
||||
@ -1111,7 +1101,7 @@ begin
|
||||
lBody := lBody + lName + '=' + lValue;
|
||||
end;
|
||||
end;
|
||||
AddBody(lBody, fContentType);
|
||||
AddBody(lBody, lCurrentContentType);
|
||||
aBodyStream := fRawBody;
|
||||
end
|
||||
else
|
||||
@ -1281,6 +1271,26 @@ begin
|
||||
Result := Head;
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.Headers: TNameValueArray;
|
||||
var
|
||||
lHeaders: TList<TNameValuePair>;
|
||||
lParam: TMVCRESTParam;
|
||||
begin
|
||||
lHeaders := TList<TNameValuePair>.Create;
|
||||
try
|
||||
for lParam in fParameters do
|
||||
begin
|
||||
if lParam.&Type = TMVCRESTParamType.Header then
|
||||
begin
|
||||
lHeaders.Add(TNameValuePair.Create(lParam.Name, lParam.Value));
|
||||
end;
|
||||
end;
|
||||
Result := lHeaders.ToArray;
|
||||
finally
|
||||
FreeAndNil(lHeaders);
|
||||
end;
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.HeaderValue(const aName: string): string;
|
||||
var
|
||||
lParam: TMVCRESTParam;
|
||||
@ -1634,7 +1644,7 @@ begin
|
||||
// Do not use TNetEncoding.Base64 here, because it may break long line
|
||||
lBase64 := TBase64Encoding.Create(0, '');
|
||||
try
|
||||
fAuthorization := TMVCRESTClientConsts.BASIC_AUTH_PREFIX + lBase64.Encode(aUsername + ':' + aPassword);
|
||||
AddHeader(sAuthorization, TMVCRESTClientConsts.BASIC_AUTH_PREFIX + lBase64.Encode(aUsername + ':' + aPassword));
|
||||
finally
|
||||
FreeAndNil(lBase64);
|
||||
end;
|
||||
@ -1642,8 +1652,7 @@ end;
|
||||
|
||||
function TMVCRESTClient.SetBearerAuthorization(const aAccessToken: string): IMVCRESTClient;
|
||||
begin
|
||||
Result := Self;
|
||||
fAuthorization := TMVCRESTClientConsts.BEARER_AUTH_PREFIX + aAccessToken;
|
||||
Result := AddHeader(sAuthorization, TMVCRESTClientConsts.BEARER_AUTH_PREFIX + aAccessToken);
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.SetBeforeRequestProc(aBeforeRequestProc: TBeforeRequestProc): IMVCRESTClient;
|
||||
@ -1654,7 +1663,7 @@ end;
|
||||
|
||||
procedure TMVCRESTClient.SetContentType(const aContentType: string);
|
||||
begin
|
||||
fContentType := aContentType;
|
||||
AddHeader(sContentType, aContentType);
|
||||
end;
|
||||
|
||||
procedure TMVCRESTClient.SetParameter(const aParamType: TMVCRESTParamType; const aName, aValue: string);
|
||||
@ -1669,7 +1678,6 @@ begin
|
||||
Break;
|
||||
end;
|
||||
end;
|
||||
|
||||
fParameters.Add(TMVCRESTParam.Create(aParamType, aName, aValue));
|
||||
end;
|
||||
|
||||
@ -1694,13 +1702,12 @@ end;
|
||||
|
||||
function TMVCRESTClient.UserAgent(const aUserAgent: string): IMVCRESTClient;
|
||||
begin
|
||||
Result := Self;
|
||||
fUserAgent := aUserAgent;
|
||||
Result := AddHeader(sUserAgent, aUserAgent);
|
||||
end;
|
||||
|
||||
function TMVCRESTClient.UserAgent: string;
|
||||
begin
|
||||
Result := fUserAgent;
|
||||
Result := HeaderValue(sUserAgent);
|
||||
end;
|
||||
|
||||
{ TMVCRESTResponse }
|
||||
|
Loading…
Reference in New Issue
Block a user