Skip to content

Commit

Permalink
Refactor HttpClientHandler and HttpContentImpl to improve variable na…
Browse files Browse the repository at this point in the history
…ming and remove the usage of this for clarity and readability.
  • Loading branch information
ajkauffmann committed Dec 11, 2024
1 parent 160d87f commit b7ef236
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ codeunit 2360 "Http Client Handler" implements "Http Client Handler"
InherentEntitlements = X;
InherentPermissions = X;

procedure Send(HttpClient: HttpClient; HttpRequestMessage: Codeunit "Http Request Message"; var HttpResponseMessage: Codeunit "Http Response Message") Success: Boolean;
procedure Send(CurrHttpClientInstance: HttpClient; HttpRequestMessage: Codeunit "Http Request Message"; var HttpResponseMessage: Codeunit "Http Response Message") Success: Boolean;
var
ResponseMessage: HttpResponseMessage;
begin
Success := HttpClient.Send(HttpRequestMessage.GetHttpRequestMessage(), ResponseMessage);
Success := CurrHttpClientInstance.Send(HttpRequestMessage.GetHttpRequestMessage(), ResponseMessage);
HttpResponseMessage := HttpResponseMessage.Create(ResponseMessage);
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace System.RestClient;

interface "Http Client Handler"
{
procedure Send(HttpClient: HttpClient; HttpRequestMessage: Codeunit "Http Request Message"; var HttpResponseMessage: Codeunit "Http Response Message") Success: Boolean;
procedure Send(CurrHttpClientInstance: HttpClient; HttpRequestMessage: Codeunit "Http Request Message"; var HttpResponseMessage: Codeunit "Http Response Message") Success: Boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ codeunit 2355 "Http Content Impl."
InherentPermissions = X;

var
HttpContent: HttpContent;
CurrHttpContentInstance: HttpContent;
ContentTypeEmptyErr: Label 'The value of the Content-Type header must be specified.';
MimeTypeTextPlainTxt: Label 'text/plain', Locked = true;
MimeTypeTextXmlTxt: Label 'text/xml', Locked = true;
Expand Down Expand Up @@ -114,73 +114,73 @@ codeunit 2355 "Http Content Impl."

procedure AddContentEncoding(ContentEncoding: Text)
var
Headers: HttpHeaders;
ContentHeaders: HttpHeaders;
begin
if not this.HttpContent.GetHeaders(Headers) then begin
this.HttpContent.Clear();
this.HttpContent.GetHeaders(Headers);
if not CurrHttpContentInstance.GetHeaders(ContentHeaders) then begin
CurrHttpContentInstance.Clear();
CurrHttpContentInstance.GetHeaders(ContentHeaders);
end;
Headers.Add('Content-Encoding', ContentEncoding);
ContentHeaders.Add('Content-Encoding', ContentEncoding);
end;

procedure SetHeader(Name: Text; Value: Text)
var
Headers: HttpHeaders;
ContentHeaders: HttpHeaders;
begin
if not this.HttpContent.GetHeaders(Headers) then begin
this.HttpContent.Clear();
this.HttpContent.GetHeaders(Headers);
if not CurrHttpContentInstance.GetHeaders(ContentHeaders) then begin
CurrHttpContentInstance.Clear();
CurrHttpContentInstance.GetHeaders(ContentHeaders);
end;

if Headers.Contains(Name) or Headers.ContainsSecret(Name) then
Headers.Remove(Name);
if ContentHeaders.Contains(Name) or ContentHeaders.ContainsSecret(Name) then
ContentHeaders.Remove(Name);

Headers.Add(Name, Value);
ContentHeaders.Add(Name, Value);
end;

procedure SetHeader(Name: Text; Value: SecretText)
var
Headers: HttpHeaders;
ContentHeaders: HttpHeaders;
begin
if not this.HttpContent.GetHeaders(Headers) then begin
this.HttpContent.Clear();
this.HttpContent.GetHeaders(Headers);
if not CurrHttpContentInstance.GetHeaders(ContentHeaders) then begin
CurrHttpContentInstance.Clear();
CurrHttpContentInstance.GetHeaders(ContentHeaders);
end;

if Headers.Contains(Name) or Headers.ContainsSecret(Name) then
Headers.Remove(Name);
if ContentHeaders.Contains(Name) or ContentHeaders.ContainsSecret(Name) then
ContentHeaders.Remove(Name);

Headers.Add(Name, Value);
ContentHeaders.Add(Name, Value);
end;

procedure GetHttpContent() ReturnValue: HttpContent
begin
ReturnValue := this.HttpContent;
ReturnValue := CurrHttpContentInstance;
end;

procedure AsText() ReturnValue: Text
begin
this.HttpContent.ReadAs(ReturnValue);
CurrHttpContentInstance.ReadAs(ReturnValue);
end;

procedure AsSecretText() ReturnValue: SecretText
begin
this.HttpContent.ReadAs(ReturnValue);
CurrHttpContentInstance.ReadAs(ReturnValue);
end;

procedure AsBlob() ReturnValue: Codeunit "Temp Blob"
var
InStr: InStream;
OutStr: OutStream;
begin
this.HttpContent.ReadAs(InStr);
CurrHttpContentInstance.ReadAs(InStr);
ReturnValue.CreateOutStream(OutStr);
CopyStream(OutStr, InStr);
end;

procedure AsInStream(var InStr: InStream)
begin
this.HttpContent.ReadAs(InStr);
CurrHttpContentInstance.ReadAs(InStr);
end;

procedure AsXmlDocument() ReturnValue: XmlDocument
Expand Down Expand Up @@ -227,26 +227,26 @@ codeunit 2355 "Http Content Impl."

procedure SetContent(Content: Text; ContentType: Text)
begin
this.HttpContent.Clear();
this.HttpContent.WriteFrom(Content);
CurrHttpContentInstance.Clear();
CurrHttpContentInstance.WriteFrom(Content);
if ContentType = '' then
ContentType := MimeTypeTextPlainTxt;
SetContentTypeHeader(ContentType);
end;

procedure SetContent(Content: SecretText; ContentType: Text)
begin
this.HttpContent.Clear();
this.HttpContent.WriteFrom(Content);
CurrHttpContentInstance.Clear();
CurrHttpContentInstance.WriteFrom(Content);
if ContentType = '' then
ContentType := MimeTypeTextPlainTxt;
SetContentTypeHeader(ContentType);
end;

procedure SetContent(Content: InStream; ContentType: Text)
begin
this.HttpContent.Clear();
this.HttpContent.WriteFrom(Content);
CurrHttpContentInstance.Clear();
CurrHttpContentInstance.WriteFrom(Content);
if ContentType = '' then
ContentType := MimeTypeApplicationOctetStreamTxt;
SetContentTypeHeader(ContentType);
Expand Down Expand Up @@ -282,7 +282,7 @@ codeunit 2355 "Http Content Impl."

procedure SetContent(var Value: HttpContent)
begin
this.HttpContent := Value;
CurrHttpContentInstance := Value;
end;

local procedure ThrowInvalidJsonException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ codeunit 2353 "Http Request Message Impl."
InherentPermissions = X;

var
HttpRequestMessage: HttpRequestMessage;
CurrHttpRequestMessageInstance: HttpRequestMessage;

procedure Create(Method: Enum "Http Method"; RequestUri: Text; Content: Codeunit "Http Content"): Codeunit "Http Request Message Impl."
begin
Expand All @@ -24,7 +24,7 @@ codeunit 2353 "Http Request Message Impl."

procedure SetHttpMethod(Method: Text)
begin
HttpRequestMessage.Method := Method;
CurrHttpRequestMessageInstance.Method := Method;
end;

procedure SetHttpMethod(Method: Enum "Http Method")
Expand All @@ -34,128 +34,128 @@ codeunit 2353 "Http Request Message Impl."

procedure GetHttpMethod() ReturnValue: Text
begin
ReturnValue := HttpRequestMessage.Method;
ReturnValue := CurrHttpRequestMessageInstance.Method;
end;

procedure SetRequestUri(Uri: Text)
begin
HttpRequestMessage.SetRequestUri(Uri);
CurrHttpRequestMessageInstance.SetRequestUri(Uri);
end;

procedure GetRequestUri() Uri: Text
begin
Uri := HttpRequestMessage.GetRequestUri();
Uri := CurrHttpRequestMessageInstance.GetRequestUri();
end;

procedure SetHeader(HeaderName: Text; HeaderValue: Text)
var
HttpHeaders: HttpHeaders;
RequestHttpHeaders: HttpHeaders;
begin
HttpRequestMessage.GetHeaders(HttpHeaders);
if HttpHeaders.Contains(HeaderName) or HttpHeaders.ContainsSecret(HeaderName) then
HttpHeaders.Remove(HeaderName);
HttpHeaders.Add(HeaderName, HeaderValue);
CurrHttpRequestMessageInstance.GetHeaders(RequestHttpHeaders);
if RequestHttpHeaders.Contains(HeaderName) or RequestHttpHeaders.ContainsSecret(HeaderName) then
RequestHttpHeaders.Remove(HeaderName);
RequestHttpHeaders.Add(HeaderName, HeaderValue);
end;

procedure SetHeader(HeaderName: Text; HeaderValue: SecretText)
var
HttpHeaders: HttpHeaders;
RequestHttpHeaders: HttpHeaders;
begin
HttpRequestMessage.GetHeaders(HttpHeaders);
if HttpHeaders.Contains(HeaderName) or HttpHeaders.ContainsSecret(HeaderName) then
HttpHeaders.Remove(HeaderName);
HttpHeaders.Add(HeaderName, HeaderValue);
CurrHttpRequestMessageInstance.GetHeaders(RequestHttpHeaders);
if RequestHttpHeaders.Contains(HeaderName) or RequestHttpHeaders.ContainsSecret(HeaderName) then
RequestHttpHeaders.Remove(HeaderName);
RequestHttpHeaders.Add(HeaderName, HeaderValue);
end;

procedure GetHeaders() ReturnValue: HttpHeaders
begin
HttpRequestMessage.GetHeaders(ReturnValue);
CurrHttpRequestMessageInstance.GetHeaders(ReturnValue);
end;

procedure GetHeaderValue(HeaderName: Text) Value: Text
var
HttpHeaders: HttpHeaders;
RequestHttpHeaders: HttpHeaders;
Values: List of [Text];
begin
HttpRequestMessage.GetHeaders(HttpHeaders);
if HttpHeaders.Contains(HeaderName) then begin
HttpHeaders.GetValues(HeaderName, Values);
CurrHttpRequestMessageInstance.GetHeaders(RequestHttpHeaders);
if RequestHttpHeaders.Contains(HeaderName) then begin
RequestHttpHeaders.GetValues(HeaderName, Values);
if Values.Count > 0 then
Value := Values.Get(1);
end;
end;

procedure GetHeaderValues(HeaderName: Text) Values: List of [Text]
var
HttpHeaders: HttpHeaders;
RequestHttpHeaders: HttpHeaders;
begin
HttpRequestMessage.GetHeaders(HttpHeaders);
if HttpHeaders.Contains(HeaderName) then
HttpHeaders.GetValues(HeaderName, Values);
CurrHttpRequestMessageInstance.GetHeaders(RequestHttpHeaders);
if RequestHttpHeaders.Contains(HeaderName) then
RequestHttpHeaders.GetValues(HeaderName, Values);
end;

procedure GetSecretHeaderValues(HeaderName: Text) Values: List of [SecretText]
var
HttpHeaders: HttpHeaders;
RequestHttpHeaders: HttpHeaders;
begin
HttpRequestMessage.GetHeaders(HttpHeaders);
if HttpHeaders.ContainsSecret(HeaderName) then
HttpHeaders.GetSecretValues(HeaderName, Values);
CurrHttpRequestMessageInstance.GetHeaders(RequestHttpHeaders);
if RequestHttpHeaders.ContainsSecret(HeaderName) then
RequestHttpHeaders.GetSecretValues(HeaderName, Values);
end;

procedure SetCookie(Name: Text; Value: Text) Success: Boolean
begin
Success := HttpRequestMessage.SetCookie(Name, Value);
Success := CurrHttpRequestMessageInstance.SetCookie(Name, Value);
end;

procedure SetCookie(Cookie: Cookie) Success: Boolean
procedure SetCookie(TheCookie: Cookie) Success: Boolean
begin
Success := HttpRequestMessage.SetCookie(Cookie);
Success := CurrHttpRequestMessageInstance.SetCookie(TheCookie);
end;

procedure GetCookieNames() CookieNames: List of [Text]
begin
CookieNames := HttpRequestMessage.GetCookieNames();
CookieNames := CurrHttpRequestMessageInstance.GetCookieNames();
end;

procedure GetCookies() Cookies: List of [Cookie]
var
CookieName: Text;
Cookie: Cookie;
TheCookie: Cookie;
begin
foreach CookieName in HttpRequestMessage.GetCookieNames() do begin
HttpRequestMessage.GetCookie(CookieName, Cookie);
Cookies.Add(Cookie);
foreach CookieName in CurrHttpRequestMessageInstance.GetCookieNames() do begin
CurrHttpRequestMessageInstance.GetCookie(CookieName, TheCookie);
Cookies.Add(TheCookie);
end;
end;

procedure GetCookie(Name: Text) ReturnValue: Cookie
begin
if HttpRequestMessage.GetCookie(Name, ReturnValue) then;
if CurrHttpRequestMessageInstance.GetCookie(Name, ReturnValue) then;
end;

procedure GetCookie(Name: Text; var Cookie: Cookie) Success: Boolean
procedure GetCookie(Name: Text; var TheCookie: Cookie) Success: Boolean
begin
Success := HttpRequestMessage.GetCookie(Name, Cookie);
Success := CurrHttpRequestMessageInstance.GetCookie(Name, TheCookie);
end;

procedure RemoveCookie(Name: Text) Success: Boolean
begin
Success := HttpRequestMessage.RemoveCookie(Name);
Success := CurrHttpRequestMessageInstance.RemoveCookie(Name);
end;

procedure SetHttpRequestMessage(var RequestMessage: HttpRequestMessage)
begin
HttpRequestMessage := RequestMessage;
CurrHttpRequestMessageInstance := RequestMessage;
end;

procedure SetContent(HttpContent: Codeunit "Http Content")
begin
HttpRequestMessage.Content := HttpContent.GetHttpContent();
CurrHttpRequestMessageInstance.Content := HttpContent.GetHttpContent();
end;

procedure GetRequestMessage() ReturnValue: HttpRequestMessage
begin
ReturnValue := HttpRequestMessage;
ReturnValue := CurrHttpRequestMessageInstance;
end;
}
Loading

0 comments on commit b7ef236

Please sign in to comment.