Files
SDL/VERAG_PROG_ALLGEMEIN/Schnittstellen/cCreditSafeAPI.vb

326 lines
11 KiB
VB.net

Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Web.UI.WebControls
Imports System.Windows.Forms
Imports Microsoft.Office.Interop.Outlook
Imports Newtonsoft.Json
Imports Spire.Pdf.Lists
Imports VERAG_PROG_ALLGEMEIN.IDEV_Intrastat
Imports VERAG_PROG_ALLGEMEIN.TESTJSON
Public Class cCreditSafeAPI
'Test
Shared API_STRING As String = "https://connect.sandbox.creditsafe.com"
'PROD
'Shared API_STRING As String = "https://connect.creditsafe.com"
Shared token As String = ""
Public dataTable As New DataTable()
Shared Function SendRequestAuthentificationToken(myuri As String, user As CreditSafeUser, contentType As String, method 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)
Return rest.LastErrorText
End If
Dim json As New Chilkat.JsonObject
json.UpdateString("username", user.username)
json.UpdateString("password", user.password)
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
End If
Dim jsonResp = New Chilkat.JsonObject()
jsonResp.LoadSb(sbResponseBody)
token = jsonResp.StringOf("token")
Return rest.ResponseStatusCode
Catch ex As WebException
VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
End Try
End Function
Shared Function SendGetRequestWithAuthHeader(url As String, company As Company, acceptContentType As String, method As String, authenticationToken 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)
End If
rest.ClearAllQueryParams()
If company IsNot Nothing Then
setSearchParam(rest, company)
End If
rest.AddHeader("Content-Type", "application/json")
rest.AddHeader("Authorization", "Bearer " & authenticationToken)
rest.AddHeader("Accept", acceptContentType)
Dim responseJson As String = rest.FullRequestNoBody(method, url)
If (rest.LastMethodSuccess <> True) Then
Debug.WriteLine(rest.LastErrorText)
MsgBox(rest.LastErrorText)
End If
Return responseJson
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(username As String, passwort As String) As String
Dim myUri As String = API_STRING & "/v1/authenticate"
Dim csUser = New CreditSafeUser(username, passwort)
Dim response = SendRequestAuthentificationToken(myUri, csUser, "application/json", "POST")
Return response
End Function
Shared Function searchCompanies(company As Company, ByRef dataTable As DataTable) As String
VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()
Dim myUrl As String = API_STRING & "/v1/companies"
Dim jsonRespString = SendGetRequestWithAuthHeader(myUrl, company, "application/json", "GET", token)
'Dim test As String = "{""correlationId"":""9194edd0-1a4a-4520-a332-7306b7c027ca"",""totalSize"":1,""companies"":[{""id"":""AT-X-7256130"",""country"":""AT"",""regNo"":""FN 410356 s"",""vatNo"":[""ATU 68490714""],""safeNo"":""AT05607514"",""name"":""Imex Customs Service GmbH"",""address"":{""simpleValue"":""Autobahngrenze 15, 4975, Suben"",""street"":""Autobahngrenze 15"",""city"":""Suben"",""postCode"":""4975""},""status"":""active"",""officeType"":""headOffice"",""type"":""Ltd"",""dateOfLatestAccounts"":""2021-12-31T00:00:00.000000Z"",""dateOfLatestChange"":""2023-03-03T02:38:30.000Z""}]}"
'jsonRespString = test
Dim json As New Chilkat.JsonObject
Dim success As Boolean = json.Load(jsonRespString)
If (success <> True) Then
Debug.WriteLine(json.LastErrorText)
Return "Verbindungsfehler"
End If
Dim num As Integer = json.SizeOfArray("companies")
If num = 0 Then
Return json.StringOf("messages[0].text")
End If
Dim companies As Chilkat.JsonArray = json.ArrayOf("companies")
If (json.LastMethodSuccess = False) Then
Return "companies member not found."
End If
Dim numCompanies As Integer = companies.Size
Dim j As Integer = 0
While j < numCompanies
Dim compObj As Chilkat.JsonObject = companies.ObjectAt(j)
Dim index = companies.FindString("address", False)
Dim adressObj As Chilkat.JsonObject = compObj.ObjectOf("address")
Dim dateTime As New Chilkat.CkDateTime
Dim dt As New Chilkat.DtObj
Dim getAsLocal As Boolean = False
success = compObj.DateOf("dateOfLatestChange", dateTime)
Debug.WriteLine(dateTime.GetAsTimestamp(getAsLocal))
'Debug.WriteLine(adressObj.StringOf("simpleValue"))
'Debug.WriteLine(compObj.StringOf("id") & " " & compObj.StringOf("phoneNo") & " " & compObj.StringOf("phoneNumbers[0]"))
Dim R As DataRow = dataTable.NewRow
R("id") = compObj.StringOf("id")
R("name") = compObj.StringOf("name")
R("country") = compObj.StringOf("country")
R("safeNo") = compObj.StringOf("safeNo")
R("vatNo") = compObj.StringOf("vatNo[0]")
R("regNo") = compObj.StringOf("regNo")
R("street") = adressObj.StringOf("street")
R("city") = adressObj.StringOf("city")
R("postCode") = adressObj.StringOf("postCode")
R("status") = compObj.StringOf("status")
R("dateOfLatestChange") = dateTime.GetAsTimestamp(getAsLocal)
R("phoneNo") = compObj.StringOf("phoneNumbers[0]")
dataTable.Rows.Add(R)
j = j + 1
End While
Return "Anzahl gefundener Datensätze: " & numCompanies
End Function
Shared Function getReport(company As Company, withPDF As Boolean) As String
Dim myUrl As String = API_STRING & "/v1/companies/" & company.creditSafeId
Dim acceptContentType = "application/json"
If withPDF Then
acceptContentType &= "+pdf"
End If
Dim jsonRespString = SendGetRequestWithAuthHeader(myUrl, Nothing, acceptContentType, "GET", token)
Dim json As New Chilkat.JsonObject
Dim success As Boolean = json.Load(jsonRespString)
If (success <> True) Then
Debug.WriteLine(json.LastErrorText)
Return "Verbindungsfehler"
End If
Dim num As Integer = json.SizeOfArray("companies")
If num = -1 Then
Return json.StringOf("details")
End If
Return "test"
End Function
Shared Sub setSearchParam(ByRef rest As Chilkat.Rest, ByRef company As Company)
rest.AddQueryParam("countries", company.country)
If company.creditsafeNo <> "" Then
rest.AddQueryParam("safeNo", company.creditsafeNo)
Else
If company.vatNo <> "" Then
rest.AddQueryParam("vatNo", company.vatNo)
Else
If company.name <> "" Then
rest.AddQueryParam("name", company.name)
End If
End If
End If
End Sub
Shared Function checkDateOfLastRequest(company As Company) As Date
Dim myUrl As String = API_STRING & "/v1/companies"
Dim jsonRespString = SendGetRequestWithAuthHeader(myUrl, company, "application/json", "GET", token)
Dim json As New Chilkat.JsonObject
Dim success As Boolean = json.Load(jsonRespString)
If (success <> True) Then
Debug.WriteLine(json.LastErrorText)
Return "Verbindungsfehler"
End If
Dim companies As Chilkat.JsonArray = json.ArrayOf("companies")
Dim compObj As Chilkat.JsonObject = companies.ObjectAt(0)
Dim dateTime As New Chilkat.CkDateTime
Dim dt As New Chilkat.DtObj
Dim getAsLocal As Boolean = False
success = compObj.DateOf("dateOfLatestChange", dateTime)
Debug.WriteLine(dateTime.GetAsTimestamp(getAsLocal))
Return dateTime.GetAsTimestamp(getAsLocal)
End Function
Public Class CreditSafeUser
Public Property username As String
Public Property password As String
Public Property token As String
Public Sub New(_username As String, _password As String)
username = _username
password = _password
End Sub
Public Sub New(_username As String, _password As String, _token As String)
username = _username
password = _password
End Sub
End Class
Public Class Company
Public Property creditSafeId As String
Public Property name As String
Public Property vatNo As String
Public Property country As String
Public Property creditsafeNo As String
Public Property lastChecked As Date
Public Sub New(_name As String, _vatNo As String, _country As String, _creditsafeNo As String, _creditSafeId As String, _lastChecked As Date)
creditSafeId = _creditSafeId
name = _name
vatNo = _vatNo
country = _country
creditsafeNo = _creditsafeNo
lastChecked = _lastChecked
End Sub
Public Sub New()
End Sub
End Class
End Class