Forum Discussion

hoaphan's avatar
hoaphan
New Contributor III
4 years ago

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...
  • pvanerp's avatar
    pvanerp
    22 days 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