Aqui esta parte de mi codigo el cual include 2 funciones.
La funcion getUsers se repite constantemente y genera una y otra vez el mismo archivo sin parar.
public static void Main(string[] args)
{
try
{
getTickets().Wait();
getUsers().Wait();
}
catch (Exception ex)
{
AddLog(string.Format(ex.ToString()));
}
}
public static async Task getTickets()
{
string firstTicketURL = "http://localhost/testapi/testTickets.json";
DataTable table = new DataTable();
var textWriter = File.CreateText(@"test.csv");
var csv = new CsvWriter(textWriter);
csv.Configuration.ShouldQuote = (field, context) => true;
var counter = 0;
try
{
while (firstTicketURL != "")
{
counter++;
var client = new RestClient(firstTicketURL);
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var content = response.Content;
string resultJSonRequest = Convert.ToString(content);
var ticketContainer = JsonConvert.DeserializeObject<TicketContainer>(resultJSonRequest);
var tickets = new List<Dictionary<string, object>>();
foreach (var ticket in ticketContainer.tickets)
{
var ticketAsJsonString = JsonConvert.SerializeObject(ticket);
var flatTicket = JsonHelper.DeserializeAndFlatten(ticketAsJsonString);
tickets.Add(flatTicket);
}
var headers = tickets
.Select(x => x.Keys.Cast<string>())
.Aggregate(Enumerable.Empty<string>(), (aggregate, entry) => aggregate.Union(entry))
.Where(x => new string[] { "id", "subject"}.Contains(x.ToLower()))
.ToList();
table = new DataTable();
foreach (var header in headers)
{
table.Columns.Add(header);
}
foreach (var ticket in tickets)
{
var row = table.NewRow();
foreach (var entry in ticket)
{
if (headers.Contains(entry.Key, StringComparer.InvariantCultureIgnoreCase))
{
row[entry.Key] = entry.Value;
}
}
table.Rows.Add(row);
}
if (counter == 1)
{
foreach (DataColumn column in table.Columns)
{
csv.WriteField(column.ColumnName);
}
csv.NextRecord();
}
foreach (DataRow row in table.Rows)
{
for (var p = 0; p < table.Columns.Count; p++)
{
csv.WriteField(row[p]);
}
csv.NextRecord();
}
}
await getTickets();
}
catch (Exception e)
{
AddLog(e.Message);
}
}
public static async Task getUsers()
{
try
{
var client = new RestClient("http://localhost/testapi/testUsers.json");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var content = response.Content;
string resultJSonRequest = Convert.ToString(content);
var ticketContainer = JsonConvert.DeserializeObject<TicketContainer>(resultJSonRequest);
var tickets = new List<Dictionary<string, object>>();
foreach (var ticket in ticketContainer.organizations)
{
var ticketAsJsonString = JsonConvert.SerializeObject(ticket);
var flatTicket = JsonHelper.DeserializeAndFlatten(ticketAsJsonString);
tickets.Add(flatTicket);
}
DataTable table = new DataTable();
var headers = tickets
.Select(x => x.Keys.Cast<string>())
.Aggregate(Enumerable.Empty<string>(), (aggregate, entry) => aggregate.Union(entry))
.Where(x => new string[] { "id", "name" }.Contains(x.ToLower()))
.ToList();
foreach (var header in headers)
{
table.Columns.Add(header);
}
foreach (var ticket in tickets)
{
var row = table.NewRow();
foreach (var entry in ticket)
{
if (headers.Contains(entry.Key, StringComparer.InvariantCultureIgnoreCase))
{
row[entry.Key] = entry.Value;
}
}
table.Rows.Add(row);
}
using (var textWriter = new StreamWriter(File.OpenWrite(@"users.csv")))
{
using (var csv = new CsvWriter(textWriter))
{
csv.Configuration.ShouldQuote = (field, context) => true;
foreach (DataColumn column in table.Columns)
{
csv.WriteField(column.ColumnName);
}
csv.NextRecord();
foreach (DataRow row in table.Rows)
{
for (var i = 0; i < table.Columns.Count; i++)
{
csv.WriteField(row[i]);
}
csv.NextRecord();
}
}
}
await getUsers();
}
catch (Exception e)
{
AddLog(e.Message);
}
}