103 lines
4.0 KiB
VB.net
103 lines
4.0 KiB
VB.net
Imports System.Text.RegularExpressions
|
|
Imports Newtonsoft.Json
|
|
|
|
Public Class cATEZ_Tariff_simple
|
|
|
|
' Klasse zur Darstellung der Ergebnisse
|
|
Public Class cATEZ_TariffItem
|
|
Public Property Id As Integer
|
|
Public Property CommodityCode As String
|
|
Public Property Description As String
|
|
Public Property HeadingId As Integer
|
|
Public Property LanguageCode As String
|
|
Public Property Type As String
|
|
End Class
|
|
|
|
Public Shared Function GetTariffInfo_SingleOrEmpty(searchText As String, Optional countryCode As String = "DE", Optional languageCode As String = "DE", Optional measureType As String = "import", Optional ByRef result As List(Of cATEZ_TariffItem) = Nothing) As String
|
|
If GetTariffInfo(searchText, countryCode, languageCode, measureType, result) = "OK" Then
|
|
If result.Count > 1 Then
|
|
Return ""
|
|
Else
|
|
Return result(0).CommodityCode
|
|
End If
|
|
Else
|
|
Return ""
|
|
End If
|
|
End Function
|
|
|
|
Public Shared Function GetTariffInfo(searchText As String, Optional countryCode As String = "DE", Optional languageCode As String = "DE", Optional measureType As String = "import", Optional ByRef result As List(Of cATEZ_TariffItem) = Nothing) As String
|
|
|
|
If searchText = String.Empty Then Return "Error: Empty search String"
|
|
searchText = Regex.Replace(searchText, "[^\d]", "")
|
|
If searchText.Length > 8 Then searchText = searchText.Substring(0, 8)
|
|
|
|
' Chilkat HTTP-Objekt erstellen
|
|
Dim http As New Chilkat.Http()
|
|
|
|
' API-URL und Parameter definieren
|
|
Dim baseUrl As String = "https://tariff.singlewindow.io/api/v2-0/tariff-query/public/commodity-codes"
|
|
'Dim countryCode As String = "DE"
|
|
'Dim languageCode As String = "DE"
|
|
'Dim measureType As String = "import"
|
|
|
|
' Anfrage-URL mit Parametern erstellen
|
|
Dim url As String = $"{baseUrl}?countryCode={countryCode}&searchText={searchText}&languageCode={languageCode}&measureType={measureType}"
|
|
|
|
' HTTP-GET-Anfrage senden
|
|
Dim response As String = http.QuickGetStr(url)
|
|
|
|
' Fehlerüberprüfung
|
|
If http.LastMethodSuccess = False Then
|
|
Return $"Error: {http.LastErrorText}"
|
|
End If
|
|
|
|
' Statuscode überprüfen
|
|
Dim statusCode As Integer = http.LastStatus
|
|
If statusCode <> 200 Then
|
|
Return $"Error: HTTP Status {statusCode}"
|
|
End If
|
|
|
|
result = ProcessApiResponse(response)
|
|
|
|
' Antwort zurückgeben
|
|
If result.Count = 0 Then
|
|
Return "Error: 0 results"
|
|
End If
|
|
Return "OK"
|
|
End Function
|
|
|
|
Private Shared Function ProcessApiResponse(jsonResponse As String) As List(Of cATEZ_TariffItem)
|
|
' Deserialisiere die JSON-Antwort in eine Liste von Objekten
|
|
Dim items As List(Of Dictionary(Of String, Object)) = JsonConvert.DeserializeObject(Of List(Of Dictionary(Of String, Object)))(jsonResponse)
|
|
|
|
' Liste für deklarierbare Elemente erstellen
|
|
Dim declarableItems As New List(Of cATEZ_TariffItem)()
|
|
|
|
' Ergebnisse durchlaufen
|
|
For Each item As Dictionary(Of String, Object) In items
|
|
If item.ContainsKey("declarable") AndAlso CBool(item("declarable")) = True Then
|
|
Dim tariffItem As New cATEZ_TariffItem() With {
|
|
.Id = CInt(item("id")),
|
|
.CommodityCode = item("commodity_code").ToString(),
|
|
.Description = item("description").ToString(),
|
|
.HeadingId = CInt(item("heading_id")),
|
|
.LanguageCode = item("language_code").ToString(),
|
|
.Type = item("type").ToString()
|
|
}
|
|
declarableItems.Add(tariffItem)
|
|
End If
|
|
Next
|
|
|
|
Return declarableItems
|
|
End Function
|
|
|
|
Sub Example()
|
|
' Beispielaufruf der Funktion
|
|
Dim searchText As String = "6207220000"
|
|
Dim result As String = GetTariffInfo(searchText)
|
|
|
|
' Ergebnis ausgeben
|
|
Console.WriteLine(result)
|
|
End Sub
|
|
End Class
|