Код: Выделить всё
public class Tickets
{
public Ticket[] tickets { get; set; }
public string nextPageURL { get; set; }
public string previousPageURL { get; set; }
public int recordCount { get; set; }
}
public class Ticket
{
...
}
Мой текущий код:
Код: Выделить всё
private static async Task GetTicketsAsync(Action callBack = null)
{
var tickets = new List();
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"-----")));
httpClient.BaseAddress = new Uri("-----");
var nextUrl = "/api/tickets";
do
{
await httpClient.GetAsync(nextUrl)
.ContinueWith(async (ticketSearchTask) =>
{
var response = await ticketSearchTask;
if (response.IsSuccessStatusCode)
{
string jsonString = await response.Content.ReadAsStringAsync();
try
{
var result = JsonSerializer.Deserialize(jsonString);
if (result != null)
{
// Build the full list to return later after the loop.
if (result.tickets.Any())
tickets.AddRange(result.tickets.ToList());
// Run the callback method, passing the current page of data from the API.
if (callBack != null)
callBack(result);
// Get the URL for the next page
nextUrl = (result.nextPageURL != null) ? result.nextPageURL : string.Empty;
}
} catch (Exception ex)
{
Console.WriteLine($"\n We ran into an error: {ex.Message}");
nextUrl = string.Empty;
}
}
else
{
// End loop if we get an error response.
nextUrl = string.Empty;
}
});
} while (!string.IsNullOrEmpty(nextUrl));
return tickets;
}
private static void TicketsCallBack(Tickets tickets)
{
if (tickets != null && tickets.count > 0)
{
foreach (var ticket in tickets.tickets)
{
Console.WriteLine($"fetched ticket: {ticket.id}");
}
}
}
Код: Выделить всё
private static async Task GetAsync(string type, Action callBack = null, string ticketId = null)
{
var results = new List();
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"-----")));
httpClient.BaseAddress = new Uri("-----");
var nextUrl = $"/api/{type}.json";
do
{
await httpClient.GetAsync(nextUrl)
.ContinueWith(async (searchTask) =>
{
var response = await searchTask;
if (response.IsSuccessStatusCode)
{
string jsonString = await response.Content.ReadAsStringAsync();
try
{
var result = JsonSerializer.Deserialize(jsonString);
if (result != null)
{
// Build the full list to return later after the loop.
if (result.tickets.Any())
results.AddRange(result.tickets.ToList());
// Run the callback method, passing the current page of data from the API.
if (callBack != null)
callBack(result);
// Get the URL for the next page
nextUrl = (result.GetType().GetProperty("nextPageURL") != null) ? result.GetType().GetProperty("nextPageURL").ToString() : string.Empty;
}
}
catch (Exception ex)
{
Console.WriteLine($"\nWe ran into an error: {ex.Message}");
nextUrl = string.Empty;
}
}
else
{
// End loop if we get an error response.
nextUrl = string.Empty;
}
});
} while (!string.IsNullOrEmpty(nextUrl));
return results;
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... sk-generic
Мобильная версия