-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1fefa3
commit 08c75bf
Showing
13 changed files
with
369 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,3 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<PackageReference Include="Nerdbank.GitVersioning"> | ||
<Version>3.0.50</Version> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
|
||
namespace fiskaltrust.Middleware.Interface.Grpc | ||
{ | ||
public class Class1 | ||
{ | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/fiskaltrust.Middleware.Interface.Grpc/fiskaltrust.Middleware.Interface.Grpc.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
18 changes: 18 additions & 0 deletions
18
src/fiskaltrust.Middleware.Interface.Http/Helpers/XmlSerializationHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.IO; | ||
using System.Xml.Serialization; | ||
|
||
namespace fiskaltrust.Middleware.Interface.Http.Helpers | ||
{ | ||
internal static class XmlSerializationHelpers | ||
{ | ||
public static string Serialize(object inputObject) | ||
{ | ||
using (var writer = new StringWriter()) | ||
{ | ||
new XmlSerializer(inputObject.GetType()).Serialize(writer, inputObject); | ||
return writer.ToString(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
using fiskaltrust.ifPOS.v1; | ||
using fiskaltrust.Middleware.Interface.Http.Helpers; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
|
||
namespace fiskaltrust.Middleware.Interface.Http | ||
{ | ||
public class HttpPos : IPOS | ||
{ | ||
private readonly string _url; | ||
private readonly HttpPosOptions _options; | ||
|
||
private delegate string AsyncEchoCaller(string message); | ||
private delegate ifPOS.v0.ReceiptResponse AsyncSignCaller(ifPOS.v0.ReceiptRequest request); | ||
private delegate Stream AsyncJournalCaller(long ftJournalType, long from, long to); | ||
|
||
public HttpPos(HttpPosOptions options) | ||
{ | ||
_url = options.Url.Replace("rest://", "http://").Replace("xml://", "http://"); | ||
_options = options; | ||
} | ||
|
||
IAsyncResult ifPOS.v0.IPOS.BeginEcho(string message, AsyncCallback callback, object state) | ||
{ | ||
var d = new AsyncEchoCaller((this as ifPOS.v0.IPOS).Echo); | ||
return d.BeginInvoke(message, callback, d); | ||
} | ||
|
||
string ifPOS.v0.IPOS.EndEcho(IAsyncResult result) | ||
{ | ||
var d = (AsyncEchoCaller)result.AsyncState; | ||
return d.EndInvoke(result); | ||
} | ||
|
||
string ifPOS.v0.IPOS.Echo(string message) | ||
{ | ||
using (var client = new HttpClient()) | ||
{ | ||
client.BaseAddress = new Uri(_url); | ||
|
||
var jsonstring = JsonConvert.SerializeObject(message); | ||
var jsonContent = new StringContent(jsonstring, Encoding.UTF8, "application/json"); | ||
|
||
using (var response = client.PostAsync("v0/Echo", jsonContent).Result) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
var reponse = response.Content.ReadAsStringAsync().Result; | ||
return JsonConvert.DeserializeObject<string>(reponse); | ||
} | ||
} | ||
} | ||
|
||
async Task<EchoResponse> IPOS.EchoAsync(EchoRequest message) | ||
{ | ||
if (_options.CommunicationType == HttpCommunicationType.Json) | ||
{ | ||
return await JsonEchoAsync(message); | ||
} | ||
else | ||
{ | ||
return await XmlEchoAsync(message); | ||
} | ||
} | ||
|
||
private async Task<EchoResponse> XmlEchoAsync(EchoRequest message) | ||
{ | ||
var xmlString = XmlSerializationHelpers.Serialize(message); | ||
var xmlContent = new StringContent(xmlString, Encoding.UTF8, "application/xml"); | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
client.BaseAddress = new Uri(_url); | ||
|
||
using (var response = await client.PostAsync("v1/Echo", xmlContent)) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
|
||
var xml = XElement.Parse(content); | ||
string jsonText = JsonConvert.SerializeXNode(xml); | ||
return JsonConvert.DeserializeObject<EchoResponse>(jsonText); | ||
} | ||
} | ||
} | ||
|
||
private async Task<EchoResponse> JsonEchoAsync(EchoRequest message) | ||
{ | ||
var jsonstring = JsonConvert.SerializeObject(message); | ||
var jsonContent = new StringContent(jsonstring, Encoding.UTF8, "application/json"); | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
client.BaseAddress = new Uri(_url); | ||
|
||
using (var response = await client.PostAsync("v1/Echo", jsonContent)) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
return JsonConvert.DeserializeObject<EchoResponse>(content.ToString()); | ||
} | ||
} | ||
} | ||
|
||
|
||
IAsyncResult ifPOS.v0.IPOS.BeginSign(ifPOS.v0.ReceiptRequest data, AsyncCallback callback, object state) | ||
{ | ||
var d = new AsyncSignCaller((this as ifPOS.v0.IPOS).Sign); | ||
return d.BeginInvoke(data, callback, d); | ||
} | ||
|
||
ifPOS.v0.ReceiptResponse ifPOS.v0.IPOS.EndSign(IAsyncResult result) | ||
{ | ||
var d = (AsyncSignCaller)result.AsyncState; | ||
return d.EndInvoke(result); | ||
} | ||
|
||
ifPOS.v0.ReceiptResponse ifPOS.v0.IPOS.Sign(ifPOS.v0.ReceiptRequest data) | ||
{ | ||
if (_options.CommunicationType == HttpCommunicationType.Json) | ||
{ | ||
return JsonSignAsync<ifPOS.v0.ReceiptRequest, ifPOS.v0.ReceiptResponse>(data, "v0/sign").Result; | ||
} | ||
else | ||
{ | ||
return XmlSignAsync<ifPOS.v0.ReceiptRequest, ifPOS.v0.ReceiptResponse>(data, "v0/sign").Result; | ||
} | ||
} | ||
|
||
async Task<ReceiptResponse> IPOS.SignAsync(ReceiptRequest request) | ||
{ | ||
if (_options.CommunicationType == HttpCommunicationType.Json) | ||
{ | ||
return await JsonSignAsync<ReceiptRequest, ReceiptResponse>(request, "v1/sign"); | ||
} | ||
else | ||
{ | ||
return await XmlSignAsync<ReceiptRequest, ReceiptResponse>(request, "v1/sign"); | ||
} | ||
} | ||
|
||
private async Task<TResponse> JsonSignAsync<TRequest, TResponse>(TRequest request, string endpoint) | ||
{ | ||
var jsonstring = JsonConvert.SerializeObject(request); | ||
var jsonContent = new StringContent(jsonstring, Encoding.UTF8, "application/json"); | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
client.BaseAddress = new Uri(_url); | ||
|
||
using (var response = await client.PostAsync(Path.Combine(_url, endpoint), jsonContent)) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
|
||
return JsonConvert.DeserializeObject<TResponse>(content); | ||
} | ||
} | ||
} | ||
|
||
private async Task<TResponse> XmlSignAsync<TRequest, TResponse>(TRequest request, string endpoint) | ||
{ | ||
var xmlString = XmlSerializationHelpers.Serialize(request); | ||
var xmlContent = new StringContent(xmlString, Encoding.UTF8, "application/xml"); | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
client.BaseAddress = new Uri(_url); | ||
|
||
using (var response = await client.PostAsync(endpoint, xmlContent)) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
var xml = XElement.Parse(content); | ||
string jsonText = JsonConvert.SerializeXNode(xml); | ||
|
||
return JsonConvert.DeserializeObject<TResponse>(jsonText); | ||
} | ||
} | ||
} | ||
|
||
|
||
IAsyncResult ifPOS.v0.IPOS.BeginJournal(long ftJournalType, long from, long to, AsyncCallback callback, object state) | ||
{ | ||
var d = new AsyncJournalCaller((this as ifPOS.v0.IPOS).Journal); | ||
return d.BeginInvoke(ftJournalType, from, to, callback, d); | ||
} | ||
|
||
Stream ifPOS.v0.IPOS.EndJournal(IAsyncResult result) | ||
{ | ||
var d = (AsyncJournalCaller)result.AsyncState; | ||
return d.EndInvoke(result); | ||
} | ||
|
||
Stream ifPOS.v0.IPOS.Journal(long ftJournalType, long from, long to) | ||
{ | ||
using (var client = new HttpClient()) | ||
{ | ||
client.BaseAddress = new Uri(_url); | ||
client.DefaultRequestHeaders.Accept.Clear(); | ||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
|
||
using (var response = client.GetAsync($"v0/journal?type={ftJournalType}&from={from}&to={to}").Result) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
var stream = response.Content.ReadAsStreamAsync().Result; | ||
return stream; | ||
} | ||
} | ||
} | ||
|
||
IAsyncEnumerable<JournalResponse> IPOS.JournalAsync(JournalRequest request) | ||
{ | ||
throw new NotSupportedException("Async streaming is not supported in HTTP. Please call the non-async Journal method."); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/fiskaltrust.Middleware.Interface.Http/HttpPosFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using fiskaltrust.ifPOS.v1; | ||
using System; | ||
|
||
namespace fiskaltrust.Middleware.Interface.Http | ||
{ | ||
public class HttpPosFactory : IPOSFactory | ||
{ | ||
public IPOS CreatePosAsync(POSOptions options) | ||
{ | ||
if (!(options is HttpPosOptions httpOptions)) | ||
{ | ||
throw new ArgumentException($"Parameter {nameof(options)} must be of type {nameof(HttpPosOptions)}."); | ||
} | ||
|
||
return new HttpPos(httpOptions); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/fiskaltrust.Middleware.Interface.Http/HttpPosOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using fiskaltrust.ifPOS.v1; | ||
|
||
namespace fiskaltrust.Middleware.Interface.Http | ||
{ | ||
public class HttpPosOptions : POSOptions | ||
{ | ||
public HttpCommunicationType CommunicationType { get; set; } | ||
} | ||
|
||
public enum HttpCommunicationType | ||
{ | ||
Json, | ||
Xml | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/fiskaltrust.Middleware.Interface.Http/fiskaltrust.Middleware.Interface.Http.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="fiskaltrust.interface" Version="1.3.0-rc1" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="6.0.8" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<PackageId>fiskaltrust.Middleware.Interface.Http</PackageId> | ||
<Authors>fiskaltrust</Authors> | ||
<Company>fiskaltrust</Company> | ||
<PackageLicenseUrl>https://github.com/fiskaltrust/middleware-interface-dotnet/LICENSE</PackageLicenseUrl> | ||
<PackageProjectUrl>https://github.com/fiskaltrust/middleware-interface-dotnet</PackageProjectUrl> | ||
<PackageIconUrl>https://portal.fiskaltrust.at/Content/favicons/favicon-64x64.png</PackageIconUrl> | ||
<Description>An optional helper implementation to simplify the usage of the fiskaltrust Middleware interface via HTTP.</Description> | ||
<Copyright>Copyright 2020</Copyright> | ||
<PackageTags>fiskaltrust interface</PackageTags> | ||
<AssemblyCompany>fiskaltrust</AssemblyCompany> | ||
<AssemblyProduct>fiskaltrust.Middleware.Interface.Http</AssemblyProduct> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\fiskaltrust.ifPOS\fiskaltrust.Middleware.Interface.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
|
||
namespace fiskaltrust.Middleware.Interface.Soap | ||
{ | ||
public class Class1 | ||
{ | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/fiskaltrust.Middleware.Interface.Soap/fiskaltrust.Middleware.Interface.Soap.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
Oops, something went wrong.