Fehlerhandling verbessert.

This commit is contained in:
2023-06-19 14:55:23 +02:00
parent 5276cdabdc
commit 8621d57df2
4 changed files with 193 additions and 132 deletions

View File

@@ -2,6 +2,7 @@
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Web.UI.WebControls
Imports System.Windows.Forms
Imports Chilkat
@@ -123,10 +124,20 @@ Public Class cCreditSafeAPI
Return failureDesc
Else
If (rest.ResponseStatusCode <> 200) Then
Dim sbErrorText As New Chilkat.StringBuilder
sbErrorText.AppendBd(pdfData, "utf-8", 0, 0)
failureDesc = sbErrorText.GetAsString()
Return failureDesc & vbNewLine & url
For i = 0 To 4 'Versuche das PDF 5x abzufragen (funktioniert ab und zu nicht).
responseJson = rest.FullRequestNoBodyBd(method, url, pdfData)
If rest.ResponseStatusCode = 200 Then Exit For
Thread.Sleep(500)
i = i + 1
Next
If rest.ResponseStatusCode <> 200 Then
Dim sbErrorText As New Chilkat.StringBuilder
sbErrorText.AppendBd(pdfData, "utf-8", 0, 0)
failureDesc = sbErrorText.GetAsString()
Return failureDesc
End If
Else
Return pdfData.GetEncoded("base64")
@@ -145,9 +156,16 @@ Public Class cCreditSafeAPI
Return failureDesc
Else
Return responseJson
If (rest.ResponseStatusCode <> 200) Then
failureDesc = rest.ResponseStatusText
Return failureDesc
Else
Return responseJson
End If
End If
End If
@@ -174,6 +192,7 @@ Public Class cCreditSafeAPI
Shared Function searchCompanies(company As Company, ByRef dataTable As DataTable) As String
VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()
Dim failureDesc As String
Dim myUrl As String = API_STRING & "/v1/companies"
Dim jsonRespString = SendGetRequestWithAuthHeader(myUrl, company, "application/json", "GET", token, failureDesc)
@@ -268,15 +287,15 @@ Public Class cCreditSafeAPI
End Function
Shared Function getReport(ByRef company As Company, withPDF As Boolean) As String 'Shared Function getReport(ByRef company As Company, withPDF As Boolean, ByRef bytes As Byte()) As String
Dim failureDesc As String
Dim failureDesc As String = ""
Dim myUrl As String = API_STRING & "/v1/companies/" & company.creditSafeId & "/"
If company.country = "DE" Then
'Abfragen für DE benötigen einen Reason-Code
myUrl &= "?customData=de_reason_code::2"
myUrl &= "&?language=DE&?template=full"
myUrl &= "?customData=de_reason_code::3"
myUrl &= "&?language=DE"
Else
myUrl &= "?language=DE&?template=full"
myUrl &= "?language=DE"
End If
Dim acceptContentType = "application/json"
@@ -287,14 +306,11 @@ Public Class cCreditSafeAPI
Dim jsonRespString = SendGetRequestWithAuthHeader(myUrl, Nothing, acceptContentType, "GET", token, failureDesc)
If failureDesc <> "" Then Return failureDesc
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
If (success <> True) Then Return "Verbindungsfehler"
'Aubbau JSON Object
@@ -433,40 +449,32 @@ Public Class cCreditSafeAPI
End Function
Shared Function getPDF(ByRef company As Company, ByRef failure As String) As Byte()
If company IsNot Nothing AndAlso company.creditSafeId <> "" Then
Dim myUrl As String = API_STRING & "/v1/companies/" & company.creditSafeId & "/"
If company.country = "DE" Then
'Abfragen für DE benötigen einen Reason-Code
myUrl &= "?customData=de_reason_code::2"
myUrl &= "&?language=DE&?template=full"
Else
myUrl &= "?template=full"
End If
Dim acceptContentType = "application/pdf"
Dim pdfData2 As New Chilkat.BinData
Dim failureDesc As String
Dim jsonRespString = SendGetRequestWithAuthHeader(myUrl, Nothing, acceptContentType, "GET", token, failureDesc)
failure = failureDesc
If jsonRespString IsNot Nothing And failure = "" Then
Dim success = pdfData2.AppendEncoded(jsonRespString, "base64")
If success Then
Return pdfData2.GetBinary
End If
'Return Convert.FromBase64String(test)
End If
Shared Function getPDF(ByVal company As Company, ByRef failure As String) As Byte()
Dim myUrl As String = API_STRING & "/v1/companies/" & company.creditSafeId & "/"
If company.country = "DE" Then
'Abfragen für DE benötigen einen Reason-Code
myUrl &= "?customData=de_reason_code::3"
myUrl &= "&?language=DE"
Else
Return Nothing
myUrl &= "?language=DE"
End If
Dim acceptContentType = "application/pdf"
Dim pdfData2 As New Chilkat.BinData
Dim failureDesc As String = ""
Dim jsonRespString = SendGetRequestWithAuthHeader(myUrl, Nothing, acceptContentType, "GET", token, failureDesc)
If failureDesc <> "" Then Return Nothing
If jsonRespString IsNot Nothing Then
Dim success = pdfData2.AppendEncoded(jsonRespString, "base64")
If success Then
Return pdfData2.GetBinary
End If
Return Nothing
End If
Return Nothing
End Function
@@ -477,10 +485,13 @@ Public Class cCreditSafeAPI
rest.AddQueryParam("countries", company.country)
If company.creditsafeNo <> "" Then 'Eindeutiger Schlüssel
rest.AddQueryParam("safeNo", company.creditsafeNo)
Else
If company.language <> "" Then rest.AddQueryParam("language", company.language)
If company.vatNo <> "" Then
rest.AddQueryParam("vatNo", company.vatNo)
@@ -571,11 +582,13 @@ Public Class cCreditSafeAPI
Public Property csShareholder As String
Public Property csCEO As String
Public Property language As String
Public Property csBank As String
Public Sub New(_name As String, _vatNo As String, _country As String, _creditsafeNo As String, _creditSafeId As String, _lastChecked As Date, _street As String, _postalCode As String, _city As String, _sumEmployees As String, _businessPurpose As String, _shareholder As String, _ceo As String, _bank As String)
Public Sub New(_language As String, _name As String, _vatNo As String, _country As String, _creditsafeNo As String, _creditSafeId As String, _lastChecked As Date, _street As String, _postalCode As String, _city As String, _sumEmployees As String, _businessPurpose As String, _shareholder As String, _ceo As String, _bank As String)
creditSafeId = _creditSafeId
name = _name
vatNo = _vatNo
@@ -590,6 +603,7 @@ Public Class cCreditSafeAPI
csShareholder = _shareholder
csCEO = _ceo
csBank = _bank
language = _language
End Sub
Public Sub New()