Imports System.IO Imports DigitalData.Modules.Interfaces Imports DigitalData.Modules.Jobs Imports DigitalData.Modules.Config Imports DigitalData.Modules.Logging Imports Newtonsoft.Json.Linq Imports System.Collections.Generic Imports System.Linq Imports System.Text.RegularExpressions Public Class GraphQLJob Inherits JobBase Implements IJob(Of GraphQLArgs) Public Sub New(LogConfig As LogConfig) MyBase.New(LogConfig, Nothing, Nothing) End Sub Public Sub Start(Args As GraphQLArgs) Implements IJob(Of GraphQLArgs).Start Try Dim oConfigPath As String = Args.QueryConfigPath Dim oConfigManager As New ConfigManager(Of GraphQLConfig)(_LogConfig, oConfigPath) Dim oInterface As GraphQLInterface With oConfigManager.Config oInterface = New GraphQLInterface(_LogConfig, .BaseUrl, .Email, .Password, .CertificateFingerprint) End With ' Login to get cookie _Logger.Debug("Logging in") Dim oLoginResponse = oInterface.Login() ' save cookie for future requests oInterface.SaveCookies(oLoginResponse.Cookies.Item(0)) _Logger.Debug("Getting the data") For Each oQuery As GraphQLConfig.Query In oConfigManager.Config.Queries Dim oCurrentQuery = oQuery Dim oDatabase As New Database.MSSQLServer(_LogConfig, oQuery.ConnectionString) ' get the data Dim oDataResponse = oInterface.GetData(oCurrentQuery.QueryString, oCurrentQuery.OperationName) Dim oResult As String ' write data to string Using oStream = oDataResponse.GetResponseStream() Using oReader As New StreamReader(oStream) oResult = oReader.ReadToEnd() End Using End Using If IsNothing(HandleResponse(oResult, oCurrentQuery, oDatabase)) Then Continue For End If Next ' logout _Logger.Debug("Logging out") Dim oLogoutResponse = oInterface.Logout() Catch ex As Exception _Logger.Error(ex) Throw ex End Try End Sub Private Function HandleResponse(JsonString As String, QueryData As GraphQLConfig.Query, DB As Database.MSSQLServer) Dim oObj As JObject = JObject.Parse(JsonString) Dim oResultList = oObj.SelectToken(QueryData.MappingBasePath) Dim oMappings = QueryData.MappingFields If oResultList Is Nothing Then _Logger.Warn("HandleResponse: Could not find BasePath: [{0}] for query [{1}]", QueryData.MappingBasePath, QueryData.Name) Return Nothing End If _Logger.Info("Processing Queue [{0}] with [{1}] Items", QueryData.Name, oResultList.Count) If QueryData.ClearBeforeFill Then _Logger.Info("Clearing Table {0} before insert", QueryData.DestinationTable) Try DB.ExecuteNonQuery($"DELETE FROM {QueryData.DestinationTable}") Catch ex As Exception _Logger.Error(ex) End Try End If For Each oResultItem In oResultList Try Dim oValues As New List(Of String) Dim oKeys As New List(Of String) For Each oMapping In oMappings Dim oToken = oResultItem.SelectToken(oMapping.SourcePath) If oToken Is Nothing Then _Logger.Warn("HandleResponse: Could not find SourcePath: {0}", oMapping.SourcePath) End If oValues.Add(oToken.ToString()) oKeys.Add(oMapping.DestinationColumn) Next Dim oColumnValues = oValues. Select(Function(Value) Regex.Replace(Value, "'", "''")). Select(Function(Value) $"'{Value}'"). ToList() Dim oValueString = String.Join(",", oColumnValues) Dim oColumns = String.Join(",", oKeys.ToArray) Dim oSQL As String = $"INSERT INTO {QueryData.DestinationTable} ({oColumns}) VALUES ({oValueString})" DB.ExecuteNonQuery(oSQL) Catch ex As Exception _Logger.Error(ex) End Try Next Return QueryData End Function Public Function ShouldStart(Arguments As GraphQLArgs) As Boolean Implements IJob(Of GraphQLArgs).ShouldStart Return Arguments.Enabled End Function End Class