Files
dotnet-maui-workshop/MonkeyFinder/Services/MonkeyService.cs
2024-04-22 16:48:36 +02:00

33 lines
684 B
C#

using System.Net.Http.Json;
namespace MonkeyFinder.Services;
public class MonkeyService
{
HttpClient httpClient;
public MonkeyService()
{
httpClient = new HttpClient();
}
List<Monkey> monkeyList = new();
public async Task<List<Monkey>> GetMonkeys()
{
if (monkeyList?.Count > 0)
{
return monkeyList;
}
var url = "https://montemagno.com/monkeys.json";
var response = await httpClient.GetAsync(url);
if(response.IsSuccessStatusCode)
{
monkeyList = await response.Content.ReadFromJsonAsync<List<Monkey>>();
}
return monkeyList;
}
}