Skip to content

Commit

Permalink
Merge branch 'add-missing-generics' of https://github.com/DiegoFaFe/m…
Browse files Browse the repository at this point in the history
…anagement-sdk-net into add-missing-generics
  • Loading branch information
pokornyd committed Aug 12, 2024
2 parents 8dcac4f + c79343b commit cf54056
Show file tree
Hide file tree
Showing 18 changed files with 337 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Kontent.Ai.Management.Tests/Data/AssetFolder/Folder.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
{
"id": "958001d8-2228-4373-b966-5262b5b96f71",
"name": "Downloads",
"codename": "downloads",
"external_id": "folder-with-downloadable-assets",
"folders": [
{
"id": "9ca927b6-6e4d-4d6b-81e3-ec5e8f7772a0",
"name": "Archives",
"codename": "archives",
"external_id": "folder-with-downloadable-archives",
"folders": []
}
Expand All @@ -16,6 +18,7 @@
{
"id": "9ca927b6-6e4d-4d6b-81e3-ec5e8f7772a0",
"name": "Legal documents",
"codename": "legal_documents",
"external_id": "folder-documents",
"folders": []
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"enabled": true,
"root_type": "0689fcb3-24f1-49f4-b115-e7b816d59e9d"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"enabled": true,
"root_type": "3660e894-bae8-4dcd-9d3e-5fc9205c2ece"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"enabled": false,
"root_type": "0689fcb3-24f1-49f4-b115-e7b816d59e9d"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"enabled": true,
"root_type": "0689fcb3-24f1-49f4-b115-e7b816d59e9d"
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
<None Update="Data\**\*.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\WebSpotlight\DeactivationWebSpotlightResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\WebSpotlight\ActivationWebSpotlightWithProvidedRootTypeIdResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\WebSpotlight\GetStatusWebSpotlightResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public async Task CreateAssetFoldersAsync_CreatesFolder()
{
ExternalId = "external-id",
Name= "name",
Codename = "codename",
Folders = Enumerable.Empty<AssetFolderHierarchy>()
}
}
Expand Down Expand Up @@ -114,6 +115,7 @@ public async Task ModifyAssetFoldersAsync_ChangesAreNull_Throws()
{
ExternalId = "external-id",
Name= "name",
Codename = "codename",
Folders = Enumerable.Empty<AssetFolderHierarchy>()
},
Before = Reference.ByCodename("codename"),
Expand Down
113 changes: 113 additions & 0 deletions Kontent.Ai.Management.Tests/ManagementClientTests/WebSpotlightTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using Kontent.Ai.Management.Models.Shared;
using Kontent.Ai.Management.Models.WebSpotlight;
using Kontent.Ai.Management.Tests.Base;
using System;
using System.Net.Http;
using Xunit;
using static Kontent.Ai.Management.Tests.Base.Scenario;

namespace Kontent.Ai.Management.Tests.ManagementClientTests;

public class WebSpotlightTests : IClassFixture<FileSystemFixture>
{
private static readonly string WebSpotlightBaseUrl = $"{Endpoint}/projects/{ENVIRONMENT_ID}/web-spotlight";
private readonly Scenario _scenario = new(folder: "WebSpotlight");

[Fact]
public async void ActivateWebSpotlight_Returns_EnabledStatusAndRootTypeId()
{
var client = _scenario
.WithResponses("ActivationWebSpotlightResponse.json")
.CreateManagementClient();

var webSpotlightActivateModel = new WebSpotlightActivateModel { RootType = null };

var response = await client
.ActivateWebSpotlightAsync(webSpotlightActivateModel);

_scenario
.CreateExpectations()
.HttpMethod(HttpMethod.Put)
.Response(response)
.Url(WebSpotlightBaseUrl)
.Validate();
}

[Fact]
public async void ActivateWebSpotlight_WithProvidedValidRootTypeById_Returns_EnabledStatusAndRootTypeId()
{
var client = _scenario
.WithResponses("ActivationWebSpotlightWithProvidedRootTypeIdResponse.json")
.CreateManagementClient();

var rootTypeId = Guid.Parse("3660e894-bae8-4dcd-9d3e-5fc9205c2ece");
var reference = Reference.ById(rootTypeId);
var webSpotlightActivateModel = new WebSpotlightActivateModel { RootType = reference };

await client.ActivateWebSpotlightAsync(webSpotlightActivateModel);

_scenario
.CreateExpectations()
.HttpMethod(HttpMethod.Put)
.RequestPayload(webSpotlightActivateModel)
.Url(WebSpotlightBaseUrl)
.Validate();
}

[Fact]
public async void ActivateWebSpotlight_WithProvidedValidRootTypeByCodename_Returns_EnabledStatusAndRootTypeId()
{
var client = _scenario
.WithResponses("ActivationWebSpotlightWithProvidedRootTypeIdResponse.json")
.CreateManagementClient();

var rootTypeCodename = "root_type_codename";
var reference = Reference.ByCodename(rootTypeCodename);
var webSpotlightActivateModel = new WebSpotlightActivateModel { RootType = reference };

await client.ActivateWebSpotlightAsync(webSpotlightActivateModel);

_scenario
.CreateExpectations()
.HttpMethod(HttpMethod.Put)
.RequestPayload(webSpotlightActivateModel)
.Url(WebSpotlightBaseUrl)
.Validate();
}

[Fact]
public async void DeactivateWebSpotlight_Returns_DisabledStatusAndRootTypeId()
{
var client = _scenario
.WithResponses("DeactivationWebSpotlightResponse.json")
.CreateManagementClient();

var response = await client
.DeactivateWebSpotlightAsync();

_scenario
.CreateExpectations()
.HttpMethod(HttpMethod.Put)
.Response(response)
.Url(WebSpotlightBaseUrl)
.Validate();
}

[Fact]
public async void GetWebSpotlightStatus_Returns_StatusAndRootTypeId()
{
var client = _scenario
.WithResponses("GetStatusWebSpotlightResponse.json")
.CreateManagementClient();

var response = await client
.GetWebSpotlightStatusAsync();

_scenario
.CreateExpectations()
.HttpMethod(HttpMethod.Get)
.Response(response)
.Url(WebSpotlightBaseUrl)
.Validate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Xunit;

namespace Kontent.Ai.Management.Tests.Modules.UrlBuilder;

public partial class EndpointUrlBuilderTests
{
[Fact]
public void BuildWebSpotlightUrl_ReturnsWebSpotlightUrl()
{
var expectedUrl = $"{ENDPOINT}/projects/{ENVIRONMENT_ID}/web-spotlight";
var actualUrl = _builder.BuildWebSpotlightUrl();

Assert.Equal(expectedUrl, actualUrl);
}
}
59 changes: 59 additions & 0 deletions Kontent.Ai.Management/Extensions/AssetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ public static AssetFolderHierarchy GetFolderHierarchyByExternalId(this IEnumerab
}
return null;
}

/// <summary>
/// Gets folder hierarchy for a given folder codename
/// </summary>
/// <param name="folders">The <see cref="AssetFoldersModel.Folders"/> property retrieved from the <see cref="IManagementClient.GetAssetFoldersAsync"/> method.</param>
/// <param name="codename">Folder codename</param>
/// <returns>The <see cref="AssetFolderHierarchy"/> instance that represents the folder found for a given folder codename. Null if not found.</returns>
public static AssetFolderHierarchy GetFolderHierarchyByCodename(this IEnumerable<AssetFolderHierarchy> folders, string codename)
{
if (folders == null)
{
return null;
}

// Recursively search for the folder hierarchy that an asset is in. Returns null if file is not in a folder.
foreach (var folder in folders)
{
if (folder.Codename == codename)
{
return folder;
}

var nestedFolder = folder.Folders?.GetFolderHierarchyByCodename(codename);
if (nestedFolder != null) //This is required so you don't stop processing if the root contains many folders (let the above foreach loop continue)
{
return nestedFolder;
}
}
return null;
}

/// <summary>
/// Gets the full folder path string
Expand Down Expand Up @@ -147,6 +177,34 @@ public static AssetFolderLinkingHierarchy GetParentLinkedFolderHierarchyByExtern
}
return null;
}

/// <summary>
/// Gets the folder hierarchy for a given folder identifier.
/// To use this method first convert your <see cref="AssetFoldersModel.Folders"/> property retrieved from <see cref="IManagementClient.GetAssetFoldersAsync"/> to a <see cref="IEnumerable{AssetFolderLinkingHierarchy}">IEnumerable&lt;AssetFolderLinkingHierarchy&gt;</see> by using the <see cref="GetParentLinkedFolderHierarchy"/> method.
/// </summary>
/// <param name="folders">The <see cref="IEnumerable{AssetFolderLinkingHierarchy}"/> instance.</param>
/// <param name="codename">Folder codename</param>
/// <returns>Returns the <see cref="AssetFolderLinkingHierarchy"/> instance found via a given folder identifier.</returns>
public static AssetFolderLinkingHierarchy GetParentLinkedFolderHierarchyByCodename(this IEnumerable<AssetFolderLinkingHierarchy> folders, string codename)
{
if (folders != null)
{
foreach (var folder in folders)
{
if (folder.Codename == codename)
{
return folder;
}

var nestedFolder = folder.Folders?.GetParentLinkedFolderHierarchyByCodename(codename);
if (nestedFolder != null) // This is required so you don't stop processing if the root contains many folders (let the above for-each loop continue)
{
return nestedFolder;
}
}
}
return null;
}

/// <summary>
/// Retrieves a list of folders with the <see cref="AssetFolderLinkingHierarchy.Parent"/> property filled in.
Expand All @@ -166,6 +224,7 @@ public static IEnumerable<AssetFolderLinkingHierarchy> GetParentLinkedFolderHier
ExternalId = itm.ExternalId,
Folders = itm.Folders != null ? new List<AssetFolderLinkingHierarchy>() : null,
Id = itm.Id,
Codename = itm.Codename,
Name = itm.Name
};
if (itm.Folders != null)
Expand Down
20 changes: 20 additions & 0 deletions Kontent.Ai.Management/IManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Kontent.Ai.Management.Models.TypeSnippets.Patch;
using Kontent.Ai.Management.Models.Users;
using Kontent.Ai.Management.Models.Webhooks;
using Kontent.Ai.Management.Models.WebSpotlight;
using Kontent.Ai.Management.Models.Workflow;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -814,4 +815,23 @@ public interface IManagementClient
/// <param name="previewConfiguration">Represents configuration that will be used for project.</param>
/// <returns>The <see cref="PreviewConfigurationModel"/> instance that represents the preview configuration.</returns>
Task<PreviewConfigurationModel> ModifyPreviewConfigurationAsync(PreviewConfigurationModel previewConfiguration);

/// <summary>
/// Activates the web spotlight, allowing you to specify an existing Root Type ID.
/// </summary>
/// <param name="webSpotlightActivateModel">Represents configuration that will be used for web spotlight activation.</param>
/// <returns>A <see cref="WebSpotlightModel"/> instance representing the web spotlight status.</returns>
Task<WebSpotlightModel> ActivateWebSpotlightAsync(WebSpotlightActivateModel webSpotlightActivateModel);

/// <summary>
/// Deactivates the web spotlight.
/// </summary>
/// <returns>A <see cref="WebSpotlightModel"/> instance representing the web spotlight status.</returns>
Task<WebSpotlightModel> DeactivateWebSpotlightAsync();

/// <summary>
/// Returns the web spotlight status.
/// </summary>
/// <returns>A <see cref="WebSpotlightModel"/> instance representing the web spotlight status.</returns>
Task<WebSpotlightModel> GetWebSpotlightStatusAsync();
}
35 changes: 35 additions & 0 deletions Kontent.Ai.Management/ManagementClient.WebSpotlight.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Kontent.Ai.Management.Models.WebSpotlight;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace Kontent.Ai.Management;

/// <summary>
/// Executes requests against the Kontent.ai Management API.
/// </summary>
public partial class ManagementClient
{
/// <inheritdoc />
public async Task<WebSpotlightModel> ActivateWebSpotlightAsync(WebSpotlightActivateModel webSpotlightActivateModel)
{
ArgumentNullException.ThrowIfNull(webSpotlightActivateModel);

var endpointUrl = _urlBuilder.BuildWebSpotlightUrl();
return await _actionInvoker.InvokeMethodAsync<WebSpotlightActivateModel, WebSpotlightModel>(endpointUrl, HttpMethod.Put, webSpotlightActivateModel);
}

/// <inheritdoc />
public async Task<WebSpotlightModel> DeactivateWebSpotlightAsync()
{
var endpointUrl = _urlBuilder.BuildWebSpotlightUrl();
return await _actionInvoker.InvokeReadOnlyMethodAsync<WebSpotlightModel>(endpointUrl, HttpMethod.Put);
}

/// <inheritdoc />
public async Task<WebSpotlightModel> GetWebSpotlightStatusAsync()
{
var endpointUrl = _urlBuilder.BuildWebSpotlightUrl();
return await _actionInvoker.InvokeReadOnlyMethodAsync<WebSpotlightModel>(endpointUrl, HttpMethod.Get);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public sealed class AssetFolderHierarchy
/// </summary>
[JsonProperty("external_id", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string ExternalId { get; set; }

/// <summary>
/// Gets or sets the codename of the folder.
/// </summary>
[JsonProperty("codename", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Codename { get; set; }

/// <summary>
/// Gets or sets the name of the folder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public sealed class AssetFolderLinkingHierarchy
/// </summary>
[JsonProperty("external_id", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string ExternalId { get; set; }

/// <summary>
/// Gets or sets the folder's codename.
/// </summary>
[JsonProperty("codename", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Codename { get; set; }

/// <summary>
/// Name of the folder
Expand Down
Loading

0 comments on commit cf54056

Please sign in to comment.