Example #1
0
        private void SequentialSend(StringBuilder sbEvent, Stopwatch timerTokenRefresh, APIMethods api, DbCommand command)
        {
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    // conversion to DTO
                    if (VentaDTO.TryFromDBRecord(reader, out VentaDTO venta))
                    {
                        var ventaStr = venta.ToString();
                        Log(ventaStr, false);
                        sbEvent.Append(ventaStr);
                        var result = api.PostVenta(venta);
                        if (!result)
                        {
                            var msg = $"ERROR while sending IdVenta {reader["IdentificadorVenta"]}";
                            Log(msg, false);
                            sbEvent.AppendLine(" - ERROR");
                            LogToEventViewer(new Exception(msg));
                            throw new Exception(msg);
                        }
                        else
                        {
                            sbEvent.AppendLine(" - OK");
                        }

                        RefreshAPIToken(timerTokenRefresh, api);
                    }
                    else
                    {
                        var msg = $"ERROR while building DTO for IdVenta record with Id: {reader["IdentificadorVenta"]}";
                        Log(msg);
                        var ex = new Exception(msg);
                        LogToEventViewer(ex);
                        throw ex;
                    }
                }
            }
        }
Example #2
0
        private void SendVenta2API(Stopwatch timerTokenRefresh, APIMethods api, DbDataReader reader)
        {
            // conversion to DTO
            if (VentaDTO.TryFromDBRecord(reader, out VentaDTO venta))
            {
                // convertir a JSON (WebAPI will receive this)
                Debug.WriteLine(venta.ToString());
                var result = api.PostVenta(venta);
                if (!result)
                {
                    var msg = $"ERROR enviando IdVenta {reader["IdentificadorVenta"]}";
                    Debug.WriteLine(msg);
                    throw new Exception(msg);
                }

                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();
                    }
                }
            }
            else
            {
                var msg = $"ERROR de conversion a DTO en IdVenta {reader["IdentificadorVenta"]}";
                Debug.WriteLine(msg);
                throw new Exception(msg);
            }
        }