From c8a4ffaffbf9885849cb37931019057c0aaeacfe Mon Sep 17 00:00:00 2001 From: Michele George Date: Mon, 2 Aug 2021 09:30:16 +0200 Subject: [PATCH] Adding Polly Example --- ...etools.Sdk.ClientCredentialsExample.csproj | 1 + .../Controllers/Categories.cs | 36 +++++++++ .../commercetools.Sdk.PollyExample/Program.cs | 23 ++++++ .../Properties/launchSettings.json | 28 +++++++ .../commercetools.Sdk.PollyExample/Startup.cs | 75 +++++++++++++++++++ .../Views/Categories/Index.cshtml | 27 +++++++ .../appsettings.json | 26 +++++++ .../commercetools.Sdk.PollyExample.csproj | 24 ++++++ commercetools.Sdk/commercetools.Sdk.sln | 7 ++ 9 files changed, 247 insertions(+) create mode 100644 commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Controllers/Categories.cs create mode 100644 commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Program.cs create mode 100644 commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Properties/launchSettings.json create mode 100644 commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Startup.cs create mode 100644 commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Views/Categories/Index.cshtml create mode 100644 commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/appsettings.json create mode 100644 commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/commercetools.Sdk.PollyExample.csproj diff --git a/commercetools.Sdk/Examples/commercetools.Sdk.HttpApi.MvcExample/commercetools.Sdk.ClientCredentialsExample.csproj b/commercetools.Sdk/Examples/commercetools.Sdk.HttpApi.MvcExample/commercetools.Sdk.ClientCredentialsExample.csproj index a60db394..1b9e9873 100644 --- a/commercetools.Sdk/Examples/commercetools.Sdk.HttpApi.MvcExample/commercetools.Sdk.ClientCredentialsExample.csproj +++ b/commercetools.Sdk/Examples/commercetools.Sdk.HttpApi.MvcExample/commercetools.Sdk.ClientCredentialsExample.csproj @@ -4,6 +4,7 @@ netcoreapp2.1 + diff --git a/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Controllers/Categories.cs b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Controllers/Categories.cs new file mode 100644 index 00000000..14677610 --- /dev/null +++ b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Controllers/Categories.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using commercetools.Sdk.Client; +using commercetools.Sdk.Domain.Categories; +using commercetools.Sdk.HttpApi.CommandBuilders; +using Microsoft.AspNetCore.Mvc; + +namespace commercetools.Sdk.PollyExample.Controllers +{ + public class Categories : Controller + { + private readonly IClient _AdminClient; + private readonly IClient _AnonymousClient; + + public Categories(IEnumerable clients) + { + this._AdminClient = clients.FirstOrDefault(c => c.Name.Equals("AdminClient")); + this._AnonymousClient = clients.FirstOrDefault(c => c.Name.Equals("AnonymousClient")); + } + // GET + public async Task Index() + { + var categories = await _AnonymousClient.ExecuteAsync(new QueryCommand()); + + //if you want to Debug NotFound Retry Policy enable this + /* + var category = await _AdminClient.Builder() + .Categories() + .GetById("NotFoundId") + .ExecuteAsync(); + */ + return View(categories); + } + } +} \ No newline at end of file diff --git a/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Program.cs b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Program.cs new file mode 100644 index 00000000..40c289b5 --- /dev/null +++ b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Program.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace commercetools.Sdk.PollyExample +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); + } +} \ No newline at end of file diff --git a/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Properties/launchSettings.json b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Properties/launchSettings.json new file mode 100644 index 00000000..5ff3f5d6 --- /dev/null +++ b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Properties/launchSettings.json @@ -0,0 +1,28 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:54146", + "sslPort": 44346 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "commercetools.Sdk.PollyExample": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": true, + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Startup.cs b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Startup.cs new file mode 100644 index 00000000..ad90be9e --- /dev/null +++ b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Startup.cs @@ -0,0 +1,75 @@ +using System.Collections.Generic; +using commercetools.Sdk.HttpApi.Tokens; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Polly; +using Polly.Extensions.Http; + +namespace commercetools.Sdk.PollyExample +{ + public class Startup + { + private readonly IConfiguration configuration; + + public Startup(IConfiguration configuration) + { + this.configuration = configuration; + } + + // This method gets called by the runtime. Use this method to add services to the container. + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 + public void ConfigureServices(IServiceCollection services) + { + services.AddMvcCore(); + services.AddControllersWithViews(); + + //Creating the policy + var registry = services.AddPolicyRegistry(); + var policy = HttpPolicyExtensions + .HandleTransientHttpError() + //.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound) + .OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.Forbidden) + .RetryAsync(3, onRetry: (exception, retryCount, context) => + { + var exceptionText = exception?.Exception == null + ? string.Empty + : $" exception:{exception.Exception.Message}"; + // logging here + }); + registry.Add("commercetoolsRetryPolicy", policy); + + var clients = new Dictionary() + { + {"AdminClient", TokenFlow.ClientCredentials}, + {"AnonymousClient", TokenFlow.AnonymousSession} + }; + // services.UseCommercetools(configuration, "AdminClient") + // .AddPolicyHandlerFromRegistry("commercetoolsRetryPolicy"); + services.UseCommercetools(configuration, clients) + .ConfigureAllClients( + builder => builder.AddPolicyHandlerFromRegistry("commercetoolsRetryPolicy")); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute( + name: "default", + pattern: "{controller=Categories}/{action=Index}/{id?}"); + }); + } + } +} \ No newline at end of file diff --git a/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Views/Categories/Index.cshtml b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Views/Categories/Index.cshtml new file mode 100644 index 00000000..e2964076 --- /dev/null +++ b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/Views/Categories/Index.cshtml @@ -0,0 +1,27 @@ +@using commercetools.Sdk.Domain +@using commercetools.Sdk.Domain.Categories +@model PagedQueryResult +@{ + Layout = null; +} + + + + + + Categories + + +

There are @Model.Count categories

+
+
    + @foreach (var category in Model.Results) + { +
  • + @category.Name["en"] +
  • + } +
+
+ + \ No newline at end of file diff --git a/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/appsettings.json b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/appsettings.json new file mode 100644 index 00000000..44b2fde2 --- /dev/null +++ b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/appsettings.json @@ -0,0 +1,26 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AdminClient": { + "ClientId": "", + "ClientSecret": "", + "ProjectKey": "", + "Scope": "", + "ApiBaseAddress": "https://api.europe-west1.gcp.commercetools.com/", + "AuthorizationBaseAddress": "https://auth.europe-west1.gcp.commercetools.com/" + }, + "AnonymousClient": { + "ClientId": "", + "ClientSecret": "", + "ProjectKey": "", + "Scope": "", + "ApiBaseAddress": "https://api.europe-west1.gcp.commercetools.com/", + "AuthorizationBaseAddress": "https://auth.europe-west1.gcp.commercetools.com/" + }, + "AllowedHosts": "*" +} diff --git a/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/commercetools.Sdk.PollyExample.csproj b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/commercetools.Sdk.PollyExample.csproj new file mode 100644 index 00000000..dc54884d --- /dev/null +++ b/commercetools.Sdk/Examples/commercetools.Sdk.PollyExample/commercetools.Sdk.PollyExample.csproj @@ -0,0 +1,24 @@ + + + + netcoreapp3.1 + + + + + + + + + + + + + + + + + + + + diff --git a/commercetools.Sdk/commercetools.Sdk.sln b/commercetools.Sdk/commercetools.Sdk.sln index e9d5e57d..808a15b3 100644 --- a/commercetools.Sdk/commercetools.Sdk.sln +++ b/commercetools.Sdk/commercetools.Sdk.sln @@ -62,6 +62,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "commercetools.Sdk.ManualTes EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ManualTests", "ManualTests", "{73E27F17-DEB9-488B-8137-A57D4129E0E6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "commercetools.Sdk.PollyExample", "Examples\commercetools.Sdk.PollyExample\commercetools.Sdk.PollyExample.csproj", "{0FB0CEBF-A0E5-483C-92A2-916DB63ED77C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -155,6 +157,10 @@ Global {A618E44D-3C9C-4343-A7AE-595A6FADD51A}.Debug|Any CPU.Build.0 = Debug|Any CPU {A618E44D-3C9C-4343-A7AE-595A6FADD51A}.Release|Any CPU.ActiveCfg = Release|Any CPU {A618E44D-3C9C-4343-A7AE-595A6FADD51A}.Release|Any CPU.Build.0 = Release|Any CPU + {0FB0CEBF-A0E5-483C-92A2-916DB63ED77C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0FB0CEBF-A0E5-483C-92A2-916DB63ED77C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0FB0CEBF-A0E5-483C-92A2-916DB63ED77C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0FB0CEBF-A0E5-483C-92A2-916DB63ED77C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -171,6 +177,7 @@ Global {1ED86710-A7A6-4E63-AAC6-A98A1EEB46D2} = {246B3DBC-CE94-4B32-AEFB-8C42A0D8F502} {2ADE2630-89A4-4F15-A855-7FA421D589FE} = {199CAB4C-C38A-47B7-AB72-CCFCCA06C31D} {A618E44D-3C9C-4343-A7AE-595A6FADD51A} = {73E27F17-DEB9-488B-8137-A57D4129E0E6} + {0FB0CEBF-A0E5-483C-92A2-916DB63ED77C} = {246B3DBC-CE94-4B32-AEFB-8C42A0D8F502} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {38B2497B-A494-43A7-A44D-88131CE16D12}