Files
BeWoPlaner/Shared/DataContracts/Invoicing/XRechnung/XRechnungResponse.cs
2025-06-10 14:29:34 +02:00

77 lines
1.5 KiB
C#

using BS.Shared.DataContracts;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace BS.Shared.DataContracts.Invoicing.XRechnung
{
public class XRechnungResponse : OwnSoftResponse
{
[DataMember] public byte[] ResponseBytes { get; set; }
[DataMember] public string ResponseString { get; set; }
[DataMember] public List<string> Errors { get; set; } = new List<string>();
public bool HasErrors => Errors is object && Errors.Any();
private XRechnungResponse()
{
}
private XRechnungResponse(byte[] res_bytes) : this()
{
ResponseBytes = res_bytes;
}
public static XRechnungResponse Parse(byte[] res_bytes)
{
var response = new XRechnungResponse(res_bytes);
response.LoadErrors();
return response;
}
public static bool TryParse(byte[] res_bytes, out XRechnungResponse rechnungResponse)
{
rechnungResponse = null;
try
{
rechnungResponse = Parse(res_bytes);
return true;
}
catch(Exception e)
{
return false;
}
}
private void LoadErrors()
{
try
{
ResponseString = Encoding.UTF8.GetString(ResponseBytes);
var kv = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(ResponseString);
if (kv is object && kv.TryGetValue("errors", out var errors))
{
Errors = errors;
}
}
catch (Exception e)
{
}
}
public string ErrorString => string.Join(", ", Errors);
}
}