Refactor: Add ImporterShared Project
This commit is contained in:
100
ImporterShared/Winline/WebService.vb
Normal file
100
ImporterShared/Winline/WebService.vb
Normal file
@@ -0,0 +1,100 @@
|
||||
Imports System.Xml
|
||||
Imports System.Net
|
||||
Imports System.Globalization
|
||||
Imports AutoMapper
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports DigitalData.Modules.Filesystem
|
||||
Imports ImporterShared.Documents
|
||||
|
||||
Namespace Winline
|
||||
Public Class WebService
|
||||
Inherits BaseClass
|
||||
|
||||
Private ReadOnly Config As Config
|
||||
Private ReadOnly Serializer As Serializer
|
||||
Private ReadOnly Mapper As AutoMapper.Mapper
|
||||
Private ReadOnly FileEx As File
|
||||
|
||||
Public Sub New(pLogConfig As LogConfig, pConfig As Config)
|
||||
MyBase.New(pLogConfig, pLogConfig.GetLogger())
|
||||
FileEx = New File(pLogConfig)
|
||||
Serializer = New Serializer(pLogConfig)
|
||||
Config = pConfig
|
||||
End Sub
|
||||
|
||||
Public Function TransferDocumentToWinLine(pDocument As Document) As Boolean
|
||||
If TypeOf pDocument.Data Is Schemas.Orders.Input.MESOWebService Then
|
||||
TransferOrderToWinline(pDocument)
|
||||
End If
|
||||
|
||||
Return True
|
||||
End Function
|
||||
|
||||
Private Function TransferOrderToWinline(pDocument As Document)
|
||||
Dim oOrderOutput = TransformOrderToOutput(pDocument.Data)
|
||||
Dim oWS As Config.WebServiceConfig = Config.Webservice
|
||||
Dim oFilePath = SerializeOrder(oOrderOutput)
|
||||
Dim oXmlString = IO.File.ReadAllText(oFilePath)
|
||||
pDocument.DataOutput = oOrderOutput
|
||||
|
||||
Dim oURL As String = $"{oWS.BaseUrl}/ewlservice/import?User={oWS.Username}&Password={oWS.Password}&Company={pDocument.Mandator}&Type=30&Vorlage=EXIM-VRG_orders&ActionCode=1&Byref=0Data={oXmlString}"
|
||||
Dim oRequest = WebRequest.Create(oURL)
|
||||
oRequest.Method = "POST"
|
||||
|
||||
Logger.Info("Creating HTTP Request to [{0}]", oWS.BaseUrl)
|
||||
|
||||
' TODO: Make better lol
|
||||
|
||||
Using oResponse = oRequest.GetResponse()
|
||||
Using oStream = oResponse.GetResponseStream()
|
||||
Using oReader As New IO.StreamReader(oStream)
|
||||
Dim oData = oReader.ReadToEnd()
|
||||
|
||||
End Using
|
||||
End Using
|
||||
End Using
|
||||
|
||||
Return True
|
||||
End Function
|
||||
|
||||
Private Function TransformOrderToOutput(pData As Schemas.Orders.Input.MESOWebService) As Schemas.Orders.Output.MESOWebService
|
||||
Dim oData As Schemas.Orders.Input.MESOWebService = pData
|
||||
Dim oResult As Schemas.Orders.Output.MESOWebService = Mapper.Map(Of Schemas.Orders.Output.MESOWebService)(oData)
|
||||
|
||||
Dim oItems = oData.Items.
|
||||
Select(Function(i)
|
||||
If TypeOf i Is Schemas.Orders.Input.MESOWebServiceEXIMVRG_ordersT025 Then
|
||||
Return Mapper.Map(Of Schemas.Orders.Output.MESOWebServiceEXIMVRG_ordersT025)(i)
|
||||
Else
|
||||
Return Mapper.Map(Of Schemas.Orders.Output.MESOWebServiceEXIMVRG_ordersT026)(i)
|
||||
End If
|
||||
End Function).
|
||||
ToList()
|
||||
|
||||
oResult.Items = oItems.ToArray()
|
||||
Return oResult
|
||||
End Function
|
||||
|
||||
Private Function SerializeOrder(pData As Schemas.Orders.Output.MESOWebService) As String
|
||||
Dim oSerializer = Serializer.GetSerializer(GetType(Schemas.Orders.Output.MESOWebService))
|
||||
Dim oSettings As New XmlWriterSettings With {.Indent = True}
|
||||
|
||||
Dim oPath As String = IO.Path.Combine(FileEx.GetAppDataPath("Digital Data", "EDI Document Importer"), "WebService")
|
||||
Dim oFileName As String = $"{Now:yyyy-MM-dd-ffff}.xml"
|
||||
Dim oFilePath As String = IO.Path.Combine(oPath, oFileName)
|
||||
|
||||
If IO.Directory.Exists(oPath) = False Then
|
||||
IO.Directory.CreateDirectory(oPath)
|
||||
End If
|
||||
|
||||
Using oWriter = XmlWriter.Create(oFilePath, oSettings)
|
||||
oSerializer.Serialize(oWriter, pData)
|
||||
End Using
|
||||
|
||||
Return oFilePath
|
||||
End Function
|
||||
|
||||
|
||||
End Class
|
||||
|
||||
End Namespace
|
||||
Reference in New Issue
Block a user