From ef1dddd229fede9123592af0ec71ab959bc30bf9 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Tue, 4 Apr 2023 15:11:13 +0200 Subject: [PATCH 01/27] feature: Added Implementation for Interceptors --- gen/SourceGenerator/SourceGenerator.csproj | 2 +- src/RestSharp/Options/RestClientOptions.cs | 3 ++ src/RestSharp/RestClient.Async.cs | 33 +++++++++++++++++++- src/RestSharp/RestClient.Extensions.cs | 1 - src/RestSharp/RestSharp.csproj | 3 ++ src/RestSharp/Serializers/RestSerializers.cs | 10 ++++++ 6 files changed, 49 insertions(+), 3 deletions(-) diff --git a/gen/SourceGenerator/SourceGenerator.csproj b/gen/SourceGenerator/SourceGenerator.csproj index 6b640d2f8..5e1577cf8 100644 --- a/gen/SourceGenerator/SourceGenerator.csproj +++ b/gen/SourceGenerator/SourceGenerator.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 diff --git a/src/RestSharp/Options/RestClientOptions.cs b/src/RestSharp/Options/RestClientOptions.cs index d5efd312e..43faacdc5 100644 --- a/src/RestSharp/Options/RestClientOptions.cs +++ b/src/RestSharp/Options/RestClientOptions.cs @@ -21,6 +21,7 @@ using System.Text; using RestSharp.Authenticators; using RestSharp.Extensions; +using RestSharp.Interceptors; namespace RestSharp; @@ -59,6 +60,8 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba /// public IAuthenticator? Authenticator { get; set; } + public List Interceptors { get; set; } = new(); + /// /// Passed to Credentials property /// diff --git a/src/RestSharp/RestClient.Async.cs b/src/RestSharp/RestClient.Async.cs index 0848f8442..64f8f6d7f 100644 --- a/src/RestSharp/RestClient.Async.cs +++ b/src/RestSharp/RestClient.Async.cs @@ -86,6 +86,8 @@ async Task ExecuteRequestAsync(RestRequest request, CancellationTo throw new ObjectDisposedException(nameof(RestClient)); } + await OnBeforeSerialization(request); + using var requestContent = new RequestContent(this, request); var authenticator = request.Authenticator ?? Options.Authenticator; @@ -112,8 +114,8 @@ async Task ExecuteRequestAsync(RestRequest request, CancellationTo .AddAcceptHeader(AcceptedContentTypes) .AddCookieHeaders(cookieContainer, url); message.AddHeaders(headers); - if (request.OnBeforeRequest != null) await request.OnBeforeRequest(message).ConfigureAwait(false); + await OnBeforeRequest(message); var responseMessage = await HttpClient.SendAsync(message, request.CompletionOption, ct).ConfigureAwait(false); @@ -130,6 +132,7 @@ async Task ExecuteRequestAsync(RestRequest request, CancellationTo } if (request.OnAfterRequest != null) await request.OnAfterRequest(responseMessage).ConfigureAwait(false); + await OnAfterRequest(responseMessage); return new HttpResponse(responseMessage, url, cookieContainer, null, timeoutCts.Token); } @@ -138,6 +141,34 @@ async Task ExecuteRequestAsync(RestRequest request, CancellationTo } } + /// + /// Will be called before the Request becomes Serialized + /// + /// RestRequest before it will be serialized + async Task OnBeforeSerialization(RestRequest request) { + foreach (var interceptor in Options.Interceptors) { + await interceptor.InterceptBeforeSerialization(request); //.ThrowExceptionIfAvailable(); + } + } + /// + /// Will be calld before the Request will be sent + /// + /// HttpRequestMessage ready to be sent + async Task OnBeforeRequest(HttpRequestMessage requestMessage) { + foreach (var interceptor in Options.Interceptors) { + await interceptor.InterceptBeforeRequest(requestMessage); + } + } + /// + /// Will be called after the Response has been received from Server + /// + /// HttpResponseMessage as received from server + async Task OnAfterRequest(HttpResponseMessage responseMessage) { + foreach (var interceptor in Options.Interceptors) { + await interceptor.InterceptAfterRequest(responseMessage); + } + } + record HttpResponse( HttpResponseMessage? ResponseMessage, Uri Url, diff --git a/src/RestSharp/RestClient.Extensions.cs b/src/RestSharp/RestClient.Extensions.cs index 1ed035160..f382024fe 100644 --- a/src/RestSharp/RestClient.Extensions.cs +++ b/src/RestSharp/RestClient.Extensions.cs @@ -14,7 +14,6 @@ using System.Runtime.CompilerServices; using RestSharp.Extensions; -using RestSharp.Serializers; namespace RestSharp; diff --git a/src/RestSharp/RestSharp.csproj b/src/RestSharp/RestSharp.csproj index 7a9f11d0b..080c7f0e8 100644 --- a/src/RestSharp/RestSharp.csproj +++ b/src/RestSharp/RestSharp.csproj @@ -40,4 +40,7 @@ + + + diff --git a/src/RestSharp/Serializers/RestSerializers.cs b/src/RestSharp/Serializers/RestSerializers.cs index c946aaa4e..dfafc07e9 100644 --- a/src/RestSharp/Serializers/RestSerializers.cs +++ b/src/RestSharp/Serializers/RestSerializers.cs @@ -37,6 +37,7 @@ internal RestResponse Deserialize(RestRequest request, RestResponse raw, R try { request.OnBeforeDeserialization?.Invoke(raw); + OnBeforeDeserialization(raw, options); response.Data = DeserializeContent(raw); } catch (Exception ex) { @@ -51,6 +52,15 @@ internal RestResponse Deserialize(RestRequest request, RestResponse raw, R return response; } + /// + /// Will be called before the Data will be serialized + /// + /// RestResponse with Data still in Content + async Task OnBeforeDeserialization(RestResponse raw, ReadOnlyRestClientOptions options) { + foreach (var interceptor in options.Interceptors) { + await interceptor.InterceptBeforeDeserialize(raw); //.ThrowExceptionIfAvailable(); + } + } /// /// Deserialize the response content into the specified type From b8d7ec87e2720f48dfe3f05811737564f74e1a17 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Tue, 4 Apr 2023 15:13:10 +0200 Subject: [PATCH 02/27] feature: Added Interfaces for Interceptors --- RestSharp.sln | 34 ++++++++++++++++++- .../Interceptors/IAfterRequestInterceptor.cs | 17 ++++++++++ .../IBeforeDeserializationInterceptor.cs | 17 ++++++++++ .../Interceptors/IBeforeRequestInterceptor.cs | 19 +++++++++++ .../IBeforeSerializationInterceptor.cs | 14 ++++++++ src/RestSharp/Interceptors/IInterceptor.cs | 14 ++++++++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/RestSharp/Interceptors/IAfterRequestInterceptor.cs create mode 100644 src/RestSharp/Interceptors/IBeforeDeserializationInterceptor.cs create mode 100644 src/RestSharp/Interceptors/IBeforeRequestInterceptor.cs create mode 100644 src/RestSharp/Interceptors/IBeforeSerializationInterceptor.cs create mode 100644 src/RestSharp/Interceptors/IInterceptor.cs diff --git a/RestSharp.sln b/RestSharp.sln index 25439c208..e44711190 100644 --- a/RestSharp.sln +++ b/RestSharp.sln @@ -37,7 +37,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RestSharp.Tests.Serializers EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SourceGen", "SourceGen", "{55B8F371-B2BA-4DEE-AB98-5BAB8A21B1C2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGenerator", "gen\SourceGenerator\SourceGenerator.csproj", "{FE778406-ADCF-45A1-B775-A054B55BFC50}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGenerator", "gen\SourceGenerator\SourceGenerator.csproj", "{FE778406-ADCF-45A1-B775-A054B55BFC50}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestSharp.ConsoleTest", "RestSharp.ConsoleTest\RestSharp.ConsoleTest.csproj", "{5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -478,6 +480,36 @@ Global {FE778406-ADCF-45A1-B775-A054B55BFC50}.Release|x64.Build.0 = Release|Any CPU {FE778406-ADCF-45A1-B775-A054B55BFC50}.Release|x86.ActiveCfg = Release|Any CPU {FE778406-ADCF-45A1-B775-A054B55BFC50}.Release|x86.Build.0 = Release|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug.Appveyor|Any CPU.ActiveCfg = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug.Appveyor|Any CPU.Build.0 = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug.Appveyor|ARM.ActiveCfg = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug.Appveyor|ARM.Build.0 = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug.Appveyor|Mixed Platforms.ActiveCfg = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug.Appveyor|Mixed Platforms.Build.0 = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug.Appveyor|x64.ActiveCfg = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug.Appveyor|x64.Build.0 = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug.Appveyor|x86.ActiveCfg = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug.Appveyor|x86.Build.0 = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug|ARM.ActiveCfg = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug|ARM.Build.0 = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug|x64.ActiveCfg = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug|x64.Build.0 = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug|x86.ActiveCfg = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Debug|x86.Build.0 = Debug|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Release|Any CPU.Build.0 = Release|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Release|ARM.ActiveCfg = Release|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Release|ARM.Build.0 = Release|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Release|x64.ActiveCfg = Release|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Release|x64.Build.0 = Release|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Release|x86.ActiveCfg = Release|Any CPU + {5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/RestSharp/Interceptors/IAfterRequestInterceptor.cs b/src/RestSharp/Interceptors/IAfterRequestInterceptor.cs new file mode 100644 index 000000000..28ecd4904 --- /dev/null +++ b/src/RestSharp/Interceptors/IAfterRequestInterceptor.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RestSharp.Interceptors { + /// + /// Interceptor which will be executed after the response is comming back from server + /// + public interface IAfterRequestInterceptor { + /// + /// Interceptor Method which will be called when a message is comming back from server before it becomes deserialized + /// + /// Pure Http Response + /// + public ValueTask InterceptAfterRequest(HttpResponseMessage responseMessage); + } +} diff --git a/src/RestSharp/Interceptors/IBeforeDeserializationInterceptor.cs b/src/RestSharp/Interceptors/IBeforeDeserializationInterceptor.cs new file mode 100644 index 000000000..c95d6dddd --- /dev/null +++ b/src/RestSharp/Interceptors/IBeforeDeserializationInterceptor.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RestSharp.Interceptors { + /// + /// Interceptor called when before the Response has been deserialized + /// + public interface IBeforeDeserializeInterceptor { + /// + /// Function called before the Response has been deserialized + /// + /// RestResponse With Raw Response Content + /// Result of Execution + public ValueTask InterceptBeforeDeserialize(RestResponse request); + } +} diff --git a/src/RestSharp/Interceptors/IBeforeRequestInterceptor.cs b/src/RestSharp/Interceptors/IBeforeRequestInterceptor.cs new file mode 100644 index 000000000..96044d67b --- /dev/null +++ b/src/RestSharp/Interceptors/IBeforeRequestInterceptor.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RestSharp.Interceptors { + + /// + /// Interceptor which will be executed before the Request becomes sent + /// + public interface IBeforeRequestInterceptor { + + /// + /// Interceptor Function called when before the request becomes send + /// + /// Raw Http Request ready to be sent + /// Result of the Interceptor Method + public ValueTask InterceptBeforeRequest(HttpRequestMessage req); + } +} diff --git a/src/RestSharp/Interceptors/IBeforeSerializationInterceptor.cs b/src/RestSharp/Interceptors/IBeforeSerializationInterceptor.cs new file mode 100644 index 000000000..d2eba1840 --- /dev/null +++ b/src/RestSharp/Interceptors/IBeforeSerializationInterceptor.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RestSharp.Interceptors { + public interface IBeforeSerializationInterceptor { + /// + /// Interceptor Function which will be called before before the Serialization + /// + /// Outgoing request + /// Result of the Excecution + public ValueTask InterceptBeforeSerialization(RestRequest request); + } +} diff --git a/src/RestSharp/Interceptors/IInterceptor.cs b/src/RestSharp/Interceptors/IInterceptor.cs new file mode 100644 index 000000000..85f0095a4 --- /dev/null +++ b/src/RestSharp/Interceptors/IInterceptor.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RestSharp.Interceptors { + + /// + /// Interceptor, which intercepts outgoing requests and incoming responses + /// + public interface IInterceptor: IBeforeSerializationInterceptor,IBeforeRequestInterceptor, IAfterRequestInterceptor, IBeforeDeserializeInterceptor { + + + } +} From 25f5d650043ed64e41525d3b169277877bb9239c Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Tue, 4 Apr 2023 15:54:20 +0200 Subject: [PATCH 03/27] fix: Corrected Comment --- src/RestSharp/RestClient.Async.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RestSharp/RestClient.Async.cs b/src/RestSharp/RestClient.Async.cs index be05a75df..620fbfd9f 100644 --- a/src/RestSharp/RestClient.Async.cs +++ b/src/RestSharp/RestClient.Async.cs @@ -156,7 +156,7 @@ async Task OnBeforeSerialization(RestRequest request) { } } /// - /// Will be calld before the Request will be sent + /// Will be called before the Request will be sent /// /// HttpRequestMessage ready to be sent async Task OnBeforeRequest(HttpRequestMessage requestMessage) { From 85b6c799d14c1056dca8a81bfa4dc620b6f5aeee Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Tue, 4 Apr 2023 16:04:38 +0200 Subject: [PATCH 04/27] fix: Configured to wait for result --- src/RestSharp/Serializers/RestSerializers.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/RestSharp/Serializers/RestSerializers.cs b/src/RestSharp/Serializers/RestSerializers.cs index f9817cabd..425cdbd62 100644 --- a/src/RestSharp/Serializers/RestSerializers.cs +++ b/src/RestSharp/Serializers/RestSerializers.cs @@ -39,7 +39,7 @@ internal RestResponse Deserialize(RestRequest request, RestResponse raw, R try { request.OnBeforeDeserialization?.Invoke(raw); - OnBeforeDeserialization(raw, options); + OnBeforeDeserialization(raw, options).ConfigureAwait(true); response.Data = DeserializeContent(raw); } catch (Exception ex) { @@ -58,6 +58,7 @@ internal RestResponse Deserialize(RestRequest request, RestResponse raw, R /// Will be called before the Data will be serialized /// /// RestResponse with Data still in Content + /// RestClient options but readonly async Task OnBeforeDeserialization(RestResponse raw, ReadOnlyRestClientOptions options) { foreach (var interceptor in options.Interceptors) { await interceptor.InterceptBeforeDeserialize(raw); //.ThrowExceptionIfAvailable(); From 9d7b59f60072b3b536d9bc55b897d67f4d2b7cfb Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Tue, 4 Apr 2023 16:05:15 +0200 Subject: [PATCH 05/27] refactor: Renamed Class name to go allong with Class Name conventions --- .../SampleClasses/twitter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/RestSharp.Tests.Serializers.Xml/SampleClasses/twitter.cs b/test/RestSharp.Tests.Serializers.Xml/SampleClasses/twitter.cs index ec0af5146..671df1649 100644 --- a/test/RestSharp.Tests.Serializers.Xml/SampleClasses/twitter.cs +++ b/test/RestSharp.Tests.Serializers.Xml/SampleClasses/twitter.cs @@ -22,7 +22,7 @@ public class status { public string in_reply_to_screen_name { get; set; } // ignore contributors for now - public user user { get; set; } + public User user { get; set; } // ignore geo public long id { get; set; } @@ -30,7 +30,7 @@ public class status { public string text { get; set; } } -public class user { +public class User { public string url { get; set; } public string description { get; set; } @@ -106,7 +106,7 @@ public class complexStatus { public string in_reply_to_screen_name { get; set; } // ignore contributors for now - [DeserializeAs(Name = "user.following")] + [DeserializeAs(Name = "User.following")] public bool follow { get; set; } // ignore geo From 86772f5aa7605050ecc39184048e13154f5c489e Mon Sep 17 00:00:00 2001 From: fseidl-bauradar <125438559+fseidl-bauradar@users.noreply.github.com> Date: Tue, 4 Apr 2023 16:28:07 +0200 Subject: [PATCH 06/27] Update RestSharp.sln Removed Console.Test which is not inside Repository --- RestSharp.sln | 2 -- 1 file changed, 2 deletions(-) diff --git a/RestSharp.sln b/RestSharp.sln index e44711190..1cafbe805 100644 --- a/RestSharp.sln +++ b/RestSharp.sln @@ -39,8 +39,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SourceGen", "SourceGen", "{ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGenerator", "gen\SourceGenerator\SourceGenerator.csproj", "{FE778406-ADCF-45A1-B775-A054B55BFC50}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestSharp.ConsoleTest", "RestSharp.ConsoleTest\RestSharp.ConsoleTest.csproj", "{5E8D472F-5A12-4CD8-8DBE-E3F6E0F76798}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug.Appveyor|Any CPU = Debug.Appveyor|Any CPU From 5c91edd6b99336cd8631050f3174ac75d2a9899c Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Tue, 4 Apr 2023 18:22:59 +0200 Subject: [PATCH 07/27] Removed ConsoleTest from Repository From b9f408273959e3701df0104d15320945cf6e1fa8 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Tue, 4 Apr 2023 18:36:37 +0200 Subject: [PATCH 08/27] Feature: Added Base Interceptor to simplify definition of Interceptors --- src/RestSharp/Interceptors/BaseInterceptor.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/RestSharp/Interceptors/BaseInterceptor.cs diff --git a/src/RestSharp/Interceptors/BaseInterceptor.cs b/src/RestSharp/Interceptors/BaseInterceptor.cs new file mode 100644 index 000000000..c118094fa --- /dev/null +++ b/src/RestSharp/Interceptors/BaseInterceptor.cs @@ -0,0 +1,37 @@ +// Copyright (c) .NET Foundation and Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +namespace RestSharp.Interceptors; + +/// +/// Base Interceptor +/// +public class BaseInterceptor : IInterceptor { + public ValueTask InterceptBeforeSerialization(RestRequest request) { + return new(); + } + + public ValueTask InterceptBeforeRequest(HttpRequestMessage req) { + return new(); + } + + public ValueTask InterceptAfterRequest(HttpResponseMessage responseMessage) { + return new(); + } + + public ValueTask InterceptBeforeDeserialize(RestResponse request) { + return new(); + } +} \ No newline at end of file From fa2662e779661af64da42ef9b76b17a8763e851a Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Tue, 4 Apr 2023 19:17:17 +0200 Subject: [PATCH 09/27] Fix: Added virtual keyword to allow overriding the functions in derived classes --- src/RestSharp/Interceptors/BaseInterceptor.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/RestSharp/Interceptors/BaseInterceptor.cs b/src/RestSharp/Interceptors/BaseInterceptor.cs index c118094fa..e631ea462 100644 --- a/src/RestSharp/Interceptors/BaseInterceptor.cs +++ b/src/RestSharp/Interceptors/BaseInterceptor.cs @@ -19,19 +19,19 @@ namespace RestSharp.Interceptors; /// Base Interceptor /// public class BaseInterceptor : IInterceptor { - public ValueTask InterceptBeforeSerialization(RestRequest request) { + public virtual ValueTask InterceptBeforeSerialization(RestRequest request) { return new(); } - public ValueTask InterceptBeforeRequest(HttpRequestMessage req) { + public virtual ValueTask InterceptBeforeRequest(HttpRequestMessage req) { return new(); } - public ValueTask InterceptAfterRequest(HttpResponseMessage responseMessage) { + public virtual ValueTask InterceptAfterRequest(HttpResponseMessage responseMessage) { return new(); } - public ValueTask InterceptBeforeDeserialize(RestResponse request) { + public virtual ValueTask InterceptBeforeDeserialize(RestResponse request) { return new(); } } \ No newline at end of file From 1cd427ac03ebe1c8777a826c67224180863c62e5 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 19 Apr 2023 13:06:03 +0200 Subject: [PATCH 10/27] Solved Merge conflict --- src/RestSharp/RestClient.Async.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/RestSharp/RestClient.Async.cs b/src/RestSharp/RestClient.Async.cs index fb8b75168..324cde4b3 100644 --- a/src/RestSharp/RestClient.Async.cs +++ b/src/RestSharp/RestClient.Async.cs @@ -76,14 +76,7 @@ async Task ExecuteRequestAsync(RestRequest request, CancellationTo if (_disposed) { throw new ObjectDisposedException(nameof(RestClient)); } - -<<<<<<< HEAD -======= - await OnBeforeSerialization(request); - - using var requestContent = new RequestContent(this, request); - ->>>>>>> dev + await OnBeforeSerialization(request); var authenticator = request.Authenticator ?? Options.Authenticator; if (authenticator != null) await authenticator.Authenticate(this, request).ConfigureAwait(false); From afb6ff36ac4560722f733770bfc5d0af49c8cac1 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Thu, 4 May 2023 18:31:42 +0200 Subject: [PATCH 11/27] refactor: Removed Interfaces --- .../Interceptors/IAfterRequestInterceptor.cs | 17 ----------------- .../IBeforeDeserializationInterceptor.cs | 17 ----------------- .../Interceptors/IBeforeRequestInterceptor.cs | 19 ------------------- .../IBeforeSerializationInterceptor.cs | 14 -------------- 4 files changed, 67 deletions(-) delete mode 100644 src/RestSharp/Interceptors/IAfterRequestInterceptor.cs delete mode 100644 src/RestSharp/Interceptors/IBeforeDeserializationInterceptor.cs delete mode 100644 src/RestSharp/Interceptors/IBeforeRequestInterceptor.cs delete mode 100644 src/RestSharp/Interceptors/IBeforeSerializationInterceptor.cs diff --git a/src/RestSharp/Interceptors/IAfterRequestInterceptor.cs b/src/RestSharp/Interceptors/IAfterRequestInterceptor.cs deleted file mode 100644 index 28ecd4904..000000000 --- a/src/RestSharp/Interceptors/IAfterRequestInterceptor.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace RestSharp.Interceptors { - /// - /// Interceptor which will be executed after the response is comming back from server - /// - public interface IAfterRequestInterceptor { - /// - /// Interceptor Method which will be called when a message is comming back from server before it becomes deserialized - /// - /// Pure Http Response - /// - public ValueTask InterceptAfterRequest(HttpResponseMessage responseMessage); - } -} diff --git a/src/RestSharp/Interceptors/IBeforeDeserializationInterceptor.cs b/src/RestSharp/Interceptors/IBeforeDeserializationInterceptor.cs deleted file mode 100644 index c95d6dddd..000000000 --- a/src/RestSharp/Interceptors/IBeforeDeserializationInterceptor.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace RestSharp.Interceptors { - /// - /// Interceptor called when before the Response has been deserialized - /// - public interface IBeforeDeserializeInterceptor { - /// - /// Function called before the Response has been deserialized - /// - /// RestResponse With Raw Response Content - /// Result of Execution - public ValueTask InterceptBeforeDeserialize(RestResponse request); - } -} diff --git a/src/RestSharp/Interceptors/IBeforeRequestInterceptor.cs b/src/RestSharp/Interceptors/IBeforeRequestInterceptor.cs deleted file mode 100644 index 96044d67b..000000000 --- a/src/RestSharp/Interceptors/IBeforeRequestInterceptor.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace RestSharp.Interceptors { - - /// - /// Interceptor which will be executed before the Request becomes sent - /// - public interface IBeforeRequestInterceptor { - - /// - /// Interceptor Function called when before the request becomes send - /// - /// Raw Http Request ready to be sent - /// Result of the Interceptor Method - public ValueTask InterceptBeforeRequest(HttpRequestMessage req); - } -} diff --git a/src/RestSharp/Interceptors/IBeforeSerializationInterceptor.cs b/src/RestSharp/Interceptors/IBeforeSerializationInterceptor.cs deleted file mode 100644 index d2eba1840..000000000 --- a/src/RestSharp/Interceptors/IBeforeSerializationInterceptor.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace RestSharp.Interceptors { - public interface IBeforeSerializationInterceptor { - /// - /// Interceptor Function which will be called before before the Serialization - /// - /// Outgoing request - /// Result of the Excecution - public ValueTask InterceptBeforeSerialization(RestRequest request); - } -} From 56086d6a4d8cc13b3cb68b3da86fc5fd30b44c75 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Thu, 4 May 2023 18:31:56 +0200 Subject: [PATCH 12/27] refactor: Removed Interfaces --- src/RestSharp/Interceptors/IInterceptor.cs | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 src/RestSharp/Interceptors/IInterceptor.cs diff --git a/src/RestSharp/Interceptors/IInterceptor.cs b/src/RestSharp/Interceptors/IInterceptor.cs deleted file mode 100644 index 85f0095a4..000000000 --- a/src/RestSharp/Interceptors/IInterceptor.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace RestSharp.Interceptors { - - /// - /// Interceptor, which intercepts outgoing requests and incoming responses - /// - public interface IInterceptor: IBeforeSerializationInterceptor,IBeforeRequestInterceptor, IAfterRequestInterceptor, IBeforeDeserializeInterceptor { - - - } -} From a13ec647c3c2138da64519c0ce2dd50956bfc109 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Thu, 4 May 2023 18:32:25 +0200 Subject: [PATCH 13/27] refactor: Moved to Abstract Base Class --- .../{BaseInterceptor.cs => Interceptor.cs} | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) rename src/RestSharp/Interceptors/{BaseInterceptor.cs => Interceptor.cs} (56%) diff --git a/src/RestSharp/Interceptors/BaseInterceptor.cs b/src/RestSharp/Interceptors/Interceptor.cs similarity index 56% rename from src/RestSharp/Interceptors/BaseInterceptor.cs rename to src/RestSharp/Interceptors/Interceptor.cs index e631ea462..2d0c016be 100644 --- a/src/RestSharp/Interceptors/BaseInterceptor.cs +++ b/src/RestSharp/Interceptors/Interceptor.cs @@ -18,20 +18,40 @@ namespace RestSharp.Interceptors; /// /// Base Interceptor /// -public class BaseInterceptor : IInterceptor { +public abstract class Interceptor { + /// + /// Intercepts the request before serialization + /// + /// RestRequest before serialization + /// Value Tags public virtual ValueTask InterceptBeforeSerialization(RestRequest request) { return new(); } + /// + /// Intercepts the request before being sent + /// + /// HttpRequestMessage before being sent + /// Value Tags public virtual ValueTask InterceptBeforeRequest(HttpRequestMessage req) { return new(); } + /// + /// Intercepts the request before being sent + /// + /// HttpResponseMessage as received from Server + /// Value Tags public virtual ValueTask InterceptAfterRequest(HttpResponseMessage responseMessage) { return new(); } - public virtual ValueTask InterceptBeforeDeserialize(RestResponse request) { + /// + /// Intercepts the request before deserialization + /// + /// HttpResponseMessage as received from Server + /// Value Tags + public virtual ValueTask InterceptBeforeDeserialize(RestResponse response) { return new(); } } \ No newline at end of file From 7d26494394c41263080bb32d0d38a5723e699630 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Thu, 4 May 2023 18:33:33 +0200 Subject: [PATCH 14/27] refactor: Use Base class as Element --- src/RestSharp/Options/RestClientOptions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RestSharp/Options/RestClientOptions.cs b/src/RestSharp/Options/RestClientOptions.cs index bafd78825..95db15020 100644 --- a/src/RestSharp/Options/RestClientOptions.cs +++ b/src/RestSharp/Options/RestClientOptions.cs @@ -65,7 +65,7 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba /// public IAuthenticator? Authenticator { get; set; } - public List Interceptors { get; set; } = new(); + public List Interceptors { get; set; } = new(); /// /// Passed to Credentials property From a930f9829624dd77117367be8ccd02c5d577f230 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Thu, 4 May 2023 18:34:29 +0200 Subject: [PATCH 15/27] fix: Added UnitTest to Dependencies --- .../RestSharp.InteractiveTests.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj b/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj index c25f980cf..5cbf895be 100644 --- a/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj +++ b/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj @@ -8,4 +8,7 @@ + + + From adf131da154cfda16bbdfefcdf2fb7288673e467 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Thu, 4 May 2023 19:46:19 +0200 Subject: [PATCH 16/27] refactor: Adde nuget.config to gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 878dbb8d7..311c76314 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ -packages +packages/ +nuget.config + #ignore thumbnails created by windows Thumbs.db From 565f72ea800cfb3242fb68d7c03e16b61cf76615 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 28 Jun 2023 15:16:22 +0200 Subject: [PATCH 17/27] refactor: Moved OnBeforeDeserialization into async context one layer above --- src/RestSharp/RestClient.Extensions.cs | 12 ++++++++++++ src/RestSharp/Serializers/RestSerializers.cs | 12 +----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/RestSharp/RestClient.Extensions.cs b/src/RestSharp/RestClient.Extensions.cs index c03321170..7267258bd 100644 --- a/src/RestSharp/RestClient.Extensions.cs +++ b/src/RestSharp/RestClient.Extensions.cs @@ -103,8 +103,20 @@ public static async Task> ExecuteAsync( CancellationToken cancellationToken = default ) { var response = await client.ExecuteAsync(request, cancellationToken).ConfigureAwait(false); + await OnBeforeDeserialization(response, client.Options).ConfigureAwait(false); return client.Serializers.Deserialize(request, response, client.Options); } + + /// + /// Will be called before the Data will be serialized + /// + /// RestResponse with Data still in Content + /// RestClient options but readonly + static async Task OnBeforeDeserialization(RestResponse raw, ReadOnlyRestClientOptions options) { + foreach (var interceptor in options.Interceptors) { + await interceptor.InterceptBeforeDeserialize(raw); + } + } /// /// Executes the request asynchronously, authenticating if needed diff --git a/src/RestSharp/Serializers/RestSerializers.cs b/src/RestSharp/Serializers/RestSerializers.cs index 425cdbd62..0133db6cf 100644 --- a/src/RestSharp/Serializers/RestSerializers.cs +++ b/src/RestSharp/Serializers/RestSerializers.cs @@ -39,7 +39,6 @@ internal RestResponse Deserialize(RestRequest request, RestResponse raw, R try { request.OnBeforeDeserialization?.Invoke(raw); - OnBeforeDeserialization(raw, options).ConfigureAwait(true); response.Data = DeserializeContent(raw); } catch (Exception ex) { @@ -54,16 +53,7 @@ internal RestResponse Deserialize(RestRequest request, RestResponse raw, R return response; } - /// - /// Will be called before the Data will be serialized - /// - /// RestResponse with Data still in Content - /// RestClient options but readonly - async Task OnBeforeDeserialization(RestResponse raw, ReadOnlyRestClientOptions options) { - foreach (var interceptor in options.Interceptors) { - await interceptor.InterceptBeforeDeserialize(raw); //.ThrowExceptionIfAvailable(); - } - } + /// /// Deserialize the response content into the specified type From f5616cd689b0893fa63f9ed461fbb032008783b3 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 28 Jun 2023 15:19:18 +0200 Subject: [PATCH 18/27] refactor: Moved Interceptor Calls and surrounding Code out of try,catch to propagate Exceptions to Callers --- src/RestSharp/RestClient.Async.cs | 42 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/RestSharp/RestClient.Async.cs b/src/RestSharp/RestClient.Async.cs index 6fd390810..953a7af16 100644 --- a/src/RestSharp/RestClient.Async.cs +++ b/src/RestSharp/RestClient.Async.cs @@ -95,23 +95,24 @@ async Task ExecuteRequestAsync(RestRequest request, CancellationTo var ct = cts.Token; + + HttpResponseMessage? responseMessage; + // Make sure we have a cookie container if not provided in the request + CookieContainer cookieContainer = request.CookieContainer ??= new CookieContainer(); + + var headers = new RequestHeaders() + .AddHeaders(request.Parameters) + .AddHeaders(DefaultParameters) + .AddAcceptHeader(AcceptedContentTypes) + .AddCookieHeaders(url, cookieContainer) + .AddCookieHeaders(url, Options.CookieContainer); + + message.AddHeaders(headers); + if (request.OnBeforeRequest != null) await request.OnBeforeRequest(message).ConfigureAwait(false); + await OnBeforeRequest(message); + try { - // Make sure we have a cookie container if not provided in the request - var cookieContainer = request.CookieContainer ??= new CookieContainer(); - - var headers = new RequestHeaders() - .AddHeaders(request.Parameters) - .AddHeaders(DefaultParameters) - .AddAcceptHeader(AcceptedContentTypes) - .AddCookieHeaders(url, cookieContainer) - .AddCookieHeaders(url, Options.CookieContainer); - - message.AddHeaders(headers); - if (request.OnBeforeRequest != null) await request.OnBeforeRequest(message).ConfigureAwait(false); - await OnBeforeRequest(message); - - var responseMessage = await HttpClient.SendAsync(message, request.CompletionOption, ct).ConfigureAwait(false); - + responseMessage = await HttpClient.SendAsync(message, request.CompletionOption, ct).ConfigureAwait(false); // Parse all the cookies from the response and update the cookie jar with cookies if (responseMessage.Headers.TryGetValues(KnownHeaders.SetCookie, out var cookiesHeader)) { // ReSharper disable once PossibleMultipleEnumeration @@ -119,15 +120,14 @@ async Task ExecuteRequestAsync(RestRequest request, CancellationTo // ReSharper disable once PossibleMultipleEnumeration Options.CookieContainer?.AddCookies(url, cookiesHeader); } - - if (request.OnAfterRequest != null) await request.OnAfterRequest(responseMessage).ConfigureAwait(false); - await OnAfterRequest(responseMessage); - - return new HttpResponse(responseMessage, url, cookieContainer, null, timeoutCts.Token); } catch (Exception ex) { return new HttpResponse(null, url, null, ex, timeoutCts.Token); } + if (request.OnAfterRequest != null) await request.OnAfterRequest(responseMessage).ConfigureAwait(false); + await OnAfterRequest(responseMessage); + return new HttpResponse(responseMessage, url, cookieContainer, null, timeoutCts.Token); + } /// From c26b6a492be040f5d200c737a2af4dbdd7033da9 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 28 Jun 2023 15:20:04 +0200 Subject: [PATCH 19/27] refactor: Added Test for Interceptor --- .../Interceptor/InterceptorTests.cs | 127 ++++++++++++++++++ .../RestSharp.Tests.Integrated.csproj | 1 + 2 files changed, 128 insertions(+) create mode 100644 test/RestSharp.Tests.Integrated/Interceptor/InterceptorTests.cs diff --git a/test/RestSharp.Tests.Integrated/Interceptor/InterceptorTests.cs b/test/RestSharp.Tests.Integrated/Interceptor/InterceptorTests.cs new file mode 100644 index 000000000..3357f5204 --- /dev/null +++ b/test/RestSharp.Tests.Integrated/Interceptor/InterceptorTests.cs @@ -0,0 +1,127 @@ +// Copyright (c) .NET Foundation and Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using Moq; +using RestSharp.Tests.Integrated.Server; + +namespace RestSharp.Tests.Integrated.Interceptor; + +[Collection(nameof(TestServerCollection))] +public class InterceptorTests { + readonly RestClient _client; + + public InterceptorTests(TestServerFixture fixture) => _client = new RestClient(fixture.Server.Url); + + [Fact] + public async Task AddInterceptor_ShouldBeUsed() { + //Arrange + var body = new TestRequest("foo", 100); + var request = new RestRequest("post/json").AddJsonBody(body); + + var mockInterceptor = new Mock(); + var interceptor = mockInterceptor.Object; + var options = _client.Options; + options.Interceptors.Add(interceptor); + //Act + var response = await _client.ExecutePostAsync(request); + //Assert + mockInterceptor.Verify(m => m.InterceptBeforeSerialization(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptBeforeRequest(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptAfterRequest(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptBeforeDeserialize(It.IsAny())); + } + [Fact] + public async Task ThrowExceptionIn_InterceptBeforeSerialization_ShouldBeCatchedInTest() { + //Arrange + var body = new TestRequest("foo", 100); + var request = new RestRequest("post/json").AddJsonBody(body); + + var mockInterceptor = new Mock(); + mockInterceptor.Setup(m => m.InterceptBeforeSerialization(It.IsAny())).Throws(() => throw new Exception("DummyException")); + var interceptor = mockInterceptor.Object; + var options = _client.Options; + options.Interceptors.Add(interceptor); + //Act + var action = () => _client.ExecutePostAsync(request); + //Assert + await action.Should().ThrowAsync().WithMessage("DummyException"); + mockInterceptor.Verify(m => m.InterceptBeforeSerialization(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptBeforeRequest(It.IsAny()),Times.Never); + mockInterceptor.Verify(m => m.InterceptAfterRequest(It.IsAny()),Times.Never); + mockInterceptor.Verify(m => m.InterceptBeforeDeserialize(It.IsAny()),Times.Never); + } + [Fact] + public async Task ThrowExceptionIn_InterceptBeforeRequest_ShouldBeCatchableInTest() { + //Arrange + var body = new TestRequest("foo", 100); + var request = new RestRequest("post/json").AddJsonBody(body); + + var mockInterceptor = new Mock(); + mockInterceptor.Setup(m => m.InterceptBeforeRequest(It.IsAny())).Throws(() => throw new Exception("DummyException")); + var interceptor = mockInterceptor.Object; + var options = _client.Options; + options.Interceptors.Add(interceptor); + //Act + var action = () => _client.ExecutePostAsync(request); + //Assert + await action.Should().ThrowAsync().WithMessage("DummyException"); + mockInterceptor.Verify(m => m.InterceptBeforeSerialization(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptBeforeRequest(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptAfterRequest(It.IsAny()),Times.Never); + mockInterceptor.Verify(m => m.InterceptBeforeDeserialize(It.IsAny()),Times.Never); + } + [Fact] + public async Task ThrowExceptionIn_InterceptAfterRequest_ShouldBeCatchableInTest() { + //Arrange + var body = new TestRequest("foo", 100); + var request = new RestRequest("post/json").AddJsonBody(body); + + var mockInterceptor = new Mock(); + mockInterceptor.Setup(m => m.InterceptAfterRequest(It.IsAny())).Throws(() => throw new Exception("DummyException")); + var interceptor = mockInterceptor.Object; + var options = _client.Options; + options.Interceptors.Add(interceptor); + //Act + var action = () => _client.ExecutePostAsync(request); + //Assert + await action.Should().ThrowAsync().WithMessage("DummyException"); + mockInterceptor.Verify(m => m.InterceptBeforeSerialization(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptBeforeRequest(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptAfterRequest(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptBeforeDeserialize(It.IsAny()),Times.Never); + } + [Fact] + public async Task ThrowException_InInterceptBeforeDeserialize_ShouldBeCatchableInTest() { + //Arrange + var body = new TestRequest("foo", 100); + var request = new RestRequest("post/json").AddJsonBody(body); + + var mockInterceptor = new Mock(); + mockInterceptor.Setup(m => m.InterceptBeforeDeserialize(It.IsAny())).Throws(() => throw new Exception("DummyException")); + var interceptor = mockInterceptor.Object; + var options = _client.Options; + options.Interceptors.Add(interceptor); + //Act + var action = () => _client.PostAsync(request); + //Assert + await action.Should().ThrowAsync().WithMessage("DummyException"); + mockInterceptor.Verify(m => m.InterceptBeforeSerialization(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptBeforeRequest(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptAfterRequest(It.IsAny())); + mockInterceptor.Verify(m => m.InterceptBeforeDeserialize(It.IsAny())); + } + + +} \ No newline at end of file diff --git a/test/RestSharp.Tests.Integrated/RestSharp.Tests.Integrated.csproj b/test/RestSharp.Tests.Integrated/RestSharp.Tests.Integrated.csproj index 6cf43fa35..480c9b8ff 100644 --- a/test/RestSharp.Tests.Integrated/RestSharp.Tests.Integrated.csproj +++ b/test/RestSharp.Tests.Integrated/RestSharp.Tests.Integrated.csproj @@ -17,6 +17,7 @@ + From 36e688f77f3c3896cf9af6670dc3bb34e62ce166 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 28 Jun 2023 15:21:13 +0200 Subject: [PATCH 20/27] refactor: Adde global.json to .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 311c76314..6cfe2d087 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,5 @@ RestSharp.IntegrationTests/config.json /out/ /docs/.vuepress/dist/ .vscode/ -.temp/ \ No newline at end of file +.temp/ +global.json \ No newline at end of file From 25b2fb4e03e3919010bf2397ccc5c2551770e9ea Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 28 Jun 2023 15:36:06 +0200 Subject: [PATCH 21/27] refactor: ConfigureAwait(false) not needed to return to origin thread --- src/RestSharp/RestClient.Async.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/RestSharp/RestClient.Async.cs b/src/RestSharp/RestClient.Async.cs index 953a7af16..c8897520e 100644 --- a/src/RestSharp/RestClient.Async.cs +++ b/src/RestSharp/RestClient.Async.cs @@ -76,7 +76,8 @@ async Task ExecuteRequestAsync(RestRequest request, CancellationTo if (_disposed) { throw new ObjectDisposedException(nameof(RestClient)); } - await OnBeforeSerialization(request); + + await OnBeforeSerialization(request).ConfigureAwait(false); request.ValidateParameters(); var authenticator = request.Authenticator ?? Options.Authenticator; if (authenticator != null) await authenticator.Authenticate(this, request).ConfigureAwait(false); @@ -109,7 +110,7 @@ async Task ExecuteRequestAsync(RestRequest request, CancellationTo message.AddHeaders(headers); if (request.OnBeforeRequest != null) await request.OnBeforeRequest(message).ConfigureAwait(false); - await OnBeforeRequest(message); + await OnBeforeRequest(message).ConfigureAwait(false); try { responseMessage = await HttpClient.SendAsync(message, request.CompletionOption, ct).ConfigureAwait(false); @@ -125,7 +126,7 @@ async Task ExecuteRequestAsync(RestRequest request, CancellationTo return new HttpResponse(null, url, null, ex, timeoutCts.Token); } if (request.OnAfterRequest != null) await request.OnAfterRequest(responseMessage).ConfigureAwait(false); - await OnAfterRequest(responseMessage); + await OnAfterRequest(responseMessage).ConfigureAwait(false); return new HttpResponse(responseMessage, url, cookieContainer, null, timeoutCts.Token); } From 6f39867fdb71f280879d53f26072a15e9cbbeab8 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 19 Jul 2023 10:58:40 +0200 Subject: [PATCH 22/27] refactor: Remove global.json from gitignore --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6cfe2d087..311c76314 100644 --- a/.gitignore +++ b/.gitignore @@ -55,5 +55,4 @@ RestSharp.IntegrationTests/config.json /out/ /docs/.vuepress/dist/ .vscode/ -.temp/ -global.json \ No newline at end of file +.temp/ \ No newline at end of file From 721a9b079fca3738325cc8389355d86146dcd5f1 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 19 Jul 2023 11:00:20 +0200 Subject: [PATCH 23/27] refactor: Changed back Project guid --- RestSharp.sln | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RestSharp.sln b/RestSharp.sln index 2902ccf04..711c5536c 100644 --- a/RestSharp.sln +++ b/RestSharp.sln @@ -35,7 +35,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RestSharp.Tests.Serializers EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SourceGen", "SourceGen", "{55B8F371-B2BA-4DEE-AB98-5BAB8A21B1C2}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGenerator", "gen\SourceGenerator\SourceGenerator.csproj", "{FE778406-ADCF-45A1-B775-A054B55BFC50}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGenerator", "gen\SourceGenerator\SourceGenerator.csproj", "{FE778406-ADCF-45A1-B775-A054B55BFC50}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution From 08f9be4a412d73efeb8d10a8cdce34e8e08e02db Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 19 Jul 2023 11:02:52 +0200 Subject: [PATCH 24/27] refactor: Removed System.Threading.Tasks depencency from RestSharp.csproj --- src/RestSharp/RestSharp.csproj | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/RestSharp/RestSharp.csproj b/src/RestSharp/RestSharp.csproj index c8b3cbcec..1d9e0d915 100644 --- a/src/RestSharp/RestSharp.csproj +++ b/src/RestSharp/RestSharp.csproj @@ -39,7 +39,4 @@ - - - From ac79e2ba9c800a9f07d87f27e203e12b2716e132 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 19 Jul 2023 11:03:34 +0200 Subject: [PATCH 25/27] refactor: Removed xunig.assert depencency from RestSharp.InteractiveTests.csproj --- .../RestSharp.InteractiveTests.csproj | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj b/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj index 5cbf895be..c25f980cf 100644 --- a/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj +++ b/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj @@ -8,7 +8,4 @@ - - - From 4ad71a4657243433d5c53f545c464da2dd7e6376 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 19 Jul 2023 11:04:17 +0200 Subject: [PATCH 26/27] refactor: Readded global.json --- global.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 global.json diff --git a/global.json b/global.json new file mode 100644 index 000000000..36e1a9e95 --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "7.0.0", + "rollForward": "latestMajor", + "allowPrerelease": false + } +} \ No newline at end of file From 901e5c3d9b9dd57f6638fd72cd72d1d19237a293 Mon Sep 17 00:00:00 2001 From: Franz Reinhard Seidl Date: Wed, 19 Jul 2023 11:08:03 +0200 Subject: [PATCH 27/27] refactor: Undo renaming class user to User --- .../SampleClasses/twitter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/RestSharp.Tests.Serializers.Xml/SampleClasses/twitter.cs b/test/RestSharp.Tests.Serializers.Xml/SampleClasses/twitter.cs index bf0d94e63..df64ad212 100644 --- a/test/RestSharp.Tests.Serializers.Xml/SampleClasses/twitter.cs +++ b/test/RestSharp.Tests.Serializers.Xml/SampleClasses/twitter.cs @@ -23,7 +23,7 @@ public class status { public string in_reply_to_screen_name { get; set; } // ignore contributors for now - public User user { get; set; } + public user user { get; set; } // ignore geo public long id { get; set; } @@ -31,7 +31,7 @@ public class status { public string text { get; set; } } -public class User { +public class user { public string url { get; set; } public string description { get; set; } @@ -105,7 +105,7 @@ public class complexStatus { public string in_reply_to_screen_name { get; set; } // ignore contributors for now - [DeserializeAs(Name = "User.following")] + [DeserializeAs(Name = "user.following")] public bool follow { get; set; } // ignore geo