using System; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; using VeloeKemonoPartyApp.Data; using System.Text.Json; namespace VeloeKemonoPartyApp.Services { public class KemonoHttpClient { public HttpClient httpClient; public List posts; public KemonoHttpClient() { httpClient = new HttpClient() { BaseAddress = new Uri("https://kemono.party") }; posts = new List(); } public async Task> GetCreatorsList() { using (HttpResponseMessage response = await httpClient.GetAsync("https://kemono.party/api/creators")) using (HttpContent content = response.Content) { string jsonString = await response.Content.ReadAsStringAsync(); Preferences.Set("creators", jsonString); // ... Read the string. return JsonSerializer.Deserialize>(jsonString); } } public async Task> GetPostsList(string id, string service, int start) { using (HttpResponseMessage response = await httpClient.GetAsync($"https://kemono.party/api/{service}/user/{id}?o={start}")) using (HttpContent content = response.Content) { string jsonString = await response.Content.ReadAsStringAsync(); // ... Read the string. return JsonSerializer.Deserialize>(jsonString); } } public async Task GetImageAsync(string link, string toFolder, string toImage) { try { using (var s = await httpClient.GetStreamAsync(link)) { if (!Directory.Exists(toFolder)) Directory.CreateDirectory(toFolder); using (var fs = new FileStream(toImage, FileMode.CreateNew)) { await s.CopyToAsync(fs); } } } catch (Exception) { System.IO.File.Delete(toImage); throw; } } } }