Skip to content

Commit

Permalink
EN-238 Add WebSpotlight support via Management Api
Browse files Browse the repository at this point in the history
  • Loading branch information
František Záhora authored and František Záhora committed Jul 4, 2024
1 parent 24e60b5 commit e0c9db6
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"enabled": true,
"root_type_id": "0689fcb3-24f1-49f4-b115-e7b816d59e9d"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"enabled": true,
"root_type_id": "3660e894-bae8-4dcd-9d3e-5fc9205c2ece"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"enabled": false,
"root_type_id": "0689fcb3-24f1-49f4-b115-e7b816d59e9d"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"enabled": true,
"root_type_id": "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
@@ -0,0 +1,88 @@
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 response = await client
.ActivateWebSpotlightAsync();

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

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

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

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);
}
}
26 changes: 26 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 @@ -788,4 +789,29 @@ 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 status with possibility to provide existing Root Type ID.
/// </summary>
/// <param name="webSpotlightActivateModel">Represents configuration that will be used for web spotlight activation.</param>
/// <returns>The <see cref="WebSpotlightModel"/> instance that represents the web spotlight status.</returns>
Task<WebSpotlightModel> ActivateWebSpotlightAsync(WebSpotlightActivateModel webSpotlightActivateModel);

/// <summary>
/// Activates the web spotlight status.
/// </summary>
/// <returns>The <see cref="WebSpotlightModel"/> instance that represents the web spotlight status.</returns>
Task<WebSpotlightModel> ActivateWebSpotlightAsync();

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

/// <summary>
/// Returns the web spotlight status.
/// </summary>
/// <returns>The <see cref="WebSpotlightModel"/> instance that represents the web spotlight status.</returns>
Task<WebSpotlightModel> GetWebSpotlightStatusAsync();
}
42 changes: 42 additions & 0 deletions Kontent.Ai.Management/ManagementClient.WebSpotlight.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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> ActivateWebSpotlightAsync()
{
var endpointUrl = _urlBuilder.BuildWebSpotlightUrl();
return await _actionInvoker.InvokeReadOnlyMethodAsync<WebSpotlightModel>(endpointUrl, HttpMethod.Put);
}

/// <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
@@ -0,0 +1,16 @@
using Newtonsoft.Json;
using System;

namespace Kontent.Ai.Management.Models.WebSpotlight;

/// <summary>
/// Represents the web spotlight activation model.
/// </summary>
public class WebSpotlightActivateModel
{
/// <summary>
/// Gets or sets the web spotlight's Root Type ID.
/// </summary>
[JsonProperty("root_type_id")]
public Guid? RootTypeId { get; set; }
}
22 changes: 22 additions & 0 deletions Kontent.Ai.Management/Models/WebSpotlight/WebSpotlightModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Newtonsoft.Json;
using System;

namespace Kontent.Ai.Management.Models.WebSpotlight;

/// <summary>
/// Represents the web spotlight model.
/// </summary>
public class WebSpotlightModel
{
/// <summary>
/// Gets or sets the web spotlight's Enabled.
/// </summary>
[JsonProperty("enabled")]
public bool Enabled { get; set; }

/// <summary>
/// Gets or sets the web spotlight's Root Type ID.
/// </summary>
[JsonProperty("root_type_id")]
public Guid? RootTypeId { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal sealed class EndpointUrlBuilder
private readonly ItemTemplate _itemTemplate;
private readonly EnvironmentRolesTemplate _environmentRolesTemplate;
private readonly UserTemplate _userTemplate;
private readonly WebSpotlightTemplate _webSpotlightTemplate;

private readonly ManagementOptions _options;

Expand All @@ -52,6 +53,7 @@ public EndpointUrlBuilder(ManagementOptions options)
_itemTemplate = new ItemTemplate();
_environmentRolesTemplate = new EnvironmentRolesTemplate();
_userTemplate = new UserTemplate();
_webSpotlightTemplate = new WebSpotlightTemplate();

_options = options;
}
Expand Down Expand Up @@ -217,6 +219,8 @@ public string BuildSubscriptionUserDeactivateDisableUrl(UserIdentifier identifie

public string BuildMarkEnvironmentAsProductionUrl() => GetEnvironmentUrl("/mark-environment-as-production");

public string BuildWebSpotlightUrl() => GetEnvironmentUrl(_webSpotlightTemplate.Url);

private string GetEnvironmentUrl(string path, params string[] parameters) => GetUrl(BuildEnvironmentUrl(), path, parameters);

private string GetSubscriptionUrl(string path, params string[] parameters) => GetUrl(BuildSubscriptionUrl(), path, parameters);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

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

internal class WebSpotlightTemplate : UrlTemplate
{
public override string Url => "/web-spotlight";
public override string UrlId => throw new InvalidOperationException("Web Spotlight does not have Id Url.");
public override string UrlCodename => throw new InvalidOperationException("Web Spotlight does not have Codename Url.");
public override string UrlExternalId => throw new InvalidOperationException("Web Spotlight does not have External Id Url.");
}

0 comments on commit e0c9db6

Please sign in to comment.