Create a Business Central extension using AL language which handles sending REST API requests
Support for Bearer and OAuth authentications, all authentication is stored in a seperate table and referenced by the API configuration
Support for mapping the response to any table using a reference to Mapping table which holds all information to map the JSON response to the specified table
Use the ID range starting from 9030 and higher. Each file must start with the prefix XTV, match the object name, and be placed in its respective folder type (Page, Page Ext, Codeunits). Only provide the file names followed by their source code, with no additional comments.
codeunit 9034 "XTV Rest API Handler"
{
Caption = 'XTV Rest API Handler';
trigger OnRun()
begin
// Entry point if needed
end;
procedure SendRequest(APIConfigID: Integer)
var
APIConfig: Record "XTV API Configuration";
AuthConfig: Record "XTV Authentication";
Client: HttpClient;
Response: HttpResponseMessage;
OAuthResponse: HttpResponseMessage;
OAuthContent: HttpContent;
OAuthJsonObject: JsonObject;
OAuthToken: Text;
JsonText: Text;
JsonObject: JsonObject;
Mapping: Record "XTV Mapping";
MappingLine: Record "XTV Mapping Line";
TargetRecRef: RecordRef;
TargetFieldRef: FieldRef;
FieldNo: Integer;
JSONPathValue: Text;
begin
APIConfig.Get(APIConfigID);
AuthConfig.Get(APIConfig."Authentication Reference");
Client.DefaultRequestHeaders.Clear();
case AuthConfig."Authentication Type" of
AuthConfig."Authentication Type"::Bearer:
begin
Client.DefaultRequestHeaders.Authorization := 'Bearer ' + AuthConfig."Bearer Token";
end;
AuthConfig."Authentication Type"::OAuth:
begin
OAuthContent.WriteFrom('grant_type=client_credentials&client_id=' + Uri.EscapeDataString(AuthConfig."OAuth Client ID") +
'&client_secret=' + Uri.EscapeDataString(AuthConfig."OAuth Client Secret") +
'&scope=' + Uri.EscapeDataString(AuthConfig."OAuth Scope"));
OAuthContent.GetHeaders('Content-Type').Clear();
OAuthContent.GetHeaders('Content-Type').Add('application/x-www-form-urlencoded');
Client.Post(AuthConfig."OAuth Token Endpoint", OAuthContent, OAuthResponse);
if OAuthResponse.IsSuccessStatusCode() then begin
OAuthResponse.Content().ReadAs(JsonText);
OAuthJsonObject.ReadFrom(JsonText);
OAuthToken := OAuthJsonObject.GetValue('access_token').AsValue().AsText();
Client.DefaultRequestHeaders.Authorization := 'Bearer ' + OAuthToken;
end else
Error('Failed to retrieve OAuth token. Status Code: %1', OAuthResponse.HttpStatusCode());
end;
end;
case APIConfig."HTTP Method" of
APIConfig."HTTP Method"::GET:
begin
Client.Get(APIConfig."Base URL" + APIConfig.Endpoint, Response);
end;
APIConfig."HTTP Method"::POST:
begin
// Implement POST logic here
end;
APIConfig."HTTP Method"::PUT:
begin
// Implement PUT logic here
end;
APIConfig."HTTP Method"::DELETE:
begin
// Implement DELETE logic here
end;
end;
if Response.IsSuccessStatusCode() then begin
Response.Content().ReadAs(JsonText);
JsonObject.ReadFrom(JsonText);
Mapping.Get(APIConfig."Mapping Reference");
MappingLine.SetRange("Mapping ID", Mapping.ID);
if TargetRecRef.Open(Mapping."Target Table ID") then begin
if MappingLine.FindSet() then
repeat
FieldNo := MappingLine."Field No.";
if TargetRecRef.FieldExists(FieldNo) then begin
TargetFieldRef := TargetRecRef.Field(FieldNo);
JSONPathValue := GetJSONValue(JsonObject, MappingLine."JSON Path");
TargetFieldRef.Value := JSONPathValue;
end;
until MappingLine.Next() = 0;
TargetRecRef.Insert();
end;
end else
Error('Failed to get a successful response. Status Code: %1', Response.HttpStatusCode());
end;
local procedure GetJSONValue(JsonObject: JsonObject; JSONPath: Text) Result: Text
var
JsonToken: JsonToken;
begin
if JsonObject.SelectToken(JSONPath, JsonToken) then
Result := JsonToken.ToValue().AsText()
else
Result := '';
end;
}