Monday, May 25, 2020

Business Central Integration with Rest API – OnPrem - Part 2

Dear Reader’s, I am sharing simple REST API integration with Business Central 2020 Wave 1 release. This is based on Part 1, where I have used both Codeunit & DotNet. Here I am using only codeunits to simulate same thing without DotNet variables.

Thanks to "Arend-Jan Kauffmann" reply where he has mentioned why you have used DotNet where it all will be handled by AL Data Types & Methods (Wrapper Classes). He has also share detailed explanation in his blog. So, inspired and reconstructed my Part 1 into two samples:

Example 1: (Sample 1) Used codeunits to handle the request & response without any DotNet variable combination.

Example 2: (Sample 2) Used AL Data Types & Methods (Wrapper Classes) to handle the request & response without using any Codeunit & DotNet varibale. This sample will run on SaaS model as well.

Example 1: Below are example of calling API without using DotNet variable:

codeunit 50102 "Json API Test2"
{
    trigger OnRun()
    begin
        // Plesase fix below line in single line
        Url := 'http://samples.openweathermap.org/data/2.5/weather?zip=110001,in&
                        appid=b6907d289e10d714a6e88b30761fae22';
        HttpWebRequestMgt.Initialize(Url);
        HttpWebRequestMgt.DisableUI();
        HttpWebRequestMgt.SetMethod('GET');
        HttpWebRequestMgt.SetReturnType('application/json');

        Clear(TempBlob);
        TempBlob.CreateInStream(InStr);

        IF HttpWebRequestMgt.GetResponseStream(InStr) then begin

            InStr.ReadText(Json, MaxStrLen(Json));
            Message(Json);

            IF JSONMgmt.InitializeFromString(Json) then begin
                Temperature := JSONMgmt.GetValue('main.temp');
                Pressure := JSONMgmt.GetValue('main.pressure');
                Humidity := JSONMgmt.GetValue('main.humidity');
                TempMin := JSONMgmt.GetValue('main.temp_min');
                TempMax := JSONMgmt.GetValue('main.temp_max');
            end;

            MESSAGE(Text001, Temperature, Pressure, Humidity, TempMin, TempMax);
        end;
    end;

    var
        HttpWebRequestMgt: Codeunit "Http Web Request Mgt.";
        Url: Text;
        Json: Text;
        TempBlob: Codeunit "Temp Blob";
        InStr: InStream;
        Temperature: Text;
        Pressure: Text;
        Humidity: Text;
        TempMin: Text;
        TempMax: Text;
        JSONMgmt: Codeunit "JSON Management";
        // Plesase fix below line in single line
        Text001: Label 'Temperature: %1 \Pressure: %2 \Humidity: %3 
                            \Min Temperature: %4 \Max  Temperature: %5';
}


Example 2: (Sample 2) Using AL Data Types & Methods (Wrapper Classes) 

codeunit 50104 "Json API Test4"
{
    trigger OnRun()
    begin
        
        // Plesase fix below line in single line
        Url := 'http://samples.openweathermap.org/data/2.5/weather?zip=110001,in
                        &appid=b6907d289e10d714a6e88b30761fae22';
        HttpHeader := HttpWebClient.DefaultRequestHeaders;
        HttpHeader.Add('Accept''application/json');
        IF HttpWebClient.Get(Url, HttpWebResponse) then
            IF HttpWebResponse.IsSuccessStatusCode then begin
                HttpWebResponse.Content.ReadAs(Json);
                Message(Json);
                JObject.ReadFrom(Json);
                IF JObject.SelectToken('main.temp', JToken) then
                    Temperature := JToken.AsValue().AsText();
                IF JObject.SelectToken('main.pressure', JToken) then
                    Pressure := JToken.AsValue().AsText();
                IF JObject.SelectToken('main.humidity', JToken) then
                    Humidity := JToken.AsValue().AsText();
                IF JObject.SelectToken('main.temp_min', JToken) then
                    TempMin := JToken.AsValue().AsText();
                IF JObject.SelectToken('main.temp_max', JToken) then
                    TempMax := JToken.AsValue().AsText();

                MESSAGE(Text001, Temperature, Pressure, Humidity, TempMin, TempMax);
            end;
    end;

    var
        Url: Text;
        Json: Text;
        Temperature: Text;
        Pressure: Text;
        Humidity: Text;
        TempMin: Text;
        TempMax: Text;
        HttpWebClient: HttpClient;
        HttpHeader: HttpHeaders;
        HttpWebResponse: HttpResponseMessage;
        JObject: JsonObject;
        JToken: JsonToken;
// Plesase fix below line in single line
        Text001: Label 'Using Wrapper Class:\Temperature: %1 \Pressure: %2 
            \Humidity: %3 \Min Temperature: %4 \Max  Temperature: %5';
}

Stay tuned, next blog is coming soon...

No comments:

Post a Comment