Dit programma gebruikt een bestand van schijf en zal ook wat bestanden op schijf schrijven te verduidelijking. In een productieomgeving zal dit in meeste gevallen in het werkgeheugen plaatsvinden.
De directory die gebruikt wordt is c:\tmp en moet dus aangemaakt zijn. Het invoerbestand hieronder moet met de naam testAFD1.xml in die directory worden geplaatst. Als mapping type wordt AFD1_AFD2 gebruikt, dus de mapping van een AFD1 bericht naar een AFD2 bericht.
Inhoud c:\tmp\testAFD1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<n1:Contractdocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:n1="http://schemas.sivi.org/AFD/Contractbericht/2021/5/1">
<AL>
<AL_VRWRKCD>0</AL_VRWRKCD>
<AL_VIEWCOD>00010</AL_VIEWCOD>
<AL_VERSIEV>1</AL_VERSIEV>
<AL_PFUNK>001</AL_PFUNK>
<AL_DATACAT>38A</AL_DATACAT>
<AL_TPAAND>999999</AL_TPAAND>
</AL>
<PP>
<PP_VRWRKCD>1</PP_VRWRKCD>
<PP_NUMMER>5950001</PP_NUMMER>
<PP_BRANCHE>013</PP_BRANCHE>
<PP_MYAAND>A000</PP_MYAAND>
<PP_MYBRA>010</PP_MYBRA>
<PP_INGDAT>20220101</PP_INGDAT>
<PP_EXPDAT>20260101</PP_EXPDAT>
<PP_PVADAT>20220701</PP_PVADAT>
<PP_VVDAT>20221001</PP_VVDAT>
<PP_HVVDAT>20220101</PP_HVVDAT>
<PP_BETWIJZ>M</PP_BETWIJZ>
<PP_BETTERM>3</PP_BETTERM>
<PP_BTP>854.25</PP_BTP>
<PP_TKST>1.25</PP_TKST>
<PP_TTSL>0</PP_TTSL>
<PP_TASS>0</PP_TASS>
<PP_TKRT>0</PP_TKRT>
<PP_TTOT>855.50</PP_TTOT>
<PP_TPP>173.04</PP_TPP>
<PP_TPC>01</PP_TPC>
<PP_TINV>0</PP_TINV>
<VZ></VZ>
<AO>
<AO_VRWRKCD>1</AO_VRWRKCD>
<AO_CODE>1100</AO_CODE>
<AO_INDEX>N</AO_INDEX>
<AO_VERZSOM>32906</AO_VERZSOM>
<VP>
<VP_VRWRKCD>3</VP_VRWRKCD>
<VP_ANAAM>Stichting SIVI</VP_ANAAM>
<BO>
<BO_VRWRKCD>0</BO_VRWRKCD>
<BO_BKS>03</BO_BKS>
<BO_BOEKDAT>20220701</BO_BOEKDAT>
<BO_RCMND>202207</BO_RCMND>
<BO_BETWIJZ>T</BO_BETWIJZ>
<BO_BRPRM>854.25</BO_BRPRM>
<BO_KST>1.25</BO_KST>
<BO_TSL>0</BO_TSL>
<BO_KRT>0</BO_KRT>
<BO_ASSBEL>0</BO_ASSBEL>
<BO_INCASSO>855.50</BO_INCASSO>
<BO_AFSLPRV>0</BO_AFSLPRV>
<BO_PROLPRV>-173.04</BO_PROLPRV>
<BO_OVRPRV>0</BO_OVRPRV>
<BO_INCVERG>0</BO_INCVERG>
<BO_RCBEDR>682.46</BO_RCBEDR>
</BO>
</VP>
</AO>
</PP>
</n1:Contractdocument>
Daarna moet het onderstaande voorbeeldprogramma met de extensie .ps1 op schijf worden opgeslagen. Dit voorbeeldprogramma bevat enkele onderdelen om het verloop duidelijk te maken zoals bijvoorbeeld het formatteren van input en output bestanden, deze zijn niet nodig in een productieomgeving.
Inhoud c:\tmp\voorbeeldprogramma.ps1:
# Voorbeeld aanroep mapping-API
$ProgressPreference = "SilentlyContinue"
# algemene properties
$workingDirectory = "c:\tmp\"
$inputFile = $workingDirectory + "testAFD1.xml"
$outputFile = $workingDirectory + "testAFD2.json"
$requestFile = $workingDirectory + "request_AFD1.json"
$responseFile = $workingDirectory + "response_AFD2.json"
$responseAutorisatie = $workingDirectory + "response_autorisatie.json"
$mapping = "AFD1_AFD2"
$uriMapping = "https://mapping.sivi.org/api/mapping"
# Autorisatie - properties
$onMicrosoftAccount = "siviafdtooling"
$userFlow = "B2C_1_Afs_Mapping_Service_Auth"
$clientId = "92345022-c862-40fa-b921-0711fe8e8b63"
$uriAutorisatie = "https://$onMicrosoftAccount.b2clogin.com/$onMicrosoftAccount.onmicrosoft.com/$userFlow/oauth2/v2.0/token"
# Credentials - Nu input van scherm -> in programmatuur ophalen van schijf/db/etc.
$user = Read-Host -Prompt "Username voor de mapping-API"
$password = Read-Host -Prompt "Password voor de mapping-API" -MaskInput
$uriAutorisatie = $uriAutorisatie + "?username=$user&password=$password&grant_type=password&scope=openid+$clientId+offline_access&client_id=$clientId&response_type=token+id_token"
# Parameters samenstellen
$parameters = @{
Method = "POST"
Uri = $uriAutorisatie
}
# Autorisatie server aanroepen
:ProgrammaLoop while ($true) {
Try {
Invoke-RestMethod @parameters -StatusCodeVariable HTTPStatus -Outfile $responseAutorisatie | Out-Null
}
catch{
$errorMsg = "Error: " + $_.Exception.Response.StatusCode.value__ + " " + $_.Exception.Response.StatusDescription + " " + $_.Exception.Response.Detail + " " + $_.Exception.Response.Message
Write-Host $errorMsg
break ProgrammaLoop
}
# Autorisatie lezen
$autorisatie = Get-Content -Path $responseAutorisatie | ConvertFrom-Json
if ($null -eq $autorisatie) {
Write-Host "Geen autorisatie gekregen."
break ProgrammaLoop
}
# Output formatteren
ConvertTo-Json $autorisatie | Set-Content -Path $responseAutorisatie
# access token ophalen
$access_token = $autorisatie.PsObject.Properties["access_token"].Value
# Header samenstellen
$header = @{
"authorization" = "Bearer $access_token"
}
# Content ophalen
$content = Get-Content -Path $inputFile
# Coderen naar base64
#$bytes = [System.Text.Encoding]::UTF8.GetBytes($content)
$base64Coded = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($content))
# Bericht samenstellen
$body = '
{
"commonFunctional":
{
"mappingType": "' + $mapping + '",
"entityType": "default"
},
"document":
{
"content": "' + $base64Coded + '",
"entityType": "default"
}
}'
# voor testdoeleinden
Set-Content -Path $requestFile -Value $body | Out-Null
# Parameters samenstellen
$parameters = @{
Method = "POST"
Uri = $uriMapping
Header = $header
Body = $body
ContentType = "application/json"
}
# mapping-API aanroepen
Try {
Invoke-RestMethod @parameters -StatusCodeVariable HTTPStatus -Outfile $responseFile | Out-Null
}
catch{
$errorMsg = "Error: " + $_.Exception.Response.StatusCode.value__ + " " + $_.Exception.Response.StatusDescription + " " + $_.Exception.Response.Detail + " " + $_.Exception.Response.Message
Write-Host $errorMsg
break ProgrammaLoop
}
# Output lezen
$response = Get-Content -Path $responseFile | ConvertFrom-Json
if ($null -eq $response) {
Write-Host "Geen response van de mapping-API gekregen." $HTTPStatus
break ProgrammaLoop
}
# Output formatteren
ConvertTo-Json $response | Set-Content -Path $responseFile
# Bericht uit document halen
$document = $response.PsObject.Properties["document"].Value
if ($null -ne $document) {
# Content ophalen
$content = $document.PsObject.Properties["content"].Value
Try {
# Decoderen van base64
$base64Decoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($content))
# Nieuw bestand opslaan
Set-Content -Path $outputFile -Value $base64Decoded | Out-Null
}
catch {
write-host "Geen geldig base64-document teruggekregen."
break ProgrammaLoop
}
}
else {
write-host $HTTPStatus $response
break ProgrammaLoop
}
write-host "Gereed."
break ProgrammaLoop
}
Met de rechtermuisbutton kan dan het Powershellscript worden gestart. Het programma zal vragen om een username en password. Dit zijn de credentials van het systeemaccount dat is aangemaakt. Daarna gaat het programma verder en zal de volgende bestanden aanmaken:
- AutorisatieResponse.json
- Dit is het antwoord van de autorisatieserver waaruit de access_token gehaald moet worden.
- Request.json
- Dit is het bericht zoals dat naar de mapping-API wordt gestuurd. Het bestand AFD1.xml is dan omgezet naar base64 en opgenomen in de request.
- Response.json
- Dit is het bericht dat terugkomt van de mapping-API met het omgezette bericht nog in base64 gecodeerd.
- testAFD2.json
- Dit is het gedecodeerde bericht en heeft als inhoud het omgezette AFD bericht.
Dit voorbeeldprogramma is bedoeld ter ondersteuning en verduidelijking. Mochten er nog vragen of extra informatie nodig zijn, mail ons op support@sivi.org.
Geef uw reactie op dit onderwerp.