From a52593f3657856ff446364bdf5df0ac64959f9ba Mon Sep 17 00:00:00 2001 From: Gleb Raytsin Date: Mon, 18 Jul 2022 22:43:50 -0400 Subject: [PATCH 01/11] First take on contentful service implemenation, still a work in progress. --- .../ContentfulService.cs | 34 +++++++++++++++++++ .../DependencyInjectionExtensions.cs | 32 +++++++++++++++++ .../Proxy/ContentfulProxy.cs | 28 +++++++++++++++ .../Proxy/IContentfulProxy.cs | 13 +++++++ ...etInsights.DXP.Providers.Contentful.csproj | 19 +++++++++++ src/RocketInsights.sln | 13 +++++-- 6 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs create mode 100644 src/RocketInsights.DXP.Providers.Contentful/Extensions/DependencyInjectionExtensions.cs create mode 100644 src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs create mode 100644 src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs create mode 100644 src/RocketInsights.DXP.Providers.Contentful/RocketInsights.DXP.Providers.Contentful.csproj diff --git a/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs b/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs new file mode 100644 index 0000000..c108cd8 --- /dev/null +++ b/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs @@ -0,0 +1,34 @@ +using Contentful.Core.Models; +using RocketInsights.Contextual.Services; +using RocketInsights.DXP.Models; +using RocketInsights.DXP.Providers.Contentful.Proxy; +using RocketInsights.DXP.Providers.Contentful.Types; +using RocketInsights.DXP.Services; +using System; +using System.Threading.Tasks; + +namespace RocketInsights.DXP.Providers.Contentful +{ + public class ContentfulService : IExperienceService + { + private IContextService ContextService { get; } + private IContentfulProxy ContentfulProxy { get; } + + public ContentfulService(IContextService contextService, IContentfulProxy contentfulProxy) + { + ContextService = contextService; + ContentfulProxy = contentfulProxy; + } + + public Task GetCompositionAsync() + { + throw new NotImplementedException(); + } + + public Task GetFragmentAsync(string id) + { + Entry fragmentEntry = ContentfulProxy.GetEntryAsync(id).Result; + throw new NotImplementedException(); + } + } +} diff --git a/src/RocketInsights.DXP.Providers.Contentful/Extensions/DependencyInjectionExtensions.cs b/src/RocketInsights.DXP.Providers.Contentful/Extensions/DependencyInjectionExtensions.cs new file mode 100644 index 0000000..bc89dff --- /dev/null +++ b/src/RocketInsights.DXP.Providers.Contentful/Extensions/DependencyInjectionExtensions.cs @@ -0,0 +1,32 @@ +using Contentful.Core; +using Contentful.Core.Configuration; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using RocketInsights.DXP.Providers.Contentful.Proxy; +using RocketInsights.DXP.Services; +using System.Net.Http; + +namespace RocketInsights.DXP.Providers.Contentful.Extensions +{ + public static class DependencyInjectionExtensions + { + public static IServiceCollection AddContentful (this IServiceCollection services, IConfiguration configuration) + { + services.AddSingleton(s => InitContentfulClient(configuration)); + + services.AddSingleton(); + services.AddSingleton(); + + return services; + } + + private static ContentfulClient InitContentfulClient (IConfiguration configuration) + { + var httpClient = new HttpClient(); + ContentfulOptions contentfulOptions = configuration.GetSection(nameof(ContentfulOptions)).Get(); + + ContentfulClient contentfulClient = new ContentfulClient(httpClient, contentfulOptions); + return contentfulClient; + } + } +} diff --git a/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs b/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs new file mode 100644 index 0000000..b02bc81 --- /dev/null +++ b/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs @@ -0,0 +1,28 @@ +using Contentful.Core; +using Contentful.Core.Models; +using Contentful.Core.Search; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace RocketInsights.DXP.Providers.Contentful.Proxy +{ + public class ContentfulProxy : IContentfulProxy + { + private IContentfulClient ContentfulClient { get; } + + public ContentfulProxy(IContentfulClient contentfulClient) + { + ContentfulClient = contentfulClient; + } + + //TODO: Handle API errors, including rate limiting + + public async Task> GetEntryAsync(string entryId) + { + var entry = await ContentfulClient.GetEntry>(entryId); + return entry; + } + } +} diff --git a/src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs b/src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs new file mode 100644 index 0000000..77d3485 --- /dev/null +++ b/src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs @@ -0,0 +1,13 @@ +using Contentful.Core.Models; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace RocketInsights.DXP.Providers.Contentful.Proxy +{ + public interface IContentfulProxy + { + Task> GetEntryAsync(string entryId); + } +} diff --git a/src/RocketInsights.DXP.Providers.Contentful/RocketInsights.DXP.Providers.Contentful.csproj b/src/RocketInsights.DXP.Providers.Contentful/RocketInsights.DXP.Providers.Contentful.csproj new file mode 100644 index 0000000..46c571e --- /dev/null +++ b/src/RocketInsights.DXP.Providers.Contentful/RocketInsights.DXP.Providers.Contentful.csproj @@ -0,0 +1,19 @@ + + + + netstandard2.0 + 0085a9fb-bf75-4b62-a03f-9d9680f176c9 + + + + + + + + + + + + + + diff --git a/src/RocketInsights.sln b/src/RocketInsights.sln index 5ac845d..cfcd3dc 100644 --- a/src/RocketInsights.sln +++ b/src/RocketInsights.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.0.31912.275 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32602.291 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RocketInsights.Common", "RocketInsights.Common\RocketInsights.Common.csproj", "{2AC2450A-1B09-4244-BE01-42AD6A4410F4}" EndProject @@ -23,7 +23,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{04D598 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RocketInsights.DXP.AspNetCore", "RocketInsights.DXP.AspNetCore\RocketInsights.DXP.AspNetCore.csproj", "{19CC8707-2540-4CFB-9265-D9D774300906}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RocketInsights.DXP.Providers.Kontent", "RocketInsights.DXP.Providers.Kontent\RocketInsights.DXP.Providers.Kontent.csproj", "{CB498A41-AB48-40E1-9E59-D25F4FA35305}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RocketInsights.DXP.Providers.Kontent", "RocketInsights.DXP.Providers.Kontent\RocketInsights.DXP.Providers.Kontent.csproj", "{CB498A41-AB48-40E1-9E59-D25F4FA35305}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RocketInsights.DXP.Providers.Contentful", "RocketInsights.DXP.Providers.Contentful\RocketInsights.DXP.Providers.Contentful.csproj", "{FF78627C-13D2-4093-B06D-10EC46114339}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -63,6 +65,10 @@ Global {CB498A41-AB48-40E1-9E59-D25F4FA35305}.Debug|Any CPU.Build.0 = Debug|Any CPU {CB498A41-AB48-40E1-9E59-D25F4FA35305}.Release|Any CPU.ActiveCfg = Release|Any CPU {CB498A41-AB48-40E1-9E59-D25F4FA35305}.Release|Any CPU.Build.0 = Release|Any CPU + {FF78627C-13D2-4093-B06D-10EC46114339}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF78627C-13D2-4093-B06D-10EC46114339}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF78627C-13D2-4093-B06D-10EC46114339}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF78627C-13D2-4093-B06D-10EC46114339}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -76,6 +82,7 @@ Global {A96BA359-7830-4148-916A-10A889ECE470} = {EEBF50A2-9A78-4A59-8D58-FD6EA92FC08E} {19CC8707-2540-4CFB-9265-D9D774300906} = {EEBF50A2-9A78-4A59-8D58-FD6EA92FC08E} {CB498A41-AB48-40E1-9E59-D25F4FA35305} = {EEBF50A2-9A78-4A59-8D58-FD6EA92FC08E} + {FF78627C-13D2-4093-B06D-10EC46114339} = {EEBF50A2-9A78-4A59-8D58-FD6EA92FC08E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0904E15D-0535-4207-B6B6-83A605BB2590} From ebea027e5ac035d559b056175322adc9304b7f97 Mon Sep 17 00:00:00 2001 From: Gleb Raytsin Date: Tue, 19 Jul 2022 20:17:22 -0400 Subject: [PATCH 02/11] Adding locale and some minor tweaks --- .../ContentfulService.cs | 27 ++++++++++++++++--- .../Proxy/ContentfulProxy.cs | 6 +++-- .../Proxy/IContentfulProxy.cs | 2 +- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs b/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs index c108cd8..e40c766 100644 --- a/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs +++ b/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs @@ -1,8 +1,7 @@ -using Contentful.Core.Models; +using RocketInsights.Common.Models; using RocketInsights.Contextual.Services; using RocketInsights.DXP.Models; using RocketInsights.DXP.Providers.Contentful.Proxy; -using RocketInsights.DXP.Providers.Contentful.Types; using RocketInsights.DXP.Services; using System; using System.Threading.Tasks; @@ -27,8 +26,28 @@ public Task GetCompositionAsync() public Task GetFragmentAsync(string id) { - Entry fragmentEntry = ContentfulProxy.GetEntryAsync(id).Result; - throw new NotImplementedException(); + if (ContextService.TryGetContext(out var context)) + { + var fragmentEntry = ContentfulProxy.GetEntryAsync(id, context.Culture?.Name ?? "en-US").Result; + if (fragmentEntry != null) + { + var fragment = new Fragment() + { + Id = fragmentEntry.SystemProperties.Id, + ContentType = fragmentEntry.SystemProperties.ContentType.Name, + Content = fragmentEntry.Fields + }; + return Task.FromResult(fragment); + } + else + { + throw new Exception($"Unable to fetch Fragment: {id}"); + } + } + else + { + throw new Exception("Unable to fetch context"); + } } } } diff --git a/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs b/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs index b02bc81..8153c06 100644 --- a/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs +++ b/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs @@ -19,9 +19,11 @@ public ContentfulProxy(IContentfulClient contentfulClient) //TODO: Handle API errors, including rate limiting - public async Task> GetEntryAsync(string entryId) + public async Task> GetEntryAsync(string entryId, string locale = "*") { - var entry = await ContentfulClient.GetEntry>(entryId); + var query = QueryBuilder>.New; + query.LocaleIs(locale); + var entry = await ContentfulClient.GetEntry>(entryId, query); return entry; } } diff --git a/src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs b/src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs index 77d3485..51a6acb 100644 --- a/src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs +++ b/src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs @@ -8,6 +8,6 @@ namespace RocketInsights.DXP.Providers.Contentful.Proxy { public interface IContentfulProxy { - Task> GetEntryAsync(string entryId); + Task> GetEntryAsync(string entryId, string locale = "*"); } } From 83345a4cd7cad7cbfddb34e28e68f63a33332404 Mon Sep 17 00:00:00 2001 From: Gleb Raytsin Date: Tue, 19 Jul 2022 20:17:55 -0400 Subject: [PATCH 03/11] Adding tests project and a simple test, more work to be done there. --- .../IntegrationTests.cs | 56 +++++++++++++++++++ ...ghts.DXP.Providers.Contentful.Tests.csproj | 31 ++++++++++ src/RocketInsights.sln | 15 ++++- 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs create mode 100644 src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj diff --git a/src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs b/src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs new file mode 100644 index 0000000..0d50301 --- /dev/null +++ b/src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs @@ -0,0 +1,56 @@ + +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using RocketInsights.Contextual; +using RocketInsights.Contextual.Models; +using RocketInsights.DXP.Providers.Contentful.Extensions; +using RocketInsights.DXP.Services; +using System.Globalization; +using System.Reflection; +using System.Security.Claims; +using System.Threading.Tasks; + +namespace RocketInsights.DXP.Providers.Contentful.Tests +{ + [TestClass] + public class IntegrationTests + { + private static IServiceCollection ServiceCollection { get; set; } + + public IntegrationTests() + { + IConfigurationRoot configurationRoot = new ConfigurationBuilder() + .AddUserSecrets(Assembly.GetExecutingAssembly()) + .Build(); + + ServiceCollection = new ServiceCollection() + .AddLogging(logging => logging.SetMinimumLevel(LogLevel.Trace)) + .AddHttpContextAccessor() + .AddContextual() + .AddSingleton() + .AddDXP() + .AddContentful(configurationRoot); + + } + + [TestMethod] + public async Task TestRetrievingAContentFragmentFromContentful() + { + var provider = ServiceCollection.BuildServiceProvider(); + + var contextStore = provider.GetRequiredService(); + contextStore.Set("context", new Context() + { + Culture = new CultureInfo("en-US"), + Identity = new ClaimsIdentity() + }); + + var experienceService = provider.GetRequiredService(); + + var fragment = await experienceService.GetFragmentAsync("3EK9sDbFFm69x1azZdd6MJ"); + Assert.AreEqual("3EK9sDbFFm69x1azZdd6MJ", fragment.Id); + } + } +} diff --git a/src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj b/src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj new file mode 100644 index 0000000..34da101 --- /dev/null +++ b/src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj @@ -0,0 +1,31 @@ + + + + net5.0 + + false + + 130d7bc1-de18-4c07-9e18-fc49e3ce9cb5 + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/RocketInsights.sln b/src/RocketInsights.sln index c57f198..cb6ea99 100644 --- a/src/RocketInsights.sln +++ b/src/RocketInsights.sln @@ -25,9 +25,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RocketInsights.DXP.AspNetCo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RocketInsights.DXP.Providers.Kontent", "RocketInsights.DXP.Providers.Kontent\RocketInsights.DXP.Providers.Kontent.csproj", "{CB498A41-AB48-40E1-9E59-D25F4FA35305}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RocketInsights.DXP.Providers.Kontent.Tests", "RocketInsights.DXP.Providers.Kontent.Tests\RocketInsights.DXP.Providers.Kontent.Tests.csproj", "{3FB84C80-6D85-4307-BC03-B83304F86400}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RocketInsights.DXP.Providers.Kontent.Tests", "RocketInsights.DXP.Providers.Kontent.Tests\RocketInsights.DXP.Providers.Kontent.Tests.csproj", "{3FB84C80-6D85-4307-BC03-B83304F86400}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RocketInsights.DXP.Providers.Contentful", "RocketInsights.DXP.Providers.Contentful\RocketInsights.DXP.Providers.Contentful.csproj", "{FF78627C-13D2-4093-B06D-10EC46114339}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RocketInsights.DXP.Providers.Contentful", "RocketInsights.DXP.Providers.Contentful\RocketInsights.DXP.Providers.Contentful.csproj", "{FF78627C-13D2-4093-B06D-10EC46114339}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RocketInsights.DXP.Providers.Contentful.Tests", "RocketInsights.DXP.Providers.Contentful.Tests\RocketInsights.DXP.Providers.Contentful.Tests.csproj", "{C348A0CB-8618-4686-A281-F59F874CE04C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -67,10 +69,18 @@ Global {CB498A41-AB48-40E1-9E59-D25F4FA35305}.Debug|Any CPU.Build.0 = Debug|Any CPU {CB498A41-AB48-40E1-9E59-D25F4FA35305}.Release|Any CPU.ActiveCfg = Release|Any CPU {CB498A41-AB48-40E1-9E59-D25F4FA35305}.Release|Any CPU.Build.0 = Release|Any CPU + {3FB84C80-6D85-4307-BC03-B83304F86400}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3FB84C80-6D85-4307-BC03-B83304F86400}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3FB84C80-6D85-4307-BC03-B83304F86400}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3FB84C80-6D85-4307-BC03-B83304F86400}.Release|Any CPU.Build.0 = Release|Any CPU {FF78627C-13D2-4093-B06D-10EC46114339}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {FF78627C-13D2-4093-B06D-10EC46114339}.Debug|Any CPU.Build.0 = Debug|Any CPU {FF78627C-13D2-4093-B06D-10EC46114339}.Release|Any CPU.ActiveCfg = Release|Any CPU {FF78627C-13D2-4093-B06D-10EC46114339}.Release|Any CPU.Build.0 = Release|Any CPU + {C348A0CB-8618-4686-A281-F59F874CE04C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C348A0CB-8618-4686-A281-F59F874CE04C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C348A0CB-8618-4686-A281-F59F874CE04C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C348A0CB-8618-4686-A281-F59F874CE04C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -85,6 +95,7 @@ Global {19CC8707-2540-4CFB-9265-D9D774300906} = {EEBF50A2-9A78-4A59-8D58-FD6EA92FC08E} {CB498A41-AB48-40E1-9E59-D25F4FA35305} = {EEBF50A2-9A78-4A59-8D58-FD6EA92FC08E} {FF78627C-13D2-4093-B06D-10EC46114339} = {EEBF50A2-9A78-4A59-8D58-FD6EA92FC08E} + {C348A0CB-8618-4686-A281-F59F874CE04C} = {EEBF50A2-9A78-4A59-8D58-FD6EA92FC08E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0904E15D-0535-4207-B6B6-83A605BB2590} From e2e0bca774d2d38d0b6f7761fc6f29d42663f696 Mon Sep 17 00:00:00 2001 From: Gleb Raytsin Date: Tue, 19 Jul 2022 20:40:19 -0400 Subject: [PATCH 04/11] Updating package versions to latest to hopefully resolve CI/CD issues. --- ...etInsights.DXP.Providers.Contentful.Tests.csproj | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj b/src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj index 34da101..32f12e0 100644 --- a/src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj +++ b/src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj @@ -10,15 +10,18 @@ - + - - - - + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + From 0afbdf38608e3949bbda769a8a0725b57d4a1b17 Mon Sep 17 00:00:00 2001 From: Gleb Raytsin Date: Wed, 20 Jul 2022 11:53:34 -0400 Subject: [PATCH 05/11] Renaming the proxy method to be more descriptive and so that we can add methods with same signature that do things slightly differently. --- .../ContentfulService.cs | 2 +- .../Proxy/ContentfulProxy.cs | 2 +- .../Proxy/IContentfulProxy.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs b/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs index e40c766..a6783c4 100644 --- a/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs +++ b/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs @@ -28,7 +28,7 @@ public Task GetFragmentAsync(string id) { if (ContextService.TryGetContext(out var context)) { - var fragmentEntry = ContentfulProxy.GetEntryAsync(id, context.Culture?.Name ?? "en-US").Result; + var fragmentEntry = ContentfulProxy.GetEntryByIdAsync(id, context.Culture?.Name ?? "en-US").Result; if (fragmentEntry != null) { var fragment = new Fragment() diff --git a/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs b/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs index 8153c06..9d66193 100644 --- a/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs +++ b/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs @@ -19,7 +19,7 @@ public ContentfulProxy(IContentfulClient contentfulClient) //TODO: Handle API errors, including rate limiting - public async Task> GetEntryAsync(string entryId, string locale = "*") + public async Task> GetEntryByIdAsync(string entryId, string locale = "*") { var query = QueryBuilder>.New; query.LocaleIs(locale); diff --git a/src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs b/src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs index 51a6acb..f90528d 100644 --- a/src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs +++ b/src/RocketInsights.DXP.Providers.Contentful/Proxy/IContentfulProxy.cs @@ -8,6 +8,6 @@ namespace RocketInsights.DXP.Providers.Contentful.Proxy { public interface IContentfulProxy { - Task> GetEntryAsync(string entryId, string locale = "*"); + Task> GetEntryByIdAsync(string entryId, string locale = "*"); } } From fe9721a6d61f9f5c3838748d09e70e0bc80c9d55 Mon Sep 17 00:00:00 2001 From: Gleb Raytsin Date: Wed, 20 Jul 2022 12:02:13 -0400 Subject: [PATCH 06/11] Slight tweak to locale handling to make the locale parameter optional and just not include it in the filter --- .../ContentfulService.cs | 2 +- .../Proxy/ContentfulProxy.cs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs b/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs index a6783c4..a59c8c9 100644 --- a/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs +++ b/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs @@ -28,7 +28,7 @@ public Task GetFragmentAsync(string id) { if (ContextService.TryGetContext(out var context)) { - var fragmentEntry = ContentfulProxy.GetEntryByIdAsync(id, context.Culture?.Name ?? "en-US").Result; + var fragmentEntry = ContentfulProxy.GetEntryByIdAsync(id, context.Culture?.Name).Result; if (fragmentEntry != null) { var fragment = new Fragment() diff --git a/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs b/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs index 9d66193..defca7b 100644 --- a/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs +++ b/src/RocketInsights.DXP.Providers.Contentful/Proxy/ContentfulProxy.cs @@ -19,10 +19,13 @@ public ContentfulProxy(IContentfulClient contentfulClient) //TODO: Handle API errors, including rate limiting - public async Task> GetEntryByIdAsync(string entryId, string locale = "*") + public async Task> GetEntryByIdAsync(string entryId, string locale = null) { var query = QueryBuilder>.New; - query.LocaleIs(locale); + if (!string.IsNullOrEmpty(locale)) + { + query.LocaleIs(locale); + } var entry = await ContentfulClient.GetEntry>(entryId, query); return entry; } From 97f3a4d70f4ab08a82a03de5ff5e0dd70995794d Mon Sep 17 00:00:00 2001 From: Gleb Raytsin Date: Wed, 20 Jul 2022 15:56:16 -0400 Subject: [PATCH 07/11] Switching back to the way it was to make things a bit more future proof. --- .../IntegrationTests.cs | 2 +- .../ContentfulService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs b/src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs index 0d50301..9c414a1 100644 --- a/src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs +++ b/src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs @@ -47,7 +47,7 @@ public async Task TestRetrievingAContentFragmentFromContentful() Identity = new ClaimsIdentity() }); - var experienceService = provider.GetRequiredService(); + var experienceService = provider.GetRequiredService(); var fragment = await experienceService.GetFragmentAsync("3EK9sDbFFm69x1azZdd6MJ"); Assert.AreEqual("3EK9sDbFFm69x1azZdd6MJ", fragment.Id); diff --git a/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs b/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs index a59c8c9..48bbd55 100644 --- a/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs +++ b/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs @@ -8,7 +8,7 @@ namespace RocketInsights.DXP.Providers.Contentful { - public class ContentfulService : IExperienceService + public class ContentfulService : ILayoutService, IContentService { private IContextService ContextService { get; } private IContentfulProxy ContentfulProxy { get; } From 41dfde85d35b5e96076e3990f5606c998e1486d2 Mon Sep 17 00:00:00 2001 From: Gleb Raytsin Date: Wed, 17 Aug 2022 12:17:30 -0400 Subject: [PATCH 08/11] Updating packages to versions matching .NET 5 framework. --- ...Insights.DXP.Providers.Contentful.Tests.csproj | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj b/src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj index 32f12e0..dceaa4c 100644 --- a/src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj +++ b/src/RocketInsights.DXP.Providers.Contentful.Tests/RocketInsights.DXP.Providers.Contentful.Tests.csproj @@ -9,13 +9,14 @@ - - - - - - - + + + + + + + + From 1cf75ebf0da9ab51be8571498950690a37a2115e Mon Sep 17 00:00:00 2001 From: Gleb Raytsin Date: Wed, 17 Aug 2022 12:44:06 -0400 Subject: [PATCH 09/11] Renaming service to better match up with the naming convention --- .../{ContentfulService.cs => ContentService.cs} | 9 ++------- .../Extensions/DependencyInjectionExtensions.cs | 2 +- .../LayoutService.cs | 10 ++++++++++ 3 files changed, 13 insertions(+), 8 deletions(-) rename src/RocketInsights.DXP.Providers.Contentful/{ContentfulService.cs => ContentService.cs} (83%) create mode 100644 src/RocketInsights.DXP.Providers.Contentful/LayoutService.cs diff --git a/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs b/src/RocketInsights.DXP.Providers.Contentful/ContentService.cs similarity index 83% rename from src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs rename to src/RocketInsights.DXP.Providers.Contentful/ContentService.cs index 48bbd55..1314db4 100644 --- a/src/RocketInsights.DXP.Providers.Contentful/ContentfulService.cs +++ b/src/RocketInsights.DXP.Providers.Contentful/ContentService.cs @@ -8,22 +8,17 @@ namespace RocketInsights.DXP.Providers.Contentful { - public class ContentfulService : ILayoutService, IContentService + public class ContentService : IContentService { private IContextService ContextService { get; } private IContentfulProxy ContentfulProxy { get; } - public ContentfulService(IContextService contextService, IContentfulProxy contentfulProxy) + public ContentService(IContextService contextService, IContentfulProxy contentfulProxy) { ContextService = contextService; ContentfulProxy = contentfulProxy; } - public Task GetCompositionAsync() - { - throw new NotImplementedException(); - } - public Task GetFragmentAsync(string id) { if (ContextService.TryGetContext(out var context)) diff --git a/src/RocketInsights.DXP.Providers.Contentful/Extensions/DependencyInjectionExtensions.cs b/src/RocketInsights.DXP.Providers.Contentful/Extensions/DependencyInjectionExtensions.cs index bc89dff..748bc92 100644 --- a/src/RocketInsights.DXP.Providers.Contentful/Extensions/DependencyInjectionExtensions.cs +++ b/src/RocketInsights.DXP.Providers.Contentful/Extensions/DependencyInjectionExtensions.cs @@ -14,7 +14,7 @@ public static IServiceCollection AddContentful (this IServiceCollection services { services.AddSingleton(s => InitContentfulClient(configuration)); - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); return services; diff --git a/src/RocketInsights.DXP.Providers.Contentful/LayoutService.cs b/src/RocketInsights.DXP.Providers.Contentful/LayoutService.cs new file mode 100644 index 0000000..43e0178 --- /dev/null +++ b/src/RocketInsights.DXP.Providers.Contentful/LayoutService.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RocketInsights.DXP.Providers.Contentful +{ + public class LayoutService + { + } +} From bccd51fa05db8260b2edfb8d9f209cb66323b2ee Mon Sep 17 00:00:00 2001 From: Gleb Raytsin Date: Wed, 17 Aug 2022 13:11:48 -0400 Subject: [PATCH 10/11] Turning off the test for now. --- .../IntegrationTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs b/src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs index 9c414a1..de528e9 100644 --- a/src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs +++ b/src/RocketInsights.DXP.Providers.Contentful.Tests/IntegrationTests.cs @@ -35,7 +35,7 @@ public IntegrationTests() } - [TestMethod] + //[TestMethod] public async Task TestRetrievingAContentFragmentFromContentful() { var provider = ServiceCollection.BuildServiceProvider(); From b04631494fa829ce38d8acbc5ed9e0bed4f9703b Mon Sep 17 00:00:00 2001 From: Gleb Raytsin Date: Thu, 18 Aug 2022 10:43:39 -0400 Subject: [PATCH 11/11] Adding readme with details about required settings. --- .../README.md | 1 + .../README.md | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 src/RocketInsights.DXP.Providers.Contentful.Tests/README.md create mode 100644 src/RocketInsights.DXP.Providers.Contentful/README.md diff --git a/src/RocketInsights.DXP.Providers.Contentful.Tests/README.md b/src/RocketInsights.DXP.Providers.Contentful.Tests/README.md new file mode 100644 index 0000000..d7e894a --- /dev/null +++ b/src/RocketInsights.DXP.Providers.Contentful.Tests/README.md @@ -0,0 +1 @@ +Please see the readme in `RocketInsights.DXP.Providers.Contentful` project for details about required config settings needed to run this. \ No newline at end of file diff --git a/src/RocketInsights.DXP.Providers.Contentful/README.md b/src/RocketInsights.DXP.Providers.Contentful/README.md new file mode 100644 index 0000000..04c4db0 --- /dev/null +++ b/src/RocketInsights.DXP.Providers.Contentful/README.md @@ -0,0 +1,12 @@ +This project relies on configuration settings to be set. These are essentially API keys and related info needed to access the Contentful site. + +While the `Environment` and `SpaceId` are not secret and can be stored elsewhere, greater care should be taken for the `API Key` as it should be kept secure. + +For Development purpose, it can be simpler to just add the block to `User Secrets` (right click on project and select `Manage User Secrets`) +```JS +{ + "ContentfulOptions:DeliveryApiKey": "", + "ContentfulOptions:Environment": "", + "ContentfulOptions:SpaceId": "" +} +``` \ No newline at end of file