Skip to content

Commit

Permalink
Merge pull request #2 from redbridge-uk/webapicore
Browse files Browse the repository at this point in the history
Tidy up api core
  • Loading branch information
binarysenator authored Nov 21, 2022
2 parents 634735a + 24cf312 commit 4d2235e
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 93 deletions.
34 changes: 3 additions & 31 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,12 @@
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/.vs/Redbridge.WebApiFramework
/Redbridge.WebApi/bin/Debug/net472
/Redbridge.WebApi/obj
/Redbridge.WebApi.Tests/bin/Debug/net472
/Redbridge.WebApi.Tests/obj
/.vs/ProjectEvaluation/redbridge.webapiframework.projects.v5.1
/.vs/ProjectEvaluation/redbridge.webapiframework.projects.v2
/.vs/ProjectEvaluation/redbridge.webapiframework.metadata.v5.1
/.vs/ProjectEvaluation/redbridge.webapiframework.metadata.v2
/__azurite_db_table__.json
/__azurite_db_queue__.json
/__azurite_db_queue_extent__.json
/__azurite_db_blob__.json
/__azurite_db_blob_extent__.json
/__blobstorage__/289dd10e-63cb-41f9-ac37-d811b89ca871
/Redbridge.WebApiCore/obj/Redbridge.WebApiCore.csproj.nuget.g.targets
/Redbridge.WebApiCore/obj/Redbridge.WebApiCore.csproj.nuget.g.props
/Redbridge.WebApiCore/obj/Redbridge.WebApiCore.csproj.nuget.dgspec.json
/Redbridge.WebApiCore/obj/project.nuget.cache
/Redbridge.WebApiCore/obj/project.assets.json
/Redbridge.WebApiCore/obj/Debug/net6.0/_IsIncrementalBuild
/Redbridge.WebApiCore/obj/Debug/net6.0/refint/Redbridge.WebApiCore.dll
/Redbridge.WebApiCore/obj/Debug/net6.0/ref/Redbridge.WebApiCore.dll
/Redbridge.WebApiCore/obj/Debug/net6.0/Redbridge.WebApiCore.pdb
/Redbridge.WebApiCore/obj/Debug/net6.0/Redbridge.WebApiCore.GlobalUsings.g.cs
/Redbridge.WebApiCore/obj/Debug/net6.0/Redbridge.WebApiCore.GeneratedMSBuildEditorConfig.editorconfig
/Redbridge.WebApiCore/obj/Debug/net6.0/Redbridge.WebApiCore.dll
/Redbridge.WebApiCore/obj/Debug/net6.0/Redbridge.WebApiCore.csproj.FileListAbsolute.txt
/Redbridge.WebApiCore/obj/Debug/net6.0/Redbridge.WebApiCore.csproj.CoreCompileInputs.cache
/Redbridge.WebApiCore/obj/Debug/net6.0/Redbridge.WebApiCore.csproj.AssemblyReference.cache
/Redbridge.WebApiCore/obj/Debug/net6.0/Redbridge.WebApiCore.assets.cache
/Redbridge.WebApiCore/obj/Debug/net6.0/Redbridge.WebApiCore.AssemblyInfoInputs.cache
/Redbridge.WebApiCore/obj/Debug/net6.0/Redbridge.WebApiCore.AssemblyInfo.cs
/Redbridge.WebApiCore/obj/Debug/net6.0/.NETCoreApp,Version=v6.0.AssemblyAttributes.cs
/Redbridge.WebApiCore/bin/Debug/net6.0/Redbridge.WebApiCore.pdb
/Redbridge.WebApiCore/bin/Debug/net6.0/Redbridge.WebApiCore.dll
/Redbridge.WebApiCore/bin/Debug/net6.0/Redbridge.WebApiCore.deps.json
.vs
bin
obj
4 changes: 2 additions & 2 deletions Redbridge.WebApi.Tests/TestExceptionLoggerFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class TestLoggingExceptionFilter
public void FilterExceptionNormalMessage()
{
var logger = new Mock<ILogger>();
var filter = new LoggingExceptionFilter(logger.Object);
var filter = new LoggingExceptionFilterAttribute(logger.Object);
var httpActionContext = new HttpActionContext
{
ControllerContext = new HttpControllerContext
Expand All @@ -36,7 +36,7 @@ public void FilterExceptionNormalMessage()
public void FilterExceptionMessageWithCarriageReturns()
{
var logger = new Mock<ILogger>();
var filter = new LoggingExceptionFilter(logger.Object);
var filter = new LoggingExceptionFilterAttribute(logger.Object);
var messageBuilder = new StringBuilder("Unknown/invalid some sort of message:");
messageBuilder.AppendLine("Link ID: abc");
messageBuilder.AppendLine("Link ID: def");
Expand Down
4 changes: 2 additions & 2 deletions Redbridge.WebApi.Tests/TestUnknownEntityException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public UnknownItemException(string message) : base(message) { }
public void ProcessUnknownEntityExceptionConvertTo422Response()
{
var logger = new Mock<ILogger>();
var filter = new UnknownEntityExceptionFilter(logger.Object);
var filter = new UnknownEntityExceptionFilterAttribute(logger.Object);

var httpActionContext = new HttpActionContext
{
Expand All @@ -46,7 +46,7 @@ public void ProcessUnknownEntityExceptionConvertTo422Response()
public void ProcessUnknownEntityExceptionConvertToValidationException()
{
var logger = new Mock<ILogger>();
var filter = new UnknownEntityExceptionFilter(logger.Object);
var filter = new UnknownEntityExceptionFilterAttribute(logger.Object);

var httpActionContext = new HttpActionContext
{
Expand Down
8 changes: 4 additions & 4 deletions Redbridge.WebApi.Tests/TestValidationExceptionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public class TestValidationExceptionFilter
public void FilterValidationExceptionSingleType()
{
var logger = new Mock<ILogger>();
var filter = new ValidationExceptionFilter(logger.Object);
var filter = new ValidationExceptionFilterAttribute(logger.Object);
filter.OnException(new HttpActionExecutedContext() { });
}

[Test()]
public void FilterValidationExceptionSingleTypeViewValidateBodyJson()
{
var logger = new Mock<ILogger>();
var filter = new ValidationExceptionFilter(logger.Object);
var filter = new ValidationExceptionFilterAttribute(logger.Object);

var httpActionContext = new HttpActionContext
{
Expand All @@ -55,7 +55,7 @@ public void FilterValidationExceptionSingleTypeViewValidateBodyJson()
public void FilterValidationExceptionMultiTypeViewValidateBodyJson()
{
var logger = new Mock<ILogger>();
var filter = new ValidationExceptionFilter(logger.Object);
var filter = new ValidationExceptionFilterAttribute(logger.Object);

var httpActionContext = new HttpActionContext
{
Expand Down Expand Up @@ -89,7 +89,7 @@ public void FilterValidationExceptionMultiTypeViewValidateBodyJson()
public void FilterValidationExceptionMultiTypeViewValidateBodyJsonNoDefinedMessage()
{
var logger = new Mock<ILogger>();
var filter = new ValidationExceptionFilter(logger.Object);
var filter = new ValidationExceptionFilterAttribute(logger.Object);

var httpActionContext = new HttpActionContext
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public static void InstallExceptionFilters(this HttpConfiguration configuration,
if (configuration == null) throw new ArgumentNullException(nameof(configuration));

logger.WriteDebug("Installing exception filters...");
configuration.Filters.Add(new ValidationExceptionFilter(logger));
configuration.Filters.Add(new UnknownEntityExceptionFilter(logger));
configuration.Filters.Add(new UserNotAuthenticatedExceptionFilter(logger));
configuration.Filters.Add(new UserNotAuthorizedExceptionFilter(logger));
configuration.Filters.Add(new LoggingExceptionFilter(logger));
configuration.Filters.Add(new ValidationExceptionFilterAttribute(logger));
configuration.Filters.Add(new UnknownEntityExceptionFilterAttribute(logger));
configuration.Filters.Add(new UserNotAuthenticatedExceptionFilterAttribute());
configuration.Filters.Add(new UserNotAuthorizedExceptionFilterAttribute(logger));
configuration.Filters.Add(new LoggingExceptionFilterAttribute(logger));
configuration.MessageHandlers.Add(new NotFoundCustomMessageHandler());

logger.WriteDebug("Installing exception logger...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace Redbridge.WebApi.Filters
{
public class LoggingExceptionFilter : ExceptionFilterAttribute
public class LoggingExceptionFilterAttribute : ExceptionFilterAttribute
{
private readonly ILogger _logger;

public LoggingExceptionFilter(ILogger logger)
public LoggingExceptionFilterAttribute(ILogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@

namespace Redbridge.WebApi.Filters
{
public class UnknownEntityExceptionFilter : ExceptionFilterAttribute
public class UnknownEntityExceptionFilterAttribute : ExceptionFilterAttribute
{
private readonly ILogger _logger;

public UnknownEntityExceptionFilter(ILogger logger)
public UnknownEntityExceptionFilterAttribute(ILogger logger)
{
if (logger == null) throw new ArgumentNullException(nameof(logger));
_logger = logger;
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
var unknownEntityException = actionExecutedContext.Exception as UnknownEntityException;

if (unknownEntityException != null)
if (actionExecutedContext.Exception is UnknownEntityException unknownEntityException)
{
_logger.WriteInfo($"Unknown entity exception processing with message {unknownEntityException.Message}");

Expand Down
34 changes: 0 additions & 34 deletions Redbridge.WebApi/Filters/UserNotAuthenticatedExceptionFilter.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Net;
using System.Net.Http;
using System.Web.Http.Filters;
using Redbridge.Exceptions;

namespace Redbridge.WebApi.Filters
{
public class UserNotAuthenticatedExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception is UserNotAuthenticatedException)
{
var responseMessage = new HttpResponseMessage(HttpStatusCode.Unauthorized);
actionExecutedContext.Response = responseMessage;
actionExecutedContext.Exception = null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

namespace Redbridge.WebApi.Filters
{
public class UserNotAuthorizedExceptionFilter : ExceptionFilterAttribute
public class UserNotAuthorizedExceptionFilterAttribute : ExceptionFilterAttribute
{
readonly ILogger _logger;

public UserNotAuthorizedExceptionFilter(ILogger logger)
public UserNotAuthorizedExceptionFilterAttribute(ILogger logger)
{
if (logger == null) throw new System.ArgumentNullException(nameof(logger));
_logger = logger;
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
}

public override void OnException(HttpActionExecutedContext actionExecutedContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

namespace Redbridge.WebApi.Filters
{
public class ValidationExceptionFilter : ExceptionFilterAttribute
public class ValidationExceptionFilterAttribute : ExceptionFilterAttribute
{
readonly ILogger _logger;

public ValidationExceptionFilter(ILogger logger)
public ValidationExceptionFilterAttribute(ILogger logger)
{
if (logger == null) throw new ArgumentNullException(nameof(logger));
_logger = logger;
Expand Down
18 changes: 18 additions & 0 deletions Redbridge.WebApiCore.Tests/Redbridge.WebApiCore.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Redbridge.WebApiCore.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
6 changes: 6 additions & 0 deletions Redbridge.WebApiFramework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Redbridge.WebApi.Tests", "R
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Redbridge.WebApiCore", "Redbridge.WebApiCore\Redbridge.WebApiCore.csproj", "{CC19B56C-15E4-4CD7-9ACE-4AA6CA22A214}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Redbridge.WebApiCore.Tests", "Redbridge.WebApiCore.Tests\Redbridge.WebApiCore.Tests.csproj", "{826C1EB0-0466-405E-A567-F912DD9FE39B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{CC19B56C-15E4-4CD7-9ACE-4AA6CA22A214}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC19B56C-15E4-4CD7-9ACE-4AA6CA22A214}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC19B56C-15E4-4CD7-9ACE-4AA6CA22A214}.Release|Any CPU.Build.0 = Release|Any CPU
{826C1EB0-0466-405E-A567-F912DD9FE39B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{826C1EB0-0466-405E-A567-F912DD9FE39B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{826C1EB0-0466-405E-A567-F912DD9FE39B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{826C1EB0-0466-405E-A567-F912DD9FE39B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 4d2235e

Please sign in to comment.