Forum Discussion
hoaphan
4 years agoNew Contributor III
REST API USE
Hello OneStream Community, I'm working on how to implement a REST API in OS, but i'm not sure to understand all the steps, i've already read the documentation and i've found it very hard to unders...
- 1 year ago
Ahhh, thank you CoPilot. I got it to work with this code:
Private Function GetSourceDataREST(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Transformer) As DataTable Try Dim strEntityWF As String = api.WorkflowProfile.Name.Split(".")(0) ' Use already encoded credentials (eg.,In Postman) key Dim wdCreds As String = "<CREDENTIALS>" 'Call the REST API Dim wc As New WebClient wc.Encoding = Encoding.UTF8 wc.Headers("cache-control") = "no-cache" wc.Headers("ContentType") = "application/json" wc.Headers("Accept") = "application/json" wc.Headers("Authorization") = "Basic " & wdCreds Dim jsonURL As String = "<AURL>" Dim json As String = wc.DownloadString(jsonurl) Dim pullData As DataTable = ParseJsonToDataTable(json, strEntityWF) 'Close Web connection wc.dispose 'Return the dataset Return pullData Catch ex As Exception Throw ErrorHandler.LogWrite(si, New XFException(si, ex)) End Try End Function Function ParseJsonToDataTable(json As String, EntityName As String) As DataTable Dim dataTable As New DataTable() dataTable.Columns.Add("entity", GetType(String)) dataTable.Columns.Add("displayName", GetType(String)) dataTable.Columns.Add("amount", GetType(Integer)) Dim jObject As JObject = JObject.Parse(json) Dim dataArray As JArray = jObject("employees") For Each item As JObject In dataArray Dim row As DataRow = dataTable.NewRow() row("entity") = EntityName row("displayName") = item("displayName") row("amount") = 1 dataTable.Rows.Add(row) Next Return dataTable End Function
Sai_Maganti
4 years agoContributor II
Here is a connector BR we used to import from workday. You can use this as a starting point/reference and build your BR and use this connector BR in a DataSource. Original code provided by OneStream (courtesy Joakim Kulan)
Imports System
Imports System.Data
Imports System.Data.Common
Imports System.IO
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Linq
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports OneStream.Shared.Common
Imports OneStream.Shared.Wcf
Imports OneStream.Shared.Engine
Imports OneStream.Shared.Database
Imports OneStream.Stage.Engine
Imports OneStream.Stage.Database
Imports OneStream.Finance.Engine
Imports OneStream.Finance.Database
Imports System.Net
Imports System.Web.Script.Serialization
Imports newtonsoft.json
Imports newtonsoft.json.linq
Imports System.Xml
Namespace OneStream.BusinessRule.Connector.FTE_Workday
Public Class MainClass
Public Function Main(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Transformer, ByVal args As ConnectorArgs) As Object
Try
'Get the Field name list or load the data
Select Case args.ActionType
Case Is = ConnectorActionTypes.GetFieldList
'Return Field Name List if using Field List Method to get field list in GetFieldList
'-----------------------------------------------------------
Dim fieldList As List(Of String) = GetFieldList(si, globals, api)
Return fieldList
Case Is = ConnectorActionTypes.GetData
Dim sourceDataREST As DataTable = GetSourceDataREST(si, globals, api)
api.Parser.ProcessDataTable(si, sourceDataREST, False, api.ProcessInfo)
Return Nothing
End Select
Return Nothing
Catch ex As Exception
Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
End Try
End Function
'Create the field list items to get field list (Recommended)
'-----------------------------------------------------------
Private Function GetFieldList(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Transformer) As List(Of String)
Try
Dim fields As New List(Of String)
fields.add("Field1")
fields.add("Field2")
..
fields.add("FieldN")
Return fields
Catch ex As Exception
Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
End Try
End Function
'Create the data load Statement
Private Function GetSourceDataREST(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Transformer) As DataTable
Try
' Dim wdUser As String = ""
' Dim wdPass As String = ""
' Dim wdCreds As String = encodebase64(wdUser & ":" & wdPass)
' Use already encoded credentials (eg.,In Postman) key
Dim wdCreds As String = "<encodedkey>"
'Call the REST API
Dim wc As New WebClient
wc.Encoding = Encoding.UTF8
wc.Headers("cache-control") = "no-cache"
wc.Headers("ContentType") = "application/json"
wc.Headers("Accept") = "application/json"
wc.Headers("Authorization") = "Basic " & wdCreds
Dim jsonURL As String = "<webapiurl>?Format=json&<paramname>=" & <paramvalue>
Dim json As String = wc.DownloadString(jsonurl)
'Convert result in API to table format
' Dim pulldata As DataTable = jsonConvert.DeserializeObject(Of DataTable)(json)
Dim dataSet As DataSet = jsonConvert.DeserializeObject(Of DataSet)(json)
Dim pullData As DataTable = dataSet.Tables(0)
'Close Web connection
wc.dispose
'Return the dataset
Return pullData
Catch ex As Exception
Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
End Try
End Function
End Class
End Namespace
- hoaphan4 years agoNew Contributor III
Ok thanks for the BR, for now what im trying to test is to connect Onestrem to POSTMAN via the URL of my application and i have already a data management sequence created and i want to execute this sequence from POSTMAN, but i can't the URL is invalid in Postman i don't know why
Thanks for your help 🙂- Sai_Maganti4 years agoContributor II
I think the key to this is setting up the one of the Authentication providers OneStream supports. The design and reference guide explains how you set this up. I hope you've already done this.
If you can provide more information like what error message you're getting 400, 404 etc from postman, someone might be able to help you.
- WernerN3 years agoContributor II
Life saving contribution. Thank you so much for sharing your code.
- Sridhar_M3 years agoContributor
Thank you Sai for sharing the snippet. This is really helpful.
I've a question, your using this connector to load directly to a register table ? If yes May I know how your calling this business rule ?
- Sai_Maganti3 years agoContributor II
Sridhar
I used this in a connector BR to import FTE actuals from workday.
Best
Sai
- Sridhar_M3 years agoContributor
Got it, Thank you Sai
- IBK1 year agoNew Contributor III
Hello Sai,
Thank You for sharing this. I have another question on the json payload itself. Would you have an example of the json which has nested namespaces as below: Any help would be greatly appreciated:
{
"A1": null,"B1": 999999,"C1": [888888],"FY": 2023,"D1": "ZZ","E1": "test","F1": 1,"costSummary": [{"A1": 100000.0,"B1":999999},{"C1": 100000.0,"D1":999999}]}This is how we have in Postman right now: But have to make it nested as shown in the example above.
."BaseWebServerUrl": "https://XXX.com/OneStreamWeb",
"ApplicationName": "OneStream NBC",
"DataManagementGroupName": "ABC",
"StepName": "XYZ",
"CustomSubstVarsAsCommaSeparatedPairs":"A1=-66666666|7777777,
B1=888888,C1=70638,D1=99999,D1=,fy=2024,E1=AG,F1=0.00,D1=5"- pvanerp1 year agoNew Contributor II
Hi IBK,
Did you get a json with nested namespaces working? I'm having the same issue.
- IBK1 year agoNew Contributor III
Hi pvanerp,
Yes I did get it to work. What is the exact error you are getting?
In the Business Rule you are looking for each one of Name Value pairs as below and that worked. args.NameValuePairs.XFGetValue("etaPayplanName")
Sample:
"CustomSubstVarsAsCommaSeparatedPairs":"piResponse=https://sage.nia.nih.gov/api/filedownload/16694,applId=10941338,"
Thank You,
Indu
- WernerN1 year agoContributor II
Wonderful, thanks for sharing. I am suddenly an 'expert' in Workday Integration :)
Related Content
- 4 months ago
- 2 years ago
- 4 years ago