Skip to content

Commit

Permalink
Refactored out the filter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jezzsantos committed Feb 4, 2025
1 parent d8a7fdf commit d31ebbe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
13 changes: 3 additions & 10 deletions src/Infrastructure.Web.Api.Common/Endpoints/ApiUsageFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ private void TrackResponse(EndpointFilterInvocationContext context, ICallerConte
}

private static Dictionary<string, object> PopulatePropertiesFromRequest(HttpContext httpContext,
IWebRequest request,
ICallerContext caller)
IWebRequest request, ICallerContext caller)
{
var requestName = request.GetType().Name.ToLowerInvariant();
var route = httpContext.GetEndpoint()!.DisplayName!;
Expand Down Expand Up @@ -126,14 +125,8 @@ private static Dictionary<string, object> PopulatePropertiesFromRequest(HttpCont

private static IWebRequest? GetRequest(EndpointFilterInvocationContext context)
{
var arguments = context.Arguments;
if (arguments.Count < 2)
{
return null;
}

var request = context.Arguments[1]!;
if (request is not IWebRequest webRequest)
var webRequest = context.GetRequestDto();
if (webRequest.NotExists())
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Infrastructure.Web.Api.Interfaces;
using Microsoft.AspNetCore.Http;

namespace Infrastructure.Web.Api.Common.Endpoints;

public static class EndpointFilterExtensions
{
/// <summary>
/// Determines the request DTO of the current HTTP request.
/// We expect that all <see cref="RequestDelegate" /> are of this form: (IMediatr mediatr, IWebRequest request)
/// where the second parameter is an instance of a <see cref="IWebRequest{TResponse}" />
/// </summary>
public static IWebRequest? GetRequestDto(this EndpointFilterInvocationContext context)
{
var arguments = context.Arguments;
if (arguments.Count < 2)
{
return null;
}

var request = context.Arguments[1]!;
if (request is not IWebRequest webRequest)
{
return null;
}

return webRequest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public HttpRecordingFilter(IRecorder recorder, ICallerContextFactory callerConte
return response;
}

private void TraceRequest(IRecorder recorder, ICallerContext caller, string requestDescriptor)
private static void TraceRequest(IRecorder recorder, ICallerContext caller, string requestDescriptor)
{
recorder.TraceInformation(caller.ToCall(),
#if TESTINGONLY
Expand All @@ -60,8 +60,8 @@ private void TraceRequest(IRecorder recorder, ICallerContext caller, string requ
requestDescriptor);
}

private void TraceResponse(IRecorder recorder, ICallerContext caller, EndpointFilterInvocationContext context,
string requestDescriptor, object response)
private static void TraceResponse(IRecorder recorder, ICallerContext caller,
EndpointFilterInvocationContext context, string requestDescriptor, object response)
{
var httpResponse = context.HttpContext.Response;
var statusCode = httpResponse.StatusCode;
Expand Down

0 comments on commit d31ebbe

Please sign in to comment.