Deduplicate the magic string "/management/api/" (#16016)

This commit is contained in:
Kenn Jacobsen
2024-04-10 14:21:59 +02:00
committed by GitHub
parent 7b6db02426
commit 1c799802c5
5 changed files with 13 additions and 9 deletions

View File

@@ -1,3 +1,5 @@
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Api.Common.Security;
public static class Paths
@@ -14,7 +16,7 @@ public static class Paths
public static readonly string RevokeEndpoint = EndpointPath($"{EndpointTemplate}/revoke");
private static string EndpointPath(string relativePath) => $"/umbraco/management/api/v1/{relativePath}";
private static string EndpointPath(string relativePath) => $"/umbraco{Constants.Web.ManagementApiPath}v1/{relativePath}";
}
public static class MemberApi

View File

@@ -27,8 +27,7 @@ internal static class ApplicationBuilderExtensions
var officePath = settings.GetBackOfficePath(hostingEnvironment);
// Only use the API exception handler when we are requesting an API
// FIXME: magic string "management/api" is used several times across the codebase
return httpContext.Request.Path.Value?.StartsWith($"{officePath}/management/api/") ?? false;
return httpContext.Request.Path.Value?.StartsWith($"{officePath}{Constants.Web.ManagementApiPath}") ?? false;
},
innerBuilder =>
{
@@ -65,8 +64,7 @@ internal static class ApplicationBuilderExtensions
endpoints.MapControllers();
// Serve contract
// FIXME: magic string "management/api" is used several times across the codebase
endpoints.MapGet($"{officePath}/management/api/openapi.json", async context =>
endpoints.MapGet($"{officePath}{Constants.Web.ManagementApiPath}openapi.json", async context =>
{
await context.Response.SendFileAsync(new EmbeddedFileProvider(typeof(ManagementApiComposer).Assembly).GetFileInfo("OpenApi.json"));
});

View File

@@ -1,5 +1,4 @@

using Umbraco.Cms.Core;
using Umbraco.Cms.Web.Common.Routing;
namespace Umbraco.Cms.Api.Management.Routing;
@@ -7,7 +6,7 @@ namespace Umbraco.Cms.Api.Management.Routing;
public class VersionedApiBackOfficeRouteAttribute : BackOfficeRouteAttribute
{
public VersionedApiBackOfficeRouteAttribute(string template)
: base($"management/api/v{{version:apiVersion}}/{template.TrimStart('/')}")
: base($"{Constants.Web.ManagementApiPath}v{{version:apiVersion}}/{template.TrimStart('/')}")
{
}
}

View File

@@ -62,6 +62,11 @@ public static partial class Constants
public const string BackOfficeLoginArea = "UmbracoLogin"; // Used for area routes of non-api controllers for login
}
/// <summary>
/// The "base" path to the Management API
/// </summary>
public const string ManagementApiPath = "/management/api/";
public static class Routing
{
public const string ControllerToken = "controller";

View File

@@ -40,7 +40,7 @@ public class UmbracoRequestPaths
_previewMvcPath = "/" + mvcArea + "/Preview/";
_surfaceMvcPath = "/" + mvcArea + "/Surface/";
_apiMvcPath = "/" + mvcArea + "/Api/";
_managementApiPath = "/" + mvcArea + "/management/api/";
_managementApiPath = "/" + mvcArea + Constants.Web.ManagementApiPath;
_installPath = hostingEnvironment.ToAbsolute(Constants.SystemDirectories.Install);
_umbracoRequestPathsOptions = umbracoRequestPathsOptions;
}