137 lines
5.5 KiB
C#
137 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using Xylem.Common.CommonCore.Configuration;
|
|
|
|
namespace Xylem.Common.Utils.UserAccessCtrl
|
|
{
|
|
internal class ApplicationHttp
|
|
{
|
|
const String APPLICATION_JSON = "application/json";
|
|
|
|
private static readonly HttpClient httpClient = new HttpClient()
|
|
{
|
|
BaseAddress = new Uri(ServiceUrls.LaaProductionAPI)
|
|
};
|
|
|
|
internal static Int32 ApplicationId(String software, params String[] functions)
|
|
=> Task
|
|
.Run(async () =>
|
|
{
|
|
var applicationId = default(Int32);
|
|
|
|
try
|
|
{
|
|
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "/LaaProductionWeb/api/software/appid"))
|
|
{
|
|
httpRequestMessage.Content = new StringContent(
|
|
mediaType: APPLICATION_JSON,
|
|
encoding: Encoding.UTF8,
|
|
content: JsonConvert.SerializeObject(new
|
|
{
|
|
windowsUser = Environment.UserName,
|
|
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>(Int32 appId, String username, String password)
|
|
=> Task
|
|
.Run(async () =>
|
|
{
|
|
var functions = default(IEnumerable<T>);
|
|
|
|
try
|
|
{
|
|
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "/LaaProductionWeb/api/software/functions"))
|
|
{
|
|
httpRequestMessage.Content = new StringContent(
|
|
mediaType: APPLICATION_JSON,
|
|
encoding: Encoding.UTF8,
|
|
content: JsonConvert.SerializeObject(new
|
|
{
|
|
appId,
|
|
username,
|
|
password,
|
|
windowsUser = Environment.UserName,
|
|
}));
|
|
|
|
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();
|
|
|
|
internal static void LogAction(String buttonText, String buttonTag, String user, Int32 appId)
|
|
=> Task
|
|
.Run(async () =>
|
|
{
|
|
try
|
|
{
|
|
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, "/LaaProductionWeb/api/software/functions"))
|
|
{
|
|
httpRequestMessage.Content = new StringContent(
|
|
mediaType: APPLICATION_JSON,
|
|
encoding: Encoding.UTF8,
|
|
content: JsonConvert.SerializeObject(new
|
|
{
|
|
WindowsUser = Environment.UserName,
|
|
Action = "ACCESS",
|
|
AppId = appId,
|
|
Message = $"User '{user}' accessed '{buttonText}' with Tag: {buttonTag}"
|
|
}));
|
|
|
|
_ = await httpClient.SendAsync(httpRequestMessage);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// TODO: Log the error message.
|
|
}
|
|
})
|
|
.ConfigureAwait(true)
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
}
|
|
}
|