Files
BeWoPlaner/Shared/AppSender/GkvSender.cs
2026-03-02 12:02:06 +01:00

97 lines
2.6 KiB
C#

using BS.Shared.DataContracts;
using BS.Shared.DataContracts.GkvAbrechnung;
using BS.Shared.Exceptions;
using Newtonsoft.Json;
using System;
using System.Net;
using System.Threading.Tasks;
namespace BS.Shared.AppSender
{
public static class GkvSender
{
private static readonly string GKVCLIENT_URL = "{0}/GkvAbrechnungClient.aspx";
public static string SerializeResponse<Res>(Res response) where Res : GkvResponseDC
=> JsonConvert.SerializeObject(response);
public static string SerializeRequest<Req>(Req request) where Req : GkvClientRequestDC
=> JsonConvert.SerializeObject(request);
public static Req DeserializeRequest<Req>(string str, Action sendInvalidInputResponse) where Req : AppRequestDC
{
if (str is null)
sendInvalidInputResponse();
var deserialize = JsonConvert.DeserializeObject<Req>(str);
if (deserialize is null)
sendInvalidInputResponse();
return deserialize;
}
public static Res SendApp8Request<Req, Res>(Req request, string username, string password, string tenant) where Req : GkvServerRequestDC where Res : GkvResponseDC
{
request.Username = username;
request.Password = password;
return SendApp8Request<Req, Res>(request, tenant);
}
public static Res SendApp8Request<Req, Res>(Req request, string tenant) where Req : GkvServerRequestDC where Res : GkvResponseDC
{
string url = "https://app8.bewoplaner.de/service/GkvAbrechnungServer.aspx";
#if DEBUG
url = "http://localhost:3777/Host/GkvAbrechnungServer.aspx";
#endif
Res app8_response;
try
{
app8_response = AppRequestSender.Send<Req, Res>(request, url, tenant);
}
catch (Exception e)
{
throw new GkvException(e.Message, "App8");
}
if (app8_response is null)
throw new GkvException("Leere Antwort", "App8");
return app8_response;
}
public static async Task<Res> SendKundenAppClientRequestAsync<Req, Res>(Req request, string tenant, long useroid) where Req : AppRequestDC where Res : GkvResponseDC
{
string server = AppRequestSender.GetServerFromTenant(tenant);
if (string.IsNullOrEmpty(server))
return null;
var url = string.Format(GKVCLIENT_URL, $"https://{server}/service");
#if DEBUG
if (server.IndexOf("localhost") > 0)
url = string.Format(GKVCLIENT_URL, $"http://{server}/Host");
#endif
Res app8_response;
try
{
app8_response = await AppRequestSender.SendAsync<Req, Res>(request, url, tenant, useroid);
}
catch (Exception e)
{
throw new GkvException(e.ToString() + $" ({url})", "KundenApp");
}
if (app8_response is null)
throw new GkvException("Leere Antwort", "KundenApp");
return app8_response;
}
}
}