private bool PostVentaPage(VentaDTOBatch ventaBatch, out ListProcessResult result) { result = null; var client = new RestClient($"{_config.APIEndpoint}{_config.APIPostVentaDataRange}"); var postVenta = new RestRequest(Method.POST); postVenta.AddHeader("cache-control", "no-cache"); if (TokenData == null || string.IsNullOrEmpty(TokenData.access_token)) { throw new Exception("Token is null"); } postVenta.AddHeader("Authorization", $"Bearer {TokenData.access_token}"); postVenta.AddHeader("Content-Type", "application/json"); postVenta.AddJsonBody(ventaBatch); IRestResponse response = client.Execute(postVenta); if (response.StatusCode == System.Net.HttpStatusCode.Created) { // success - creation result = JsonConvert.DeserializeObject <ListProcessResult>(response.Content); return(true); } else if (response.StatusCode == System.Net.HttpStatusCode.OK) { // success - update result = JsonConvert.DeserializeObject <ListProcessResult>(response.Content); return(true); } else { // some error - error management return(false); } }
private void SendVentaPage2API(Stopwatch timerTokenRefresh, APIMethods api, VentaDTOBatch ventaListJson) { string ventaToPostJson = JsonConvert.SerializeObject(ventaListJson); Debug.WriteLine($"Sending page of records: {ventaToPostJson}"); var done = api.PostVentaPage(ventaListJson, out ListProcessResult result); if (!done) { var msg = $"ERROR enviando ventas en paginas."; Debug.WriteLine(msg); throw new Exception(msg); } else { Debug.WriteLine($"Send result info - Creates: {result.Creations}; Updates: {result.Updates}"); } if (timerTokenRefresh.Elapsed.TotalMinutes > 4) { //refresh token Debug.WriteLine("Refreshing api token"); api.SetToken(); if (api.TokenData == null) { throw new System.Exception($"Could not refresh token from WebAPI, endpoint root was {_config.APIEndpoint}"); } else { timerTokenRefresh.Restart(); } } }
private static List <VentaDTO> SendPage(APIMethods api, List <VentaDTO> ventaList, double pageSize, double pageTop, int page) { List <VentaDTO> ventaListPage = null; ventaListPage = ExtractPage(ventaList, pageSize, pageTop, page); var ventaListJson = new VentaDTOBatch { Ventas = ventaListPage }; api.PostVentaBatch(ventaListJson); return(ventaListPage); }
public void PostVentaBatch(VentaDTOBatch ventaListJson) { string ventaToPostJson = JsonConvert.SerializeObject(ventaListJson); Debug.WriteLine($"Sending page of records: {ventaToPostJson}"); var done = PostVentaPage(ventaListJson, out ListProcessResult result); if (!done) { var msg = $"ERROR enviando ventas en paginas."; Debug.WriteLine(msg); throw new Exception(msg); } else { Debug.WriteLine($"Send result info - Creates: {result.Creations}; Updates: {result.Updates}"); } }
public async Task FullTestInBatch() { var configCS = ConfigurationManager.ConnectionStrings["CS_FarmaticDB"]; if (null == configCS) { throw new Exception("No app configuration found"); } if (!int.TryParse(ConfigurationManager.AppSettings["DaysToResend"], out int daysResend)) { throw new Exception($"DaysToResend config was not found or invalid, it must be a positive int"); } ConfigAndSetStartDateToRetrieve( configCS, daysResend, out Stopwatch timerTokenRefresh, out APIMethods api, out GetLastResult _lastExportDate); DbCommand command = PrepareDbCommand(ref daysResend, _lastExportDate); //using (var reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)) // for long streams Stopwatch timer = new Stopwatch(); timer.Start(); List <VentaDTO> ventaList = new List <VentaDTO>(); using (var reader = await command.ExecuteReaderAsync()) { while (reader.Read()) { VentaDTO.TryFromDBRecord(reader, out VentaDTO venta); ventaList.Add(venta); } } int ventaCount = ventaList.Count; const double pageSize = 10; double pageTop = Math.Ceiling(ventaCount / pageSize); for (int page = 0; page < pageTop; page++) { List <VentaDTO> ventaListPage = null; if (page == pageTop - 1) { if (int.TryParse((ventaList.Count - (pageSize * (pageTop - 1))).ToString(), out int lastPageRecords)) { ventaListPage = ventaList .GetRange(int.Parse((page * pageSize).ToString()), lastPageRecords); } else { throw new Exception("ERROR: Paging last page error on type conversion"); } } else { ventaListPage = ventaList .GetRange(int.Parse((page * pageSize).ToString()), int.Parse(pageSize.ToString())); } var ventaListJson = new VentaDTOBatch { Ventas = ventaListPage }; SendVentaPage2API(timer, api, ventaListJson); } timer.Stop(); Debug.WriteLine($"Processing time: {timer.Elapsed.TotalSeconds} secs"); }