Skip to content

Commit

Permalink
EN-137 - Add access to published content
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirO-kontent authored and pokornyd committed May 13, 2024
1 parent 5ab22f2 commit ec7db0f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Kontent.Ai.Management.Tests/CodeSamples/CmApiV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,60 @@ await client.Invoking(x => x.GetLanguageVariantAsync(null))
.Should().ThrowExactlyAsync<ArgumentNullException>();
}

[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<ComplexTestModel>(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<ComplexTestModel>(null))
.Should().ThrowExactlyAsync<ArgumentNullException>();
}

[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<ArgumentNullException>();
}

[Theory]
[ClassData(typeof(CombinationOfIdentifiersAndUrl))]
public async Task UpsertLanguageVariantAsync_StronglyTyped_UpsertsVariant(LanguageVariantIdentifier identifier, string expectedUrl)
Expand Down
15 changes: 15 additions & 0 deletions Kontent.Ai.Management/IManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,21 @@ public interface IManagementClient
/// <returns>The <see cref="LanguageVariantModel{T}"/> instance that represents language variant.</returns>
Task<LanguageVariantModel<T>> GetLanguageVariantAsync<T>(LanguageVariantIdentifier identifier) where T : new();

/// <summary>
/// Returns strongly typed currently published language variant.
/// </summary>
/// <param name="identifier">The identifier of the language variant.</param>
/// <returns>The <see cref="LanguageVariantModel"/> instance that represents language variant.</returns>
Task<LanguageVariantModel> GetPublishedLanguageVariantAsync(LanguageVariantIdentifier identifier);

/// <summary>
/// Returns strongly typed currently published language variant with strongly typed elements.
/// </summary>
/// <typeparam name="T">Type of the content item elements</typeparam>
/// <param name="identifier">The identifier of the language variant.</param>
/// <returns>The <see cref="LanguageVariantModel{T}"/> instance that represents language variant.</returns>
Task<LanguageVariantModel<T>> GetPublishedLanguageVariantAsync<T>(LanguageVariantIdentifier identifier) where T : new();

/// <summary>
/// Returns environment information
/// </summary>
Expand Down
28 changes: 28 additions & 0 deletions Kontent.Ai.Management/ManagementClient.LanguageVariant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ public async Task<LanguageVariantModel> GetLanguageVariantAsync(LanguageVariantI
return _modelProvider.GetLanguageVariantModel<T>(response);
}

/// <inheritdoc />
public async Task<LanguageVariantModel> GetPublishedLanguageVariantAsync(LanguageVariantIdentifier identifier)
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}

var endpointUrl = _urlBuilder.BuildPublishedVariantsUrl(identifier);
var response = await _actionInvoker.InvokeReadOnlyMethodAsync<LanguageVariantModel>(endpointUrl, HttpMethod.Get);

return response;
}

/// <inheritdoc />
public async Task<LanguageVariantModel<T>> GetPublishedLanguageVariantAsync<T>(LanguageVariantIdentifier identifier) where T : new()
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}

var endpointUrl = _urlBuilder.BuildPublishedVariantsUrl(identifier);
var response = await _actionInvoker.InvokeReadOnlyMethodAsync<LanguageVariantModel>(endpointUrl, HttpMethod.Get);

return _modelProvider.GetLanguageVariantModel<T>(response);
}

/// <inheritdoc />
public async Task<LanguageVariantModel> UpsertLanguageVariantAsync(LanguageVariantIdentifier identifier, LanguageVariantUpsertModel languageVariantUpsertModel)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit ec7db0f

Please sign in to comment.