Skip to content

Commit

Permalink
[Rest Client] Support for Cookies + Fix secret headers + Collectible …
Browse files Browse the repository at this point in the history
…errors (#2474)

<!-- Thank you for submitting a Pull Request. If you're new to
contributing to BCApps please read our pull request guideline below
* https://github.com/microsoft/BCApps/Contributing.md
-->
#### Summary <!-- Provide a general summary of your changes -->
Adding support for cookies to the Rest Client
Fixing an issue with secret headers
Adding collectible error behavior
Redesigning internal code for better maintainability and uptaking new AL
features like the keyword `this`

#### Work Item(s) <!-- Add the issue number here after the #. The issue
needs to be open and approved. Submitting PRs with no linked issues or
unapproved issues is highly discouraged. -->
Fixes #1479 #989 


Fixes
[AB#560010](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/560010)

---------

Co-authored-by: Jesper Schulz-Wedde <[email protected]>
  • Loading branch information
ajkauffmann and JesperSchulz authored Dec 20, 2024
1 parent 0e9b78e commit 8f215cb
Show file tree
Hide file tree
Showing 23 changed files with 1,575 additions and 299 deletions.
3 changes: 1 addition & 2 deletions src/System Application/App/Rest Client/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@
"idRanges": [
{
"from": 2350,
"to": 2361
"to": 2362
}
],
"target": "OnPrem",
"runtime": "12.0",
"resourceExposurePolicy": {
"allowDebugging": true,
"allowDownloadingSource": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace System.RestClient;

codeunit 2361 "HttpAuthOAuthClientCredentials" implements "Http Authentication"
{
Access = Public;
InherentEntitlements = X;
InherentPermissions = X;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace System.RestClient;
/// <summary>Implementation of the "Http Authentication" interface for a anonymous request.</summary>
codeunit 2358 "Http Authentication Anonymous" implements "Http Authentication"
{
Access = Public;
InherentEntitlements = X;
InherentPermissions = X;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using System;

codeunit 2359 "Http Authentication Basic" implements "Http Authentication"
{
Access = Public;
InherentEntitlements = X;
InherentPermissions = X;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace System.RestClient;

/// <summary>
/// This enum contains the exceptions of the Rest Client.
/// </summary>
enum 2351 "Rest Client Exception"
{
/// <summary>
/// Specifies that the exception is an unknown exception.
/// </summary>
value(100; UnknownException)
{
Caption = 'Unknown Exception';
}
/// <summary>
/// Specifies that the connection failed.
/// </summary>
value(101; ConnectionFailed)
{
Caption = 'Connection Failed';
}
/// <summary>
/// Specifies that the request is blocked by the environment.
/// </summary>
value(102; BlockedByEnvironment)
{
Caption = 'Blocked By Environment';
}
/// <summary>
/// Specifies that the request failed.
/// </summary>
value(103; RequestFailed)
{
Caption = 'Request Failed';
}
/// <summary>
/// Specifies that the content is not valid JSON.
/// </summary>
value(201; InvalidJson)
{
Caption = 'Invalid Json';
}
/// <summary>
/// Specifies that the content is not valid XML.
/// </summary>
value(202; InvalidXml)
{
Caption = 'Invalid Xml';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace System.RestClient;

codeunit 2362 "Rest Client Exception Builder"
{
Access = Public;
InherentEntitlements = X;
InherentPermissions = X;

/// <summary>
/// Creates an exception with the specified error code and message. The exception is collectible if the errors are currently being collected.
/// </summary>
/// <param name="RestClientException">The error code for the exception.</param>
/// <param name="ErrorMessage">The message for the exception.</param>
/// <returns>The exception with the specified error code and message.</returns>
procedure CreateException(RestClientException: Enum "Rest Client Exception"; ErrorMessage: Text) Exception: ErrorInfo
begin
Exception := CreateException(RestClientException, ErrorMessage, IsCollectingErrors());
end;

/// <summary>
/// Creates an exception with the specified error code, message, and collectible flag.
/// </summary>
/// <param name="RestClientException">The error code for the exception.</param>
/// <param name="ErrorMessage">The message for the exception.</param>
/// <param name="Collectible">Whether the exception is collectible.</param>
/// <returns>The exception with the specified error code, message, and collectible flag.</returns>
procedure CreateException(RestClientException: Enum "Rest Client Exception"; ErrorMessage: Text; Collectible: Boolean) Exception: ErrorInfo
begin
Exception.Message := ErrorMessage;
Exception.CustomDimensions.Add('ExceptionCode', Format(RestClientException.AsInteger()));
Exception.CustomDimensions.Add('ExceptionName', RestClientException.Names.Get(RestClientException.Ordinals.IndexOf(RestClientException.AsInteger())));
Exception.Collectible := Collectible;
end;

/// <summary>
/// Gets the exception code from the error info.
/// </summary>
/// <param name="ErrInfo">The error info of the exception.</param>
/// <returns>The exception code.</returns>
procedure GetRestClientException(ErrInfo: ErrorInfo) RestClientException: Enum "Rest Client Exception"
var
ExceptionCode: Integer;
begin
Evaluate(ExceptionCode, ErrInfo.CustomDimensions.Get('ExceptionCode'));
RestClientException := Enum::"Rest Client Exception".FromInteger(ExceptionCode);
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ namespace System.RestClient;

codeunit 2360 "Http Client Handler" implements "Http Client Handler"
{
Access = Public;
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);
HttpResponseMessage.SetResponseMessage(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;
}
Loading

0 comments on commit 8f215cb

Please sign in to comment.