From 57a7c41534ea3f063afdd30ba19ad3267631ff11 Mon Sep 17 00:00:00 2001 From: Albie Date: Sat, 27 Nov 2021 16:57:01 +0000 Subject: [PATCH] move to overridable function --- .../Requests/FilteredRequest.cs | 14 +++++--------- DragonFruit.Common.Data/ApiClient.cs | 4 +--- DragonFruit.Common.Data/ApiRequest.cs | 8 ++++++++ DragonFruit.Common.Data/RequestFilter.cs | 13 ------------- 4 files changed, 14 insertions(+), 25 deletions(-) delete mode 100644 DragonFruit.Common.Data/RequestFilter.cs diff --git a/DragonFruit.Common.Data.Tests/Requests/FilteredRequest.cs b/DragonFruit.Common.Data.Tests/Requests/FilteredRequest.cs index e160701..a5d5f2a 100644 --- a/DragonFruit.Common.Data.Tests/Requests/FilteredRequest.cs +++ b/DragonFruit.Common.Data.Tests/Requests/FilteredRequest.cs @@ -5,22 +5,18 @@ namespace DragonFruit.Common.Data.Tests.Requests { - [ExceptionFilter] internal class FilteredRequest : ApiRequest { public override string Path { get; } + + protected override void OnRequestExecuting(ApiClient client) + { + throw new ArgumentException(); + } } internal class InheritedRequest : FilteredRequest { // this should have the exception filter applied to it as well } - - internal class ExceptionFilter : RequestFilter - { - protected override void OnRequestExecuting(ApiClient client, ApiRequest request) - { - throw new ArgumentException(); - } - } } diff --git a/DragonFruit.Common.Data/ApiClient.cs b/DragonFruit.Common.Data/ApiClient.cs index af9249f..a459f0c 100644 --- a/DragonFruit.Common.Data/ApiClient.cs +++ b/DragonFruit.Common.Data/ApiClient.cs @@ -5,7 +5,6 @@ using System.Globalization; using System.Linq; using System.Net.Http; -using System.Reflection; using System.Threading; using System.Threading.Tasks; using DragonFruit.Common.Data.Exceptions; @@ -287,8 +286,7 @@ protected virtual void ValidateRequest(ApiRequest request) } } - // check if there is a filter assigned to the class and perform it - request.GetType().GetCustomAttribute()?.OnRequestExecuting(this, request); + request.OnRequestExecuting(this); } /// diff --git a/DragonFruit.Common.Data/ApiRequest.cs b/DragonFruit.Common.Data/ApiRequest.cs index 3a02e52..67b5a52 100644 --- a/DragonFruit.Common.Data/ApiRequest.cs +++ b/DragonFruit.Common.Data/ApiRequest.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Net.Http; using System.Net.Http.Headers; +using DragonFruit.Common.Data.Exceptions; using DragonFruit.Common.Data.Parameters; using DragonFruit.Common.Data.Serializers; using DragonFruit.Common.Data.Utils; @@ -99,6 +100,13 @@ internal string QueryString /// protected virtual IEnumerable> AdditionalQueries { get; } + /// + /// Overridable method for specifying an action to occur before sending the request to the + /// + protected internal virtual void OnRequestExecuting(ApiClient client) + { + } + /// /// Create a for this , which can then be modified manually or overriden by /// diff --git a/DragonFruit.Common.Data/RequestFilter.cs b/DragonFruit.Common.Data/RequestFilter.cs deleted file mode 100644 index f4ffb5f..0000000 --- a/DragonFruit.Common.Data/RequestFilter.cs +++ /dev/null @@ -1,13 +0,0 @@ -// DragonFruit.Common Copyright 2021 DragonFruit Network -// Licensed under the MIT License. Please refer to the LICENSE file at the root of this project for details - -using System; - -namespace DragonFruit.Common.Data -{ - [AttributeUsage(AttributeTargets.Class)] - public abstract class RequestFilter : Attribute - { - protected internal abstract void OnRequestExecuting(ApiClient client, ApiRequest request); - } -}