using classes; using Model; using Model.ResponseModel; using MySqlConnector; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace AwsDotnetCsharp.Services { public class AartoFineQueryService { private readonly string _baseUrl; bool errorInducedEmpty = false; private readonly MySqlConnection _con; private readonly Logger _logger; public AartoFineQueryService(string baseUrl, MySqlConnection con, Logger logger) { _baseUrl = baseUrl; _con = con; _logger = logger; } public async Task> QueryAsync(List accountIds, string filter) { List eNatisFines = new List(); var filteredEnatis = await GeteNatisIdsDataAsync(accountIds, filter); var flatArr = filteredEnatis.SelectMany(arr => arr).Distinct().Where(f => f.payment.amount != 0).ToList(); foreach (var fine in flatArr) { var paidLocalStatus = PaymentChecker.CheckLocalPaymentStatus(fine.infringementNoticeNumber, _con); eNatisFines.Add(new ENatisQueryInfrigerResponseStatusModel { infringementNoticeNumber = fine.infringementNoticeNumber, IDNumber = fine.idNumber, date = fine.date, time = fine.time, location = fine.location, vehicle = fine.vehicle, charge = fine.charge, feesList = fine.feesList, payment = fine.payment, customerPaymentAllowed = paidLocalStatus }); } return flatArr; } private async Task>> GeteNatisIdsDataAsync(List allCognitoIds, string filter) { List urlsEnatis = new List { }; errorInducedEmpty = false; IEnumerable eNatisresponses = null; List> eNatisQueryInfrigerResponses = new List> { }; var handler = new HttpClientHandler(); try { handler.ServerCertificateCustomValidationCallback = Helper.ServerCertificateCustomValidation; var enatisClient = new HttpClient(handler) { BaseAddress = new Uri(_baseUrl) }; foreach (var id in allCognitoIds) { urlsEnatis.Add($"infringement/queryinfringer?IdDocumentType={id.IdType}&IdDocumentNumber={id.IdNumber}"); } //Start requests for all of them var eNatisrequests = urlsEnatis.Select ( url => enatisClient.GetAsync(url) ).ToList(); //Wait for all the requests to finish await Task.WhenAll(eNatisrequests); //Get the responses eNatisresponses = eNatisrequests.Select ( task => task.Result ); } catch { errorInducedEmpty = true; } if (eNatisresponses != null) { foreach (var response in eNatisresponses) { // Extract the message body var content = await response.Content.ReadAsStringAsync(); string path = response.RequestMessage.RequestUri.PathAndQuery.ToString(); var id = path.Substring(path.LastIndexOf('=') + 1); if (response.IsSuccessStatusCode) { var body = JsonConvert.DeserializeObject>(content); body.ToList().ForEach(x => x.idNumber = id); eNatisQueryInfrigerResponses.Add(body); } else { errorInducedEmpty = false; _logger.WriteLog("eNatis request failed", content, response.RequestMessage.ToString(), "DB"); } } if (!string.IsNullOrEmpty(filter)) { return eNatisQueryInfrigerResponses.Where(x => x.Any(s => s.location.issAuthority.description == filter)).ToList(); } return eNatisQueryInfrigerResponses; } return eNatisQueryInfrigerResponses; } } }