Files
laatzen/Common/Ui/GenesisToolBox/Infrastructure/ApplicationHttp.cs
T

106 lines
4.0 KiB
C#

namespace Xylem.Common.Ui.GenesisToolBox.Infrastructure
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xylem.Common.CommonCore.Configuration;
internal class ApplicationHttp
{
const string APPLICATION_JSON = "application/json";
private static readonly HttpClient httpClient = new HttpClient()
{
BaseAddress = new Uri(ServiceUrls.LaaProductionAPI)
};
internal static int ApplicationId(string software, params string[] functions)
=> Task
.Run(async () =>
{
var applicationId = default(int);
try
{
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/software/appid"))
{
httpRequestMessage.Content = new StringContent(
mediaType: APPLICATION_JSON,
encoding: Encoding.UTF8,
content: JsonConvert.SerializeObject(new
{
software,
functions
}));
using (var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage))
{
if (httpResponseMessage.Content != null)
{
var httpResponseContent = await httpResponseMessage.Content.ReadAsStringAsync();
_ = int.TryParse(httpResponseContent, out applicationId);
}
}
}
}
catch (Exception)
{
// TODO: Log the error message.
}
return applicationId;
})
.ConfigureAwait(true)
.GetAwaiter()
.GetResult();
internal static IEnumerable<T> GetSoftwareFunction<T>(int appId, string username, string password)
=> Task
.Run(async () =>
{
var functions = default(IEnumerable<T>);
try
{
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/software/functions"))
{
httpRequestMessage.Content = new StringContent(
mediaType: APPLICATION_JSON,
encoding: Encoding.UTF8,
content: JsonConvert.SerializeObject(new
{
appId,
username,
password,
}));
using (var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage))
{
if (httpResponseMessage.IsSuccessStatusCode && httpResponseMessage.Content != null)
{
var httpResponseContent = await httpResponseMessage.Content.ReadAsStringAsync();
functions = JsonConvert.DeserializeObject<T[]>(httpResponseContent);
}
}
}
}
catch (Exception)
{
// TODO: Log the error message.
}
return functions;
})
.ConfigureAwait(true)
.GetAwaiter()
.GetResult();
}
}