-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathPokeClient.cs
More file actions
33 lines (27 loc) · 752 Bytes
/
PokeClient.cs
File metadata and controls
33 lines (27 loc) · 752 Bytes
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
using System.Net.Http.Json;
using Dunet;
namespace PokemonClient;
public class PokeClient(HttpClient client)
{
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);
}