12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- 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<Post> posts;
- public KemonoHttpClient()
- {
- httpClient = new HttpClient()
- {
- BaseAddress = new Uri("https://kemono.party")
- };
-
- posts = new List<Post>();
- }
- public async Task<List<Creator>> 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<List<Creator>>(jsonString);
- }
- }
- public async Task<List<Post>> 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<List<Post>>(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;
- }
- }
- }
- }
|