Obtain Data through a WebAPI

In this scenario, you have a WebAPI (IPaaS integration or another accessible REST API) to obtain and pass back data to OneStream. You can use the following remote business rule in Smart Integration Connector to invoke the API. If you have results that are in JSON format, you can convert them to a data table and send them back to OneStream. If the data from the WebAPI is in JSON, you can process the data in Smart Integrator Connector. Additionally, you can send the raw data back as a string to a data management job for further testing.

Direct connections are preferred for this method and can be invoked using business rules within OneStream similar to the SFTP example provided above.

See Multiple WebAPI Connections for best practices on scenarios with multiple WebAPIs.

NOTE: Data transferred over a Direct Connection to a WebAPI is transferred directly over HTTP(S) and not converted to parquet format. OneStream does not control the return format.

Host Headers

Host headers specify the domain name of the server that will receive the request. The Host header is defined in the Business Rule and includes the domain name of the target server and should match what the server expects in incoming requests (for example, api.example.com). See Troubleshooting for additional information.

Copy
// The header must be set or some connections may be refused.    
internalHttpClient.DefaultRequestHeaders.Host = "api.example.com";

Access a Single WebAPIs

To set up a single WebAPI connection:

  1. Set up a Direct Connection.

  2. Export the Configuration and import to your Local Gateway Server. See the Setup and Installation section for more information on this process.

  3. Refresh your connections and verify this new connection is online.

    IMPORTANT: Copy your Bound Port in OneStream. You will reference this later in the extensibility rule.

  4. Create the Extensibility Rule below:

    Copy
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using OneStream.Shared.Common;
    using OneStream.Shared.Database;
    using OneStream.Shared.Engine;
    using OneStream.Shared.Wcf;
    using System.Net;
    using System.Net.Http;
    using Newtonsoft.Json;
    using System.Net.Http.Headers;

    namespace OneStream.BusinessRule.Extender.SIC_WebAPI
    {
        public class MainClass
        {
            private static readonly HttpClient internalHttpClient = new HttpClient();
        
            public object Main(SessionInfo si, BRGlobals globals, object api, ExtenderArgs args)
            {
                try
                {
                    internalHttpClient.DefaultRequestHeaders.Accept.Clear();
             internalHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
         internalHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
         internalHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
                    internalHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
         internalHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
                
                    // The header must be set or some connections maybe refused.    
                    internalHttpClient.DefaultRequestHeaders.Host = "api.example.com";
                    
                    // In this example, 20540 is the Bound Port in OneStream for the Gateway being used.            
                    var stringTask = internalHttpClient.GetStringAsync("https://localhost:20540/v1/forecast?latitude=40.73&longitude=-73.94&daily=temperature_2m_max,temperature_2m_min&temperature_unit=fahrenheit&timezone=America%2FNew_York");
                    
                    // Display the result in the exception dialog as an example.
                    throw new Exception(stringTask.Result);        
                }
                catch (Exception ex)
                {
                    throw ErrorHandler.LogWrite(si, new XFException(si, ex));
                }
            }
        }
    }
  5. Compile and test the business rule. If the extensibility ran successfully, you should see the correct data that corresponds with the business rule in the Exception dialog box.

Access Multiple WebAPIs

If you are using more than one WebAPI, the best practice is to perform this process using a single connection and multiple remote Business Rules.

Use the following OneStream business rule to invoke the request.

Copy
Dim objRemoteRequestResultDto As RemoteRequestResultDto = BRApi.Utilities.ExecRemoteGatewayBusinessRule(si, "RemoteWebAPISample", Nothing, "testconnection",String.Empty) If (objRemoteRequestResultDto.RemoteResultStatus = RemoteMessageResultType.Success) Dim xfDT = New XFDataTable(si,objRemoteRequestResultDto.resultSet,Nothing,1000) End If

Use the following remote business rule to execute the request in C#.

Copy
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
using System.Net.Http.Headers;
namespace OneStream.BusinessRule.SmartIntegrationFunction.RemoteWebAPISample
{
 public class MainClass
 {
 private static readonly HttpClient internalHttpClient = new HttpClient();
 
 static MainClass()
 {
 internalHttpClient.DefaultRequestHeaders.Accept.Clear();
 internalHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
 internalHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
 internalHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
 internalHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
 internalHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); 
 }
 
 public DataTable RunOperation() 
 {
 var stringTask = internalHttpClient.GetStringAsync(https://localhost:44388/WeatherForecast);
 var msg = stringTask;
 DataTable dt = (DataTable)JsonConvert.DeserializeObject(stringTask.Result, (typeof(DataTable)));
 return dt;
 }
 }
}