forked from domn1995/dunet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PokeClient.cs
37 lines (29 loc) · 844 Bytes
/
PokeClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using Dunet;
using System.Net.Http.Json;
namespace PokemonClient;
public class PokeClient
{
private readonly HttpClient client;
public PokeClient(HttpClient httpClient) => client = httpClient;
public async Task<Result<Exception, Pokemon>> GetPokemonAsync(string name)
{
var fetch = () => client.GetFromJsonAsync<Pokemon>(name);
try
{
return await fetch() is Pokemon pokemon
? pokemon
: new Exception($"Unable to retrieve pokemon '{name}'.");
}
catch (Exception ex)
{
return ex;
}
}
}
public record Pokemon(int Id, string Name, int Weight, int Height);
[Union]
public partial record Result<TFailure, TSuccess>
{
partial record Success(TSuccess Value);
partial record Failure(TFailure Error);
}