Imports System.Data.SqlClient Imports System.Net Imports System.Reflection Imports System.Threading Imports Chilkat Imports Newtonsoft.Json.Linq Imports VERAG_PROG_ALLGEMEIN.cCreditSafeAPI Public Class cDigicust Public Property session_Id As Integer Public Property token As String Public Property datetime As DateTime Public Property refresh_token As String Public Property token_validuntil As DateTime Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL Public hasEntry As Boolean Function getParameterList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("session_Id", session_Id, , True, True)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("token", token)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("datetime", datetime)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("refresh_token", refresh_token)) list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("token_validuntil", token_validuntil)) Return list End Function Sub New() LOAD() End Sub Sub New(token As String, refreshtoken As String, timestamp As DateTime) token = Me.token refreshtoken = Me.refresh_token token_validuntil = timestamp SAVE() End Sub Public Function SAVE(Optional errHinweis = "") As Boolean Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList() Dim sqlstr = " BEGIN " & getInsertCmd() & " END " & " commit tran " Dim IdTMP = session_Id IdTMP = SQL.doSQLVarListID(IdTMP, sqlstr, "FMZOLL", , list, , errHinweis) If session_Id <= 0 Then session_Id = IdTMP Return session_Id > 0 End Function Public Function getInsertCmd() As String Try Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList() Dim str As String = "" Dim values As String = "" For Each i In list If Not i.isPrimaryParam Then str &= "[" & i.Text & "]," values &= "@" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & "," End If Next str = str.Substring(0, str.Length - 1) 'wg. ',' values = values.Substring(0, values.Length - 1) 'wg. ',' Return (" INSERT INTO tblDigicust_Sessions (" & str & ") VALUES(" & values & ") ") Catch ex As Exception VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try Return "" End Function Public Sub LOAD() Try hasEntry = False Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL() Using cmd As New SqlCommand("SELECT TOP(1) * FROM tblDigicust_Sessions where token_validuntil >= GetDate() order by datetime desc ", conn) Dim dr = cmd.ExecuteReader() If dr.Read Then For Each li In getParameterList() Dim propInfo As PropertyInfo = Me.GetType.GetProperty(li.Scalarvariable) If dr.Item(li.Text) Is DBNull.Value Then propInfo.SetValue(Me, Nothing) Else propInfo.SetValue(Me, dr.Item(li.Text)) End If Next hasEntry = True End If dr.Close() End Using End Using Catch ex As Exception VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub End Class Public Class cDigicustAPI 'Test 'Shared API_STRING As String = "https://connect.sandbox.creditsafe.com" 'PROD Shared API_STRING As String = "https://api.digicust.com" Shared token As String = "" Shared refresh_token As String = "" Public dataTable As New DataTable() Shared Function SendRequestAuthentificationToken(ByVal myuri As String, ByVal contentType As String, ByVal method As String) As String Try Dim session = New cDigicust() If Not IsDBNull(session.token) AndAlso session.token IsNot Nothing Then Return "token filled" VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat() Dim rest As New Chilkat.Rest Dim success As Boolean Dim bTls As Boolean = True Dim port As Integer = 443 Dim bAutoReconnect As Boolean = True success = rest.Connect(API_STRING, port, bTls, bAutoReconnect) If (success <> True) Then Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason) Debug.WriteLine(rest.LastErrorText) Return rest.LastErrorText End If Dim json As New Chilkat.JsonObject success = json.UpdateString("username", "edv@verag.ag") success = json.UpdateString("password", "hbG&%RhpE6kjV@r") rest.AddHeader("Content-Type", contentType) Dim sbRequestBody As New Chilkat.StringBuilder json.EmitSb(sbRequestBody) Dim sbResponseBody As New Chilkat.StringBuilder Dim ResponseStr = rest.FullRequestSb(method, myuri, sbRequestBody, sbResponseBody) If (rest.ResponseStatusCode <> 200) Then Return rest.ResponseStatusCode & " " & rest.ResponseStatusText End If Dim jsonResp = New Chilkat.JsonObject() jsonResp.LoadSb(sbResponseBody) token = jsonResp.StringOf("access_token") refresh_token = jsonResp.StringOf("refresh_token") Dim now As DateTime = DateTime.Now() now = now.AddSeconds(jsonResp.IntOf("expires_in")) session = New cDigicust(token, refresh_token, now.ToString("yyyy-MM-dd HH:mm:ss")) '"yyyyMMdd HH:mm:ss" Return rest.ResponseStatusCode & " " & rest.ResponseStatusText Catch ex As WebException VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Function Shared Function authenticate(ByRef token As String) As String Dim myUri As String = "/generic/api/auth/authenticate" Dim response = SendRequestAuthentificationToken(myUri, "application/json", "POST") Return response End Function Shared Function SendRequestWithAuthHeader(acceptContentType As String, ByRef failureDesc As String) As String Try VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat() Dim rest As New Chilkat.Rest Dim success As Boolean Dim bTls As Boolean = True Dim port As Integer = 443 Dim bAutoReconnect As Boolean = True success = rest.Connect(API_STRING, port, bTls, bAutoReconnect) If (success <> True) Then Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason) Debug.WriteLine(rest.LastErrorText) failureDesc = rest.LastErrorText Return failureDesc End If rest.AddHeader("Content-Type", "application/json") rest.AddHeader("Authorization", "Bearer " & token) rest.AddHeader("Accept", acceptContentType) Dim sbRequestBody As New Chilkat.StringBuilder 'jsonData.EmitSb(sbRequestBody) 'Debug.WriteLine(sbRequestBody) 'DEBUG-Mode -> no request will be sent! 'rest.DebugMode = True Dim sbResponseBody As New Chilkat.StringBuilder success = rest.FullRequestSb("POST", "/api/integrations/verag", sbRequestBody, sbResponseBody) If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) failureDesc = rest.LastErrorText Return failureDesc Else If (rest.ResponseStatusCode <> 200) Then If rest.ResponseStatusCode = 500 Then Dim jsonResultFailure As New Chilkat.JsonObject jsonResultFailure.LoadSb(sbResponseBody) jsonResultFailure.Emit() failureDesc = jsonResultFailure.StringOf("code") & "-" & jsonResultFailure.StringOf("message") Return failureDesc Else failureDesc = rest.ResponseStatusCode & "-" & rest.ResponseStatusText Return failureDesc End If Else Dim jsonResult As New Chilkat.JsonObject success = jsonResult.LoadSb(sbResponseBody) jsonResult.Emit() Return jsonResult.StringOf("atezId") End If End If Catch ex As WebException VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Function Shared Sub uploadginCase() Dim rest As New Chilkat.Rest Dim success As Boolean ' URL: https://api.digicust.com/upload/api/{customerId}/{projectId}/new Dim bTls As Boolean = True Dim port As Integer = 443 Dim bAutoReconnect As Boolean = True success = rest.Connect("api.digicust.com", port, bTls, bAutoReconnect) If (success <> True) Then Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason) Debug.WriteLine(rest.LastErrorText) Exit Sub End If rest.PartSelector = "1" Dim fileStream1 As New Chilkat.Stream fileStream1.SourceFile = " path to file1.pdf" rest.AddHeader("Content-Disposition", "form-data; name=""files[]""; filename="" path to file1.pdf""") rest.AddHeader("Content-Type", "application/pdf") rest.SetMultipartBodyStream(fileStream1) rest.PartSelector = "2" Dim fileStream2 As New Chilkat.Stream fileStream2.SourceFile = " path to file2.pdf" rest.AddHeader("Content-Disposition", "form-data; name=""files[]""; filename="" path to file2.pdf""") rest.AddHeader("Content-Type", "application/pdf") rest.SetMultipartBodyStream(fileStream2) rest.PartSelector = "3" rest.AddHeader("Content-Disposition", "form-data; name=""classifications""") rest.SetMultipartBodyString("[{""fileName"":""file1.pdf"",""documentType"":""invoice""},{""fileName"":""file2.pdf"",""documentType"":""waybill""}]") rest.PartSelector = "4" rest.AddHeader("Content-Disposition", "form-data; name=""executionStrategyId""") rest.SetMultipartBodyString("{executionStrategyId}") rest.PartSelector = "5" rest.AddHeader("Content-Disposition", "form-data; name=""reference""") rest.SetMultipartBodyString("{reference}") rest.PartSelector = "6" rest.AddHeader("Content-Disposition", "form-data; name=""documents""") rest.SetMultipartBodyString("[{""priority"":""afterNormalization"",""annotatedAggregated"":{}}]") rest.PartSelector = "0" rest.AddHeader("Content-Type", "multipart/form-data") rest.AddHeader("Authorization", "Bearer " & token) rest.AddHeader("Expect", "100-continue") Dim strResponseBody As String = rest.FullRequestMultipart("POST", "/upload/api/{customerId}/{projectId}/new") If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) Exit Sub End If Dim respStatusCode As Integer = rest.ResponseStatusCode Debug.WriteLine("response status code = " & respStatusCode) If (respStatusCode >= 400) Then Debug.WriteLine("Response Status Code = " & respStatusCode) Debug.WriteLine("Response Header:") Debug.WriteLine(rest.ResponseHeader) Debug.WriteLine("Response Body:") Debug.WriteLine(strResponseBody) Exit Sub End If End Sub End Class