From c18edf47b0e5d75a8245d35d936d8373fea458d6 Mon Sep 17 00:00:00 2001 From: Jitca Beniamin Date: Fri, 22 Sep 2023 22:18:41 +0100 Subject: [PATCH] use polly to make retries calls on API --- .../BlazorShop.WebClient.csproj | 1 + .../Services/AccountService.cs | 12 ++++- BlazorShop.WebClient/Services/CartService.cs | 49 ++++++++++++++++--- .../Services/ClotheService.cs | 30 ++++++++++-- BlazorShop.WebClient/Services/MusicService.cs | 30 ++++++++++-- BlazorShop.WebClient/Services/OrderService.cs | 30 ++++++++++-- .../Services/ReceiptService.cs | 30 ++++++++++-- BlazorShop.WebClient/Services/RoleService.cs | 37 +++++++++++--- .../Services/SubscriberService.cs | 36 +++++++++++--- .../Services/SubscriptionService.cs | 30 ++++++++++-- .../Services/TodoItemService.cs | 24 +++++++-- .../Services/TodoListService.cs | 30 ++++++++++-- BlazorShop.WebClient/Services/UserService.cs | 48 +++++++++++++++--- BlazorShop.WebClient/_Imports.cs | 1 + 14 files changed, 324 insertions(+), 64 deletions(-) diff --git a/BlazorShop.WebClient/BlazorShop.WebClient.csproj b/BlazorShop.WebClient/BlazorShop.WebClient.csproj index 957ac102..92986b0e 100644 --- a/BlazorShop.WebClient/BlazorShop.WebClient.csproj +++ b/BlazorShop.WebClient/BlazorShop.WebClient.csproj @@ -25,6 +25,7 @@ + diff --git a/BlazorShop.WebClient/Services/AccountService.cs b/BlazorShop.WebClient/Services/AccountService.cs index e7fc2524..bad1580d 100644 --- a/BlazorShop.WebClient/Services/AccountService.cs +++ b/BlazorShop.WebClient/Services/AccountService.cs @@ -39,7 +39,11 @@ public AccountService(HttpClient httpClient, ISnackbar snackBar) /// public async Task ChangePassword(ChangePasswordCommand command) { - var response = await this.HttpClient.PutAsJsonAsync("Accounts/change-password", command); + var response = await Policy + .Handle() + .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( responseResult, this.Options); @@ -66,7 +70,11 @@ public async Task ResetPassword(ResetPasswordCommand command) new KeyValuePair("NewConfirmPassword", command.NewConfirmPassword), }); - var response = await this.HttpClient.PostAsync("Accounts/reset-password", data); + var response = await Policy + .Handle() + .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( responseResult, this.Options); diff --git a/BlazorShop.WebClient/Services/CartService.cs b/BlazorShop.WebClient/Services/CartService.cs index ddd0cb51..fcb1b1dc 100644 --- a/BlazorShop.WebClient/Services/CartService.cs +++ b/BlazorShop.WebClient/Services/CartService.cs @@ -39,7 +39,11 @@ public CartService(HttpClient httpClient, ISnackbar snackBar) /// public async Task AddCart(CartResponse cart) { - var response = await this.HttpClient.PostAsJsonAsync($"Carts/cart", cart); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync($"Carts/cart", cart)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -59,7 +63,11 @@ public async Task AddCart(CartResponse cart) /// public async Task DeleteCart(int id, int userId) { - var response = await this.HttpClient.DeleteAsync($"Carts/cart/{id}/{userId}"); + var response = await Policy + .Handle() + .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( responseResult, this.Options); @@ -79,7 +87,11 @@ public async Task DeleteCart(int id, int userId) /// public async Task EmptyCart(int userId) { - var response = await this.HttpClient.DeleteAsync($"Carts/carts/{userId}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Carts/carts/{userId}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -99,7 +111,11 @@ public async Task EmptyCart(int userId) /// public async Task GetCart(int id, int userId) { - var response = await this.HttpClient.GetAsync($"Carts/cart/{id}/{userId}"); + var response = await Policy + .Handle() + .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>( responseResult, this.Options); @@ -117,7 +133,11 @@ public async Task GetCart(int id, int userId) /// public async Task GetCartCounts(int userId) { - var response = await this.HttpClient.GetAsync($"Carts/count/{userId}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Carts/count/{userId}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = 0; @@ -140,7 +160,11 @@ public async Task GetCartCounts(int userId) /// public async Task> GetCarts(int userId) { - var response = await this.HttpClient.GetAsync($"Carts/carts/{userId}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Carts/carts/{userId}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -158,7 +182,11 @@ public async Task> GetCarts(int userId) /// public async Task UpdateCart(CartResponse cart) { - var response = await this.HttpClient.PutAsJsonAsync($"Carts/cart", cart); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync($"Carts/cart", cart)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -179,7 +207,12 @@ public async Task UpdateCart(CartResponse cart) public async Task Checkout(int userId) { var carts = await this.GetCarts(userId); - var response = await this.HttpClient.PostAsJsonAsync("Payments/checkout", carts.ToList()); + + var response = await Policy + .Handle() + .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( responseResult, this.Options); diff --git a/BlazorShop.WebClient/Services/ClotheService.cs b/BlazorShop.WebClient/Services/ClotheService.cs index 3c4f711e..1644cbc7 100644 --- a/BlazorShop.WebClient/Services/ClotheService.cs +++ b/BlazorShop.WebClient/Services/ClotheService.cs @@ -39,7 +39,11 @@ public ClotheService(HttpClient httpClient, ISnackbar snackBar) /// public async Task> GetClothes() { - var response = await this.HttpClient.GetAsync("Clothes/clothes"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync("Clothes/clothes")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -57,7 +61,11 @@ public async Task> GetClothes() /// public async Task GetClothe(int id) { - var response = await this.HttpClient.GetAsync($"Clothes/clothe/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Clothes/clothe/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -75,7 +83,11 @@ public async Task GetClothe(int id) /// public async Task AddClothe(ClotheResponse clothe) { - var response = await this.HttpClient.PostAsJsonAsync("Clothes/clothe", clothe); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync("Clothes/clothe", clothe)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -95,7 +107,11 @@ public async Task AddClothe(ClotheResponse clothe) /// public async Task UpdateClothe(ClotheResponse clothe) { - var response = await this.HttpClient.PutAsJsonAsync("Clothes/clothe", clothe); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("Clothes/clothe", clothe)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -115,7 +131,11 @@ public async Task UpdateClothe(ClotheResponse clothe) /// public async Task DeleteClothe(int id) { - var response = await this.HttpClient.DeleteAsync($"Clothes/clothe/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Clothes/clothe/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); diff --git a/BlazorShop.WebClient/Services/MusicService.cs b/BlazorShop.WebClient/Services/MusicService.cs index b9fc0856..87ae0397 100644 --- a/BlazorShop.WebClient/Services/MusicService.cs +++ b/BlazorShop.WebClient/Services/MusicService.cs @@ -39,7 +39,11 @@ public MusicService(HttpClient httpClient, ISnackbar snackBar) /// public async Task> GetMusics() { - var response = await this.HttpClient.GetAsync("Musics/musics"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync("Musics/musics")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -57,7 +61,11 @@ public async Task> GetMusics() /// public async Task GetMusic(int id) { - var response = await this.HttpClient.GetAsync($"Musics/music/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Musics/music/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -75,7 +83,11 @@ public async Task GetMusic(int id) /// public async Task AddMusic(MusicResponse music) { - var response = await this.HttpClient.PostAsJsonAsync("Musics/music", music); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync("Musics/music", music)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -95,7 +107,11 @@ public async Task AddMusic(MusicResponse music) /// public async Task UpdateMusic(MusicResponse music) { - var response = await this.HttpClient.PutAsJsonAsync("Musics/music", music); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("Musics/music", music)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -115,7 +131,11 @@ public async Task UpdateMusic(MusicResponse music) /// public async Task DeleteMusic(int id) { - var response = await this.HttpClient.DeleteAsync($"Musics/music/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Musics/music/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); diff --git a/BlazorShop.WebClient/Services/OrderService.cs b/BlazorShop.WebClient/Services/OrderService.cs index 905c8dca..9cc77e0d 100644 --- a/BlazorShop.WebClient/Services/OrderService.cs +++ b/BlazorShop.WebClient/Services/OrderService.cs @@ -39,7 +39,11 @@ public OrderService(HttpClient httpClient, ISnackbar snackBar) /// public async Task> GetOrders(string userEmail) { - var response = await this.HttpClient.GetAsync($"Orders/orders/{userEmail}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Orders/orders/{userEmail}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -57,7 +61,11 @@ public async Task> GetOrders(string userEmail) /// public async Task GetOrder(int id, string userEmail) { - var response = await this.HttpClient.GetAsync($"Orders/order/{id}/{userEmail}"); + var response = await Policy + .Handle() + .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>( responseResult, this.Options); @@ -75,7 +83,11 @@ public async Task GetOrder(int id, string userEmail) /// public async Task AddOrder(OrderResponse order) { - var response = await this.HttpClient.PostAsJsonAsync("Orders/order", order); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync("Orders/order", order)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -95,7 +107,11 @@ public async Task AddOrder(OrderResponse order) /// public async Task UpdateOrder(OrderResponse order) { - var response = await this.HttpClient.PutAsJsonAsync("Orders/order", order); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("Orders/order", order)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -115,7 +131,11 @@ public async Task UpdateOrder(OrderResponse order) /// public async Task DeleteOrder(int id) { - var response = await this.HttpClient.DeleteAsync($"Orders/order/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Orders/order/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); diff --git a/BlazorShop.WebClient/Services/ReceiptService.cs b/BlazorShop.WebClient/Services/ReceiptService.cs index 04aedda0..3b066220 100644 --- a/BlazorShop.WebClient/Services/ReceiptService.cs +++ b/BlazorShop.WebClient/Services/ReceiptService.cs @@ -39,7 +39,11 @@ public ReceiptService(HttpClient httpClient, ISnackbar snackBar) /// public async Task> GetReceipts(string userEmail) { - var response = await this.HttpClient.GetAsync($"Receipts/receipts/{userEmail}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Receipts/receipts/{userEmail}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -57,7 +61,11 @@ public async Task> GetReceipts(string userEmail) /// public async Task GetReceipt(int id, string userEmail) { - var response = await this.HttpClient.GetAsync($"Receipts/receipt/{id}/{userEmail}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Receipts/receipt/{id}/{userEmail}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -75,7 +83,11 @@ public async Task GetReceipt(int id, string userEmail) /// public async Task AddReceipt(ReceiptResponse receipt) { - var response = await this.HttpClient.PostAsJsonAsync("Receipts/receipt", receipt); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync("Receipts/receipt", receipt)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -95,7 +107,11 @@ public async Task AddReceipt(ReceiptResponse receipt) /// public async Task UpdateReceipt(ReceiptResponse receipt) { - var response = await this.HttpClient.PutAsJsonAsync("Receipts/receipt", receipt); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("Receipts/receipt", receipt)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -115,7 +131,11 @@ public async Task UpdateReceipt(ReceiptResponse receipt) /// public async Task DeleteReceipt(int id) { - var response = await this.HttpClient.DeleteAsync($"Receipts/receipt/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Receipts/receipt/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); diff --git a/BlazorShop.WebClient/Services/RoleService.cs b/BlazorShop.WebClient/Services/RoleService.cs index 3750cfbb..0a00cfca 100644 --- a/BlazorShop.WebClient/Services/RoleService.cs +++ b/BlazorShop.WebClient/Services/RoleService.cs @@ -39,7 +39,11 @@ public RoleService(HttpClient httpClient, ISnackbar snackBar) /// public async Task AddRole(RoleResponse role) { - var response = await this.HttpClient.PostAsJsonAsync($"Roles/role", role); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync($"Roles/role", role)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -59,7 +63,11 @@ public async Task AddRole(RoleResponse role) /// public async Task DeleteRole(int id) { - var response = await this.HttpClient.DeleteAsync($"Roles/role/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Roles/role/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -79,7 +87,11 @@ public async Task DeleteRole(int id) /// public async Task GetRole(int id) { - var response = await this.HttpClient.GetAsync($"Roles/role/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Roles/role/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -97,7 +109,11 @@ public async Task GetRole(int id) /// public async Task> GetRoles() { - var response = await this.HttpClient.GetAsync("Roles/roles"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync("Roles/roles")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -115,7 +131,11 @@ public async Task> GetRoles() /// public async Task> GetRolesForAdmin() { - var response = await this.HttpClient.GetAsync("Roles/rolesAdmin"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync("Roles/rolesAdmin")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -138,7 +158,12 @@ public async Task UpdateRole(RoleResponse role) Id = role.Id, Name = role.Name, }; - var response = await this.HttpClient.PutAsJsonAsync($"Roles/role", data); + + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync($"Roles/role", data)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); diff --git a/BlazorShop.WebClient/Services/SubscriberService.cs b/BlazorShop.WebClient/Services/SubscriberService.cs index 8060545c..87227945 100644 --- a/BlazorShop.WebClient/Services/SubscriberService.cs +++ b/BlazorShop.WebClient/Services/SubscriberService.cs @@ -39,7 +39,11 @@ public SubscriberService(HttpClient httpClient, ISnackbar snackBar) /// public async Task> GetSubscribers() { - var response = await this.HttpClient.GetAsync("Subscribers/subscribers"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync("Subscribers/subscribers")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -57,7 +61,11 @@ public async Task> GetSubscribers() /// public async Task> GetUserAllSubscribers(int userId) { - var response = await this.HttpClient.GetAsync($"Subscribers/subscribers/{userId}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Subscribers/subscribers/{userId}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -75,7 +83,11 @@ public async Task> GetUserAllSubscribers(int userId) /// public async Task GetUserSubscriber(int userId) { - var response = await this.HttpClient.GetAsync($"Subscribers/subscriber/{userId}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Subscribers/subscriber/{userId}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -93,7 +105,11 @@ public async Task GetUserSubscriber(int userId) /// public async Task AddSubscriber(SubscriberResponse subscriber) { - var response = await this.HttpClient.PostAsJsonAsync("Subscribers/subscriber", subscriber); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync("Subscribers/subscriber", subscriber)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -113,7 +129,11 @@ public async Task AddSubscriber(SubscriberResponse subscriber) /// public async Task UpdateSubscriber(SubscriberResponse subscriber) { - var response = await this.HttpClient.PutAsJsonAsync("Subscribers/subscriber", subscriber); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("Subscribers/subscriber", subscriber)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -133,7 +153,11 @@ public async Task UpdateSubscriber(SubscriberResponse subscribe /// public async Task DeleteSubscriber(int id) { - var response = await this.HttpClient.DeleteAsync($"Subscribers/subscriber/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Subscribers/subscriber/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); diff --git a/BlazorShop.WebClient/Services/SubscriptionService.cs b/BlazorShop.WebClient/Services/SubscriptionService.cs index 43cd2bb9..d031720a 100644 --- a/BlazorShop.WebClient/Services/SubscriptionService.cs +++ b/BlazorShop.WebClient/Services/SubscriptionService.cs @@ -39,7 +39,11 @@ public SubscriptionService(HttpClient httpClient, ISnackbar snackBar) /// public async Task> GetSubscriptions() { - var response = await this.HttpClient.GetAsync("Subscriptions/subscriptions"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync("Subscriptions/subscriptions")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -57,7 +61,11 @@ public async Task> GetSubscriptions() /// public async Task GetSubscription(int id) { - var response = await this.HttpClient.GetAsync($"Subscriptions/subscription/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Subscriptions/subscription/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -75,7 +83,11 @@ public async Task GetSubscription(int id) /// public async Task AddSubscription(SubscriptionResponse subscription) { - var response = await this.HttpClient.PostAsJsonAsync("Subscriptions/subscription", subscription); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync("Subscriptions/subscription", subscription)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -95,7 +107,11 @@ public async Task AddSubscription(SubscriptionResponse subscrip /// public async Task UpdateSubscription(SubscriptionResponse subscription) { - var response = await this.HttpClient.PutAsJsonAsync("Subscriptions/subscription", subscription); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("Subscriptions/subscription", subscription)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -115,7 +131,11 @@ public async Task UpdateSubscription(SubscriptionResponse subsc /// public async Task DeleteSubscription(int id) { - var response = await this.HttpClient.DeleteAsync($"Subscriptions/subscription/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Subscriptions/subscription/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); diff --git a/BlazorShop.WebClient/Services/TodoItemService.cs b/BlazorShop.WebClient/Services/TodoItemService.cs index a49c12d6..9c229d11 100644 --- a/BlazorShop.WebClient/Services/TodoItemService.cs +++ b/BlazorShop.WebClient/Services/TodoItemService.cs @@ -39,7 +39,11 @@ public TodoItemService(HttpClient httpClient, ISnackbar snackBar) /// public async Task GetTodoItemAsync(int id) { - var response = await this.HttpClient.GetAsync($"TodoItems/item/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"TodoItems/item/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -57,7 +61,11 @@ public async Task GetTodoItemAsync(int id) /// public async Task PutTodoItemAsync(TodoItemResponse todoItem) { - var response = await this.HttpClient.PutAsJsonAsync("TodoItems/item", todoItem); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("TodoItems/item", todoItem)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -77,7 +85,11 @@ public async Task PutTodoItemAsync(TodoItemResponse todoItem) /// public async Task DeleteTodoItemAsync(int id) { - var response = await this.HttpClient.DeleteAsync($"TodoItems/item/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"TodoItems/item/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -97,7 +109,11 @@ public async Task DeleteTodoItemAsync(int id) /// public async Task PostTodoItemAsync(TodoItemResponse todoItem) { - var response = await this.HttpClient.PostAsJsonAsync("TodoItems/item", todoItem); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync("TodoItems/item", todoItem)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); diff --git a/BlazorShop.WebClient/Services/TodoListService.cs b/BlazorShop.WebClient/Services/TodoListService.cs index c3dc06d6..89452d16 100644 --- a/BlazorShop.WebClient/Services/TodoListService.cs +++ b/BlazorShop.WebClient/Services/TodoListService.cs @@ -39,7 +39,11 @@ public TodoListService(HttpClient httpClient, ISnackbar snackBar) /// public async Task> GetTodoListsAsync() { - var response = await this.HttpClient.GetAsync("TodoLists/lists"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync("TodoLists/lists")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -58,7 +62,11 @@ public async Task> GetTodoListsAsync() /// public async Task GetTodoListAsync(int id) { - var response = await this.HttpClient.GetAsync($"TodoLists/list/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"TodoLists/list/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -76,7 +84,11 @@ public async Task GetTodoListAsync(int id) /// public async Task PostTodoListAsync(TodoListResponse todoList) { - var response = await this.HttpClient.PostAsJsonAsync($"TodoLists/list", todoList); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync($"TodoLists/list", todoList)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -98,7 +110,11 @@ public async Task PostTodoListAsync(TodoListResponse todoList) /// public async Task PutTodoListAsync(TodoListResponse todoList) { - var response = await this.HttpClient.PutAsJsonAsync("TodoLists/list", todoList); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync("TodoLists/list", todoList)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -118,7 +134,11 @@ public async Task PutTodoListAsync(TodoListResponse todoList) /// public async Task DeleteTodoListAsync(int id) { - var response = await this.HttpClient.DeleteAsync($"TodoLists/list/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"TodoLists/list/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); diff --git a/BlazorShop.WebClient/Services/UserService.cs b/BlazorShop.WebClient/Services/UserService.cs index 74cbf1f9..af589d18 100644 --- a/BlazorShop.WebClient/Services/UserService.cs +++ b/BlazorShop.WebClient/Services/UserService.cs @@ -47,7 +47,11 @@ public async Task AddUser(UserResponse user) Role = user.RoleName, }; - var response = await this.HttpClient.PostAsJsonAsync($"Users/user", data); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync($"Users/user", data)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -72,7 +76,11 @@ public async Task ActivateUser(int userId) Id = userId, }; - var response = await this.HttpClient.PostAsJsonAsync($"Users/userActivate", data); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PostAsJsonAsync($"Users/userActivate", data)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -92,7 +100,11 @@ public async Task ActivateUser(int userId) /// public async Task DeleteUser(int id) { - var response = await this.HttpClient.DeleteAsync($"Users/user/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.DeleteAsync($"Users/user/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -112,7 +124,11 @@ public async Task DeleteUser(int id) /// public async Task GetUser(int id) { - var response = await this.HttpClient.GetAsync($"Users/user/{id}"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync($"Users/user/{id}")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -130,7 +146,11 @@ public async Task GetUser(int id) /// public async Task> GetUsers() { - var response = await this.HttpClient.GetAsync("Users/users"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync("Users/users")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -148,7 +168,11 @@ public async Task> GetUsers() /// public async Task> GetUsersInactive() { - var response = await this.HttpClient.GetAsync("Users/usersInactive"); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.GetAsync("Users/usersInactive")); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize>( responseResult, this.Options); @@ -174,7 +198,11 @@ public async Task UpdateUser(UserResponse user) Role = user.RoleName, }; - var response = await this.HttpClient.PutAsJsonAsync($"Users/user", data); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync($"Users/user", data)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); @@ -194,7 +222,11 @@ public async Task UpdateUser(UserResponse user) /// public async Task UpdateUserEmail(UpdateUserEmailCommand user) { - var response = await this.HttpClient.PutAsJsonAsync($"Users/userEmail", user); + var response = await Policy + .Handle() + .WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1)) + .ExecuteAsync(async () => await this.HttpClient.PutAsJsonAsync($"Users/userEmail", user)); + var responseResult = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize( responseResult, this.Options); diff --git a/BlazorShop.WebClient/_Imports.cs b/BlazorShop.WebClient/_Imports.cs index b7adfbb6..126b232a 100644 --- a/BlazorShop.WebClient/_Imports.cs +++ b/BlazorShop.WebClient/_Imports.cs @@ -34,6 +34,7 @@ global using Microsoft.JSInterop; global using MudBlazor; global using MudBlazor.Services; +global using Polly; global using Serilog; global using Toolbelt.Blazor; global using Toolbelt.Blazor.Extensions.DependencyInjection;