-
Notifications
You must be signed in to change notification settings - Fork 2
/
RefitExample.cs
88 lines (75 loc) · 3.16 KB
/
RefitExample.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Dapr.Client;
using Refit;
using ProblemDetails = Microsoft.AspNetCore.Mvc.ProblemDetails;
namespace BankClient
{
public class RefitExample : Example
{
public override string DisplayName => "Refit";
public interface IBank
{
[Get("/accounts/{accountId}")]
Task<Account> GetUser(string accountId, CancellationToken cancellationToken = default);
[Post("/deposit")]
Task<Account> Deposit([Body] Transaction transaction, CancellationToken cancellationToken = default);
[Post("/withdraw")]
Task<HttpResponseMessage> Withdraw([Body] Transaction transaction, CancellationToken cancellationToken = default);
}
public override async Task RunAsync(CancellationToken cancellationToken)
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
var settings = new RefitSettings(new SystemTextJsonContentSerializer(options));
var client = RestService.For<IBank>(new HttpClient(new ServiceInvocationHandler(new HttpClientHandler()))
{
// Using app-id as the hostname.
BaseAddress = new Uri("http://bank"),
}, settings);
// Scenario 1: Check if the account already exists.
Account? account = null;
try
{
account = await client.GetUser("17", cancellationToken);
}
catch (ApiException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
// Account does not exist.
}
Console.WriteLine($"Scenario 1: account '17' {(account is null ? "does not exist" : "already exists")}");
// Scenario 2: Deposit some money
var transaction = new Transaction()
{
Amount = 100m,
Id = "17",
};
// read updated balance
account = await client.Deposit(transaction, cancellationToken);
Console.WriteLine($"Scenario 2: account '17' has '{account?.Balance}' money");
// Scenario 3: Handle a validation error without exceptions
transaction = new Transaction()
{
Amount = 1_000_000m,
Id = "17",
};
var response = await client.Withdraw(transaction, cancellationToken);
if (response.StatusCode != HttpStatusCode.BadRequest)
{
// We don't actually expect this example to succeed - we expect a 400
Console.WriteLine("Something went wrong :(");
return;
}
var problem = await response.Content.ReadFromJsonAsync<ProblemDetails>(cancellationToken: cancellationToken);
Console.WriteLine($"Scenario 3: got the following errors:");
foreach (var kvp in problem!.Errors)
{
Console.WriteLine($"{kvp.Key}: {string.Join(", ", kvp.Value)}");
}
}
}
}