Improved client handling and episode models

This commit is contained in:
2020-02-27 23:11:36 +01:00
parent 82722e77d6
commit 2d2f8b6b76
22 changed files with 540 additions and 257 deletions

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using EstusShots.Shared.Dto;
using EstusShots.Shared.Models;
namespace EstusShots.Client
@@ -13,7 +15,7 @@ namespace EstusShots.Client
{
PropertyNameCaseInsensitive = true,
};
private string ApiUrl { get; }
private HttpClient HttpClient { get; }
@@ -23,12 +25,39 @@ namespace EstusShots.Client
HttpClient = new HttpClient {Timeout = TimeSpan.FromSeconds(10)};
}
public async Task<List<Season>> GetSeasons()
public async Task<(OperationResult, List<Season>)> GetSeasons()
{
var response = HttpClient.GetAsync(ApiUrl + "seasons").Result;
var jsonData = await response.Content.ReadAsStringAsync();
var data = JsonSerializer.Deserialize<List<Season>>(jsonData, _serializerOptions);
return data;
try
{
var response = await HttpClient.GetAsync(ApiUrl + "seasons");
var jsonData = await response.Content.ReadAsStringAsync();
var data = JsonSerializer.Deserialize<List<Season>>(jsonData, _serializerOptions);
return (new OperationResult(), data);
}
catch (Exception e)
{
return (new OperationResult(e), new List<Season>());
}
}
public async Task<(OperationResult, Guid)> CreateSeason(Season season)
{
try
{
var content = new StringContent(JsonSerializer.Serialize(season), Encoding.UTF8, "application/json");
var response = await HttpClient.PostAsync(ApiUrl + "season", content);
if (!response.IsSuccessStatusCode)
{
return (new OperationResult(false, response.ReasonPhrase), Guid.Empty);
}
// TODO should give the created id
return (new OperationResult(), Guid.Empty);
}
catch (Exception e)
{
return (new OperationResult(e), Guid.Empty);
}
}
}
}