79 lines
2.6 KiB
C#
79 lines
2.6 KiB
C#
using LCU.API.Internal.Operations.IncentiveApi.Configuration;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Models.Dayforce;
|
|
using System.Text;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace LCU.API.Internal.Operations.IncentiveApi.Services.Dayforce;
|
|
|
|
public class DayForceClientService : IDayForceClientService
|
|
{
|
|
private readonly DayForceConfiguration _dayForceConfig;
|
|
private readonly Uri _baseUri;
|
|
|
|
public DayForceClientService(DayForceConfiguration dayForceConfig)
|
|
{
|
|
_dayForceConfig = dayForceConfig;
|
|
_baseUri = new Uri(_dayForceConfig.Url + "/" +
|
|
_dayForceConfig.DayForceClientName + "/" +
|
|
_dayForceConfig.DayForceApiVersion);
|
|
}
|
|
|
|
public async Task<Report> GetReportData(string reportXRefCode)
|
|
{
|
|
var url = _baseUri.ToString() + "/Reports/" + reportXRefCode;
|
|
var response = await GetHttpResponseAsync(url, true);
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
try
|
|
{
|
|
var jsonString = await response.Content.ReadAsStringAsync();
|
|
var payload = JsonConvert.DeserializeObject<Payload<Report>>(jsonString);
|
|
return payload.Data;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error Deserializing Dayforce Report");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Log.Error("Bad Status Code retrieving report data from Dayforce {0}", response.ToString());
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public async Task<HttpResponseMessage> GetHttpResponseAsync(string apiUrl, bool attemptRedirect = false)
|
|
{
|
|
var client = TrustingHttpClient.GetClient(apiUrl);
|
|
var request = new HttpRequestMessage(HttpMethod.Get, apiUrl);
|
|
request.Headers.Authorization = CreateBasicAuthorizationHeader(request,
|
|
_dayForceConfig.DayForceUsername,
|
|
_dayForceConfig.DayForcePassword);
|
|
|
|
var response = await client.SendAsync(request);
|
|
|
|
if (attemptRedirect)
|
|
{
|
|
response = await GetHttpResponseAsync(response.RequestMessage.RequestUri.ToString(), false);
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
public AuthenticationHeaderValue CreateBasicAuthorizationHeader(HttpRequestMessage request, string userName,
|
|
string password)
|
|
{
|
|
if (!(string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)))
|
|
{
|
|
return new AuthenticationHeaderValue(
|
|
"Basic", Convert.ToBase64String(
|
|
Encoding.UTF8.GetBytes(userName + ":" + password)));
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|