-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
56 lines (52 loc) · 2.14 KB
/
Program.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
#region Configuration
var builder = new ConfigurationBuilder();
var memoryCollection = new Dictionary<string, string>();
memoryCollection.Add("X-RapidAPI-Key", "<PutYourApiKeyHere>");
memoryCollection.Add("HearthStoneBaseAddress", "https://omgvamp-hearthstone-v1.p.rapidapi.com");
builder.AddInMemoryCollection(memoryCollection);
var configuration = builder.Build();
var serviceCollection = new ServiceCollection();
#endregion Configuration
#region Dependency Injection
serviceCollection.AddScoped<HearthstoneApiKeyInjectorDelegatingHandler>();
serviceCollection.AddSingleton<IConfiguration>(configuration);
var httpClientBuilder = serviceCollection
.AddRefitClient<IHearthStoneApi>(new RefitSettings
{
ContentSerializer = new NewtonsoftJsonContentSerializer(new JsonSerializerSettings
{
Converters = { new StringEnumConverter() },
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
})
})
.ConfigureHttpClient((s, c) =>
{
var config = s.GetService<IConfiguration>();
var baseAddress = config.GetRequiredSection("HearthStoneBaseAddress").Value;
c.BaseAddress = new Uri(baseAddress);
});
#endregion Dependency Injection
#region Microsoft.Extensions.Http.Polly
var retryPolicy = HttpPolicyExtensions
.HandleTransientHttpError()
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
.WaitAndRetryAsync(3, retryAttempt =>
{
retryAttempt.Print("Attempt: ");
return TimeSpan.FromSeconds(Math.Pow(2, retryAttempt));
});
httpClientBuilder.AddHttpMessageHandler<HearthstoneApiKeyInjectorDelegatingHandler>();
httpClientBuilder.AddPolicyHandler(retryPolicy);
#endregion Microsoft.Extensions.Http.Polly
var serviceProvider = serviceCollection.BuildServiceProvider();
var hearthstoneApiClient = serviceProvider.GetRequiredService<IHearthStoneApi>();
try
{
var cards = await hearthstoneApiClient.GetCardsByName("xpto");
cards.PrintJson();
}
catch (ApiException e)
{
(await e.GetContentAsAsync<HearthstoneError>()).Print("GetContentAsAsync: ");
}