diff --git a/Kontent.Ai.Management.Tests/CodeSamples/CmApiV2.cs b/Kontent.Ai.Management.Tests/CodeSamples/CmApiV2.cs index d7e2710f..b2422aa1 100644 --- a/Kontent.Ai.Management.Tests/CodeSamples/CmApiV2.cs +++ b/Kontent.Ai.Management.Tests/CodeSamples/CmApiV2.cs @@ -487,6 +487,25 @@ public async void GetLanguageVariant() Assert.NotNull(response); } + // DocSection: cm_api_v2_get_published_variant + // Tip: Find more about .NET SDKs at https://kontent.ai/learn/net + [Fact] + public async void GetPublishedLanguageVariant() + { + var client = _fileSystemFixture.CreateMockClientWithResponse("LanguageVariant.json"); + + var identifier = new LanguageVariantIdentifier(Reference.ById(Guid.Parse("f4b3fc05-e988-4dae-9ac1-a94aba566474")), Reference.ById(Guid.Parse("d1f95fde-af02-b3b5-bd9e-f232311ccab8"))); + // var identifier = new LanguageVariantIdentifier(Reference.ById(Guid.Parse("f4b3fc05-e988-4dae-9ac1-a94aba566474")), Reference.ByCodename("es-ES")); + // var identifier = new LanguageVariantIdentifier(Reference.ByCodename("on_roasts"), Reference.ById(Guid.Parse("d1f95fde-af02-b3b5-bd9e-f232311ccab8"))); + // var identifier = new LanguageVariantIdentifier(Reference.ByCodename("on_roasts"), Reference.ByCodename("es-ES")); + // var identifier = new LanguageVariantIdentifier(Reference.ByExternalId("59713"), Reference.ById(Guid.Parse("d1f95fde-af02-b3b5-bd9e-f232311ccab8"))); + // var identifier = new LanguageVariantIdentifier(Reference.ByExternalId("59713"), Reference.ByCodename("es-ES")); + + var response = await client.GetPublishedLanguageVariantAsync(identifier); + + Assert.NotNull(response); + } + // DocSection: cm_api_v2_get_variants // Tip: Find more about .NET SDKs at https://kontent.ai/learn/net [Fact] diff --git a/Kontent.Ai.Management.Tests/ManagementClientTests/LanguageVariantTests.cs b/Kontent.Ai.Management.Tests/ManagementClientTests/LanguageVariantTests.cs index 3619fb55..40adff74 100644 --- a/Kontent.Ai.Management.Tests/ManagementClientTests/LanguageVariantTests.cs +++ b/Kontent.Ai.Management.Tests/ManagementClientTests/LanguageVariantTests.cs @@ -287,6 +287,60 @@ await client.Invoking(x => x.GetLanguageVariantAsync(null)) .Should().ThrowExactlyAsync(); } + [Theory] + [ClassData(typeof(CombinationOfIdentifiersAndUrl))] + public async Task GetPublishedLanguageVariantAsync_StronglyTyped_GetsVariant(LanguageVariantIdentifier identifier, string expectedUrl) + { + var client = _scenario + .WithResponses("LanguageVariant.json") + .CreateManagementClient(); + + var response = await client.GetPublishedLanguageVariantAsync(identifier); + + _scenario + .CreateExpectations() + .HttpMethod(HttpMethod.Get) + .Response(response, GetExpectedComplexTestModel()) + .Url(expectedUrl + "/published") + .Validate(); + } + + [Fact] + public async Task GetPublishedLanguageVariantAsync_StronglyTyped_IdentifierIsNull_Throws() + { + var client = _scenario.CreateManagementClient(); + + await client.Invoking(x => x.GetPublishedLanguageVariantAsync(null)) + .Should().ThrowExactlyAsync(); + } + + [Theory] + [ClassData(typeof(CombinationOfIdentifiersAndUrl))] + public async Task GetPublishedLanguageVariantAsync_DynamicallyTyped_GetsVariant(LanguageVariantIdentifier identifier, string expectedUrl) + { + var client = _scenario + .WithResponses("LanguageVariant.json") + .CreateManagementClient(); + + var response = await client.GetPublishedLanguageVariantAsync(identifier); + + _scenario + .CreateExpectations() + .HttpMethod(HttpMethod.Get) + .Response(response, GetExpectedLanguageVariantModel()) + .Url(expectedUrl + "/published") + .Validate(); + } + + [Fact] + public async Task GetPublishedLanguageVariantAsync_DynamicallyTyped_IdentifierIsNull_Throws() + { + var client = _scenario.CreateManagementClient(); + + await client.Invoking(x => x.GetPublishedLanguageVariantAsync(null)) + .Should().ThrowExactlyAsync(); + } + [Theory] [ClassData(typeof(CombinationOfIdentifiersAndUrl))] public async Task UpsertLanguageVariantAsync_StronglyTyped_UpsertsVariant(LanguageVariantIdentifier identifier, string expectedUrl) diff --git a/Kontent.Ai.Management/IManagementClient.cs b/Kontent.Ai.Management/IManagementClient.cs index c27372c5..3077f416 100644 --- a/Kontent.Ai.Management/IManagementClient.cs +++ b/Kontent.Ai.Management/IManagementClient.cs @@ -343,6 +343,21 @@ public interface IManagementClient /// The instance that represents language variant. Task> GetLanguageVariantAsync(LanguageVariantIdentifier identifier) where T : new(); + /// + /// Returns strongly typed currently published language variant. + /// + /// The identifier of the language variant. + /// The instance that represents language variant. + Task GetPublishedLanguageVariantAsync(LanguageVariantIdentifier identifier); + + /// + /// Returns strongly typed currently published language variant with strongly typed elements. + /// + /// Type of the content item elements + /// The identifier of the language variant. + /// The instance that represents language variant. + Task> GetPublishedLanguageVariantAsync(LanguageVariantIdentifier identifier) where T : new(); + /// /// Returns environment information /// diff --git a/Kontent.Ai.Management/ManagementClient.LanguageVariant.cs b/Kontent.Ai.Management/ManagementClient.LanguageVariant.cs index 3ef138e7..bb1006c2 100644 --- a/Kontent.Ai.Management/ManagementClient.LanguageVariant.cs +++ b/Kontent.Ai.Management/ManagementClient.LanguageVariant.cs @@ -137,6 +137,34 @@ public async Task GetLanguageVariantAsync(LanguageVariantI return _modelProvider.GetLanguageVariantModel(response); } + /// + public async Task GetPublishedLanguageVariantAsync(LanguageVariantIdentifier identifier) + { + if (identifier == null) + { + throw new ArgumentNullException(nameof(identifier)); + } + + var endpointUrl = _urlBuilder.BuildPublishedVariantsUrl(identifier); + var response = await _actionInvoker.InvokeReadOnlyMethodAsync(endpointUrl, HttpMethod.Get); + + return response; + } + + /// + public async Task> GetPublishedLanguageVariantAsync(LanguageVariantIdentifier identifier) where T : new() + { + if (identifier == null) + { + throw new ArgumentNullException(nameof(identifier)); + } + + var endpointUrl = _urlBuilder.BuildPublishedVariantsUrl(identifier); + var response = await _actionInvoker.InvokeReadOnlyMethodAsync(endpointUrl, HttpMethod.Get); + + return _modelProvider.GetLanguageVariantModel(response); + } + /// public async Task UpsertLanguageVariantAsync(LanguageVariantIdentifier identifier, LanguageVariantUpsertModel languageVariantUpsertModel) { diff --git a/Kontent.Ai.Management/Modules/UrlBuilder/EndpointUrlBuilder.cs b/Kontent.Ai.Management/Modules/UrlBuilder/EndpointUrlBuilder.cs index f1f8b1c6..f5b6787f 100644 --- a/Kontent.Ai.Management/Modules/UrlBuilder/EndpointUrlBuilder.cs +++ b/Kontent.Ai.Management/Modules/UrlBuilder/EndpointUrlBuilder.cs @@ -71,6 +71,12 @@ public string BuildVariantsUrl(LanguageVariantIdentifier identifier) => GetEnvir _itemTemplate.GetIdentifierUrlSegment(identifier.ItemIdentifier), _variantTemplate.GetIdentifierUrlSegment(identifier.LanguageIdentifier))); + public string BuildPublishedVariantsUrl(LanguageVariantIdentifier identifier) => GetEnvironmentUrl( + string.Concat( + _itemTemplate.GetIdentifierUrlSegment(identifier.ItemIdentifier), + _variantTemplate.GetIdentifierUrlSegment(identifier.LanguageIdentifier), + "/published")); + public string BuildTypeUrl() => GetEnvironmentUrl(_typeTemplate.Url); public string BuildTypeUrl(Reference identifier) => GetEnvironmentUrl(_typeTemplate.GetIdentifierUrlSegment(identifier));