using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Text; using Newtonsoft.Json; using LCU.Api.Internal.ArchiverApi.Configuration; using LCU.Library.Internal.ArchiverApi.Utilities; using LCU.Library.Internal.ArchiverApiModel.Models; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreRequests; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.ApplicationHistories; namespace LCU.Api.Internal.ArchiverApi.Services; public class CoreApiWrapperService : ICoreApiWrapperService { private readonly ArchiverApiConfig _ArchiverApiConfig; public CoreApiWrapperService(ArchiverApiConfig ArchiverApiConfig) { _ArchiverApiConfig = ArchiverApiConfig; } public async Task> GetApplicationHistories(ArchiveRequest archiveRequest) { try { var applicationHistoryRequest = new ApplicationHistoryRequest() { ApplicationNumbers = archiveRequest.ArchiveIdentifiers, EffectiveDate = archiveRequest.EffectiveDate.ToString() }; var url = SetUpUrl(applicationHistoryRequest); var client = HttpUtilities.GetClient(url); var response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); if (!response.IsSuccessStatusCode) { throw new Exception($"Issue retrieving Application History from Core Api Wrapper. " + $"Request: {JsonConvert.SerializeObject(applicationHistoryRequest)}"); } var stringResponse = await response.Content.ReadAsStringAsync(); var applicationHistories = JsonConvert.DeserializeObject>(stringResponse); return applicationHistories; } catch (Exception exception) { throw new Exception($"Failure to get application histories. Exception Message: {exception.Message}"); } } public string SetUpUrl(ApplicationHistoryRequest applicationHistoryRequest) { var endpointBuilder = new StringBuilder(); endpointBuilder.Append(_ArchiverApiConfig.CoreApiWrapperGetApplicationHistory); var propertyCounter = 0; var propertyName = nameof(applicationHistoryRequest.ApplicationNumbers); HttpUtilities.AppendListOfValuesToEndpoint(endpointBuilder, propertyCounter, applicationHistoryRequest.ApplicationNumbers, propertyName); endpointBuilder.Append($"&{nameof(applicationHistoryRequest.EffectiveDate)}={applicationHistoryRequest.EffectiveDate}"); return endpointBuilder.ToString(); } }