Skip to content

Commit

Permalink
Merge pull request #57 from JBeni/56-use-polly-library-to-make-retrie…
Browse files Browse the repository at this point in the history
…s-calls-to-the-apis

use polly to make retries calls on API
  • Loading branch information
JBeni authored Sep 22, 2023
2 parents 5e06cfe + c18edf4 commit 84fda02
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 64 deletions.
1 change: 1 addition & 0 deletions BlazorShop.WebClient/BlazorShop.WebClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.27.0" />
<PackageReference Include="MudBlazor" Version="6.0.7" />
<PackageReference Include="Polly" Version="7.2.4" />
<PackageReference Include="Radzen.Blazor" Version="3.14.2" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
Expand Down
12 changes: 10 additions & 2 deletions BlazorShop.WebClient/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public AccountService(HttpClient httpClient, ISnackbar snackBar)
/// <inheritdoc/>
public async Task<RequestResponse> ChangePassword(ChangePasswordCommand command)
{
var response = await this.HttpClient.PutAsJsonAsync("Accounts/change-password", command);
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("Accounts/change-password", command));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand All @@ -66,7 +70,11 @@ public async Task<RequestResponse> ResetPassword(ResetPasswordCommand command)
new KeyValuePair<string, string>("NewConfirmPassword", command.NewConfirmPassword),
});

var response = await this.HttpClient.PostAsync("Accounts/reset-password", data);
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.PostAsync("Accounts/reset-password", data));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand Down
49 changes: 41 additions & 8 deletions BlazorShop.WebClient/Services/CartService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public CartService(HttpClient httpClient, ISnackbar snackBar)
/// <inheritdoc/>
public async Task<RequestResponse> AddCart(CartResponse cart)
{
var response = await this.HttpClient.PostAsJsonAsync($"Carts/cart", cart);
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync($"Carts/cart", cart));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand All @@ -59,7 +63,11 @@ public async Task<RequestResponse> AddCart(CartResponse cart)
/// <inheritdoc/>
public async Task<RequestResponse> DeleteCart(int id, int userId)
{
var response = await this.HttpClient.DeleteAsync($"Carts/cart/{id}/{userId}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Carts/cart/{id}/{userId}"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand All @@ -79,7 +87,11 @@ public async Task<RequestResponse> DeleteCart(int id, int userId)
/// <inheritdoc/>
public async Task<RequestResponse> EmptyCart(int userId)
{
var response = await this.HttpClient.DeleteAsync($"Carts/carts/{userId}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Carts/carts/{userId}"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand All @@ -99,7 +111,11 @@ public async Task<RequestResponse> EmptyCart(int userId)
/// <inheritdoc/>
public async Task<CartResponse> GetCart(int id, int userId)
{
var response = await this.HttpClient.GetAsync($"Carts/cart/{id}/{userId}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.GetAsync($"Carts/cart/{id}/{userId}"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<Result<CartResponse>>(
responseResult, this.Options);
Expand All @@ -117,7 +133,11 @@ public async Task<CartResponse> GetCart(int id, int userId)
/// <inheritdoc/>
public async Task<int> GetCartCounts(int userId)
{
var response = await this.HttpClient.GetAsync($"Carts/count/{userId}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.GetAsync($"Carts/count/{userId}"));

var responseResult = await response.Content.ReadAsStringAsync();

var result = 0;
Expand All @@ -140,7 +160,11 @@ public async Task<int> GetCartCounts(int userId)
/// <inheritdoc/>
public async Task<List<CartResponse>> GetCarts(int userId)
{
var response = await this.HttpClient.GetAsync($"Carts/carts/{userId}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.GetAsync($"Carts/carts/{userId}"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<Result<CartResponse>>(
responseResult, this.Options);
Expand All @@ -158,7 +182,11 @@ public async Task<List<CartResponse>> GetCarts(int userId)
/// <inheritdoc/>
public async Task<RequestResponse> UpdateCart(CartResponse cart)
{
var response = await this.HttpClient.PutAsJsonAsync($"Carts/cart", cart);
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync($"Carts/cart", cart));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand All @@ -179,7 +207,12 @@ public async Task<RequestResponse> UpdateCart(CartResponse cart)
public async Task<string> Checkout(int userId)
{
var carts = await this.GetCarts(userId);
var response = await this.HttpClient.PostAsJsonAsync("Payments/checkout", carts.ToList());

var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync("Payments/checkout", carts.ToList()));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand Down
30 changes: 25 additions & 5 deletions BlazorShop.WebClient/Services/ClotheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public ClotheService(HttpClient httpClient, ISnackbar snackBar)
/// <inheritdoc/>
public async Task<List<ClotheResponse>> GetClothes()
{
var response = await this.HttpClient.GetAsync("Clothes/clothes");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.GetAsync("Clothes/clothes"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<Result<ClotheResponse>>(
responseResult, this.Options);
Expand All @@ -57,7 +61,11 @@ public async Task<List<ClotheResponse>> GetClothes()
/// <inheritdoc/>
public async Task<ClotheResponse> GetClothe(int id)
{
var response = await this.HttpClient.GetAsync($"Clothes/clothe/{id}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.GetAsync($"Clothes/clothe/{id}"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<Result<ClotheResponse>>(
responseResult, this.Options);
Expand All @@ -75,7 +83,11 @@ public async Task<ClotheResponse> GetClothe(int id)
/// <inheritdoc/>
public async Task<RequestResponse> AddClothe(ClotheResponse clothe)
{
var response = await this.HttpClient.PostAsJsonAsync("Clothes/clothe", clothe);
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync("Clothes/clothe", clothe));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand All @@ -95,7 +107,11 @@ public async Task<RequestResponse> AddClothe(ClotheResponse clothe)
/// <inheritdoc/>
public async Task<RequestResponse> UpdateClothe(ClotheResponse clothe)
{
var response = await this.HttpClient.PutAsJsonAsync("Clothes/clothe", clothe);
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("Clothes/clothe", clothe));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand All @@ -115,7 +131,11 @@ public async Task<RequestResponse> UpdateClothe(ClotheResponse clothe)
/// <inheritdoc/>
public async Task<RequestResponse> DeleteClothe(int id)
{
var response = await this.HttpClient.DeleteAsync($"Clothes/clothe/{id}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Clothes/clothe/{id}"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand Down
30 changes: 25 additions & 5 deletions BlazorShop.WebClient/Services/MusicService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public MusicService(HttpClient httpClient, ISnackbar snackBar)
/// <inheritdoc/>
public async Task<List<MusicResponse>> GetMusics()
{
var response = await this.HttpClient.GetAsync("Musics/musics");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.GetAsync("Musics/musics"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<Result<MusicResponse>>(
responseResult, this.Options);
Expand All @@ -57,7 +61,11 @@ public async Task<List<MusicResponse>> GetMusics()
/// <inheritdoc/>
public async Task<MusicResponse> GetMusic(int id)
{
var response = await this.HttpClient.GetAsync($"Musics/music/{id}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.GetAsync($"Musics/music/{id}"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<Result<MusicResponse>>(
responseResult, this.Options);
Expand All @@ -75,7 +83,11 @@ public async Task<MusicResponse> GetMusic(int id)
/// <inheritdoc/>
public async Task<RequestResponse> AddMusic(MusicResponse music)
{
var response = await this.HttpClient.PostAsJsonAsync("Musics/music", music);
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync("Musics/music", music));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand All @@ -95,7 +107,11 @@ public async Task<RequestResponse> AddMusic(MusicResponse music)
/// <inheritdoc/>
public async Task<RequestResponse> UpdateMusic(MusicResponse music)
{
var response = await this.HttpClient.PutAsJsonAsync("Musics/music", music);
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("Musics/music", music));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand All @@ -115,7 +131,11 @@ public async Task<RequestResponse> UpdateMusic(MusicResponse music)
/// <inheritdoc/>
public async Task<RequestResponse> DeleteMusic(int id)
{
var response = await this.HttpClient.DeleteAsync($"Musics/music/{id}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Musics/music/{id}"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand Down
30 changes: 25 additions & 5 deletions BlazorShop.WebClient/Services/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public OrderService(HttpClient httpClient, ISnackbar snackBar)
/// <inheritdoc/>
public async Task<List<OrderResponse>> GetOrders(string userEmail)
{
var response = await this.HttpClient.GetAsync($"Orders/orders/{userEmail}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.GetAsync($"Orders/orders/{userEmail}"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<Result<OrderResponse>>(
responseResult, this.Options);
Expand All @@ -57,7 +61,11 @@ public async Task<List<OrderResponse>> GetOrders(string userEmail)
/// <inheritdoc/>
public async Task<OrderResponse> GetOrder(int id, string userEmail)
{
var response = await this.HttpClient.GetAsync($"Orders/order/{id}/{userEmail}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.GetAsync($"Orders/order/{id}/{userEmail}"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<Result<OrderResponse>>(
responseResult, this.Options);
Expand All @@ -75,7 +83,11 @@ public async Task<OrderResponse> GetOrder(int id, string userEmail)
/// <inheritdoc/>
public async Task<RequestResponse> AddOrder(OrderResponse order)
{
var response = await this.HttpClient.PostAsJsonAsync("Orders/order", order);
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync("Orders/order", order));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand All @@ -95,7 +107,11 @@ public async Task<RequestResponse> AddOrder(OrderResponse order)
/// <inheritdoc/>
public async Task<RequestResponse> UpdateOrder(OrderResponse order)
{
var response = await this.HttpClient.PutAsJsonAsync("Orders/order", order);
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("Orders/order", order));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand All @@ -115,7 +131,11 @@ public async Task<RequestResponse> UpdateOrder(OrderResponse order)
/// <inheritdoc/>
public async Task<RequestResponse> DeleteOrder(int id)
{
var response = await this.HttpClient.DeleteAsync($"Orders/order/{id}");
var response = await Policy<HttpResponseMessage>
.Handle<Exception>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1))
.ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Orders/order/{id}"));

var responseResult = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RequestResponse>(
responseResult, this.Options);
Expand Down
Loading

0 comments on commit 84fda02

Please sign in to comment.