Import call
Here is an example of an import call implemented through a powershell script that will be analyzed in depth in this article:
#Auth
$user = "TestUser"
$password = "TestPassword"
$authorization = "Basic " + [Convert]::ToBase64String([System.Text.Encoding]::Ascii.GetBytes($user + ":" + $password))
#Header
$header = @{
"Accept"="application/json"
"Content-Type"="application/json"
"Authorization"=$authorization
}
#Body
$filename = $args[0]
$fluentisFormat = "TestFormat"
class ImportRequest {
[int]$Format
[string]$FluentisFormat
[string]$BinaryContent
[int]$CompanyId
[string]$CompanyCode
[int]$DepartmentId
[string]$DepartmentCode
}
$request = [ImportRequest]::new()
$request.Format = 0
$request.FluentisFormat = $fluentisFormat
$request.BinaryContent = [Convert]::ToBase64String([System.IO.File]::ReadAllBytes($filename))
$request.CompanyCode = "1"
$request.DepartmentCode = "1"
$requestAsJson = ConvertTo-Json $request
#WebRequest
try
{
$jsonResponse = Invoke-WebRequest `
-Method POST `
-UseBasicParsing `
-Headers $header `
-Body $requestAsJson `
-ContentType application/json `
-Uri https://ServerName/Fluentis/api/public/FluentisErp/ControllerEndPoint/
$response = ConvertFrom-Json $jsonResponse.Content
Write-Output ("Response:")
$response.Details
}
catch
{
Write-Output ("Message: " + $_.Exception.Message)
Write-Output ("Status code: " + $_.Exception.Response.StatusCode.value__ + " (" + $_.Exception.Response.StatusCode + ")")
Write-Output ("Status description: " + $_.Exception.Response.StatusDescription)
Write-Output ("Error: ")
$memStream = $_.Exception.Response.GetResponseStream()
$readStream = New-Object System.IO.StreamReader($memStream)
while ($readStream.Peek() -ne -1) {
Write-Output $readStream.ReadLine()
}
$readStream.Dispose()
}
Implementation
The code shown in the example is divided into four areas. The code showed in the example is split into four blocks identified by the related remarks:
- Auth to handle authentication
- Header to build the header of the call
- Body with the body of the call
- WebRequest with the call execution and the controller URI
Auth
#Auth
$user = "TestUser"
$password = "TestPassword"
$authorization = "Basic " + [Convert]::ToBase64String([System.Text.Encoding]::Ascii.GetBytes($user + ":" + $password))
User and Password parameters required for authentication must be the credentials reported for the Service User set in ARM menu Home - Connections.
Header
$header = @{
"Accept"="application/json"
"Content-Type"="application/json"
"Authorization"=$authorization
}
Body
$filename = $args[0]
$fluentisFormat = "TestFormat"
class ImportRequest {
[int]$Format
[string]$FluentisFormat
[string]$BinaryContent
}
$request = [ImportRequest]::new()
$request.Format = 0
$request.FluentisFormat = $fluentisFormat
$request.BinaryContent = [Convert]::ToBase64String([System.IO.File]::ReadAllBytes($filename))
$requestAsJson = ConvertTo-Json $request
The body of the import WebApi is based on the standard ImportRequest class. This class provides three parameters:
- Format to handle the import format, 0 for XML and 1 for JSON
- FluentisFormat identifies the import template parameterized in ARM menu Platform - WebApi - Import/Export Format (if the FluentisFormat is not reported the default layout assigned to the controller in ARM menu Platform - WebApi - WebApi Controller will be used ).
- BinaryContent which will contain the path to be imported with the tags valued. This example loaded a file from disk using the path $filename handed to the call as a parameter.
WebRequest
$jsonResponse = Invoke-WebRequest `
-Method POST `
-UseBasicParsing `
-Headers $header `
-Body $requestAsJson `
-ContentType application/json `
-Uri https://ServerName/Fluentis/api/public/FluentisErp/ControllerEndPoint/
$response = ConvertFrom-Json $jsonResponse.Content
Write-Output ("Response:")
$response.Details
The WebRequest is a POST-type call in JSON format that will make a call based on the HTTPS protocol (authentication credentials are included in the header), and will call the Endpoint exposed by Fluentis for the required controller and related method. The list of all endpoints exposed is published in the documentation on the Endpoints List page.