Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ef1dddd
feature: Added Implementation for Interceptors
fseidl-bauradar Apr 4, 2023
b8d7ec8
feature: Added Interfaces for Interceptors
fseidl-bauradar Apr 4, 2023
38450ff
Merge branch 'restsharp:dev' into dev
fseidl-bauradar Apr 4, 2023
25f5d65
fix: Corrected Comment
fseidl-bauradar Apr 4, 2023
85b6c79
fix: Configured to wait for result
fseidl-bauradar Apr 4, 2023
9d7b59f
refactor: Renamed Class name to go allong with Class Name conventions
fseidl-bauradar Apr 4, 2023
86772f5
Update RestSharp.sln
fseidl-bauradar Apr 4, 2023
651dabc
Merge branch 'restsharp:dev' into dev
fseidl-bauradar Apr 4, 2023
5c91edd
Removed ConsoleTest from Repository
fseidl-bauradar Apr 4, 2023
b9f4082
Feature: Added Base Interceptor to simplify definition of Interceptors
fseidl-bauradar Apr 4, 2023
b7ed190
Merge branch 'restsharp:dev' into dev
fseidl-bauradar Apr 4, 2023
fa2662e
Fix: Added virtual keyword to allow overriding the functions in deriv…
fseidl-bauradar Apr 4, 2023
6241787
Synchronized Fork with RestSharp Repository
fseidl-bauradar Apr 19, 2023
1cd427a
Solved Merge conflict
fseidl-bauradar Apr 19, 2023
afb6ff3
refactor: Removed Interfaces
fseidl-bauradar May 4, 2023
56086d6
refactor: Removed Interfaces
fseidl-bauradar May 4, 2023
a13ec64
refactor: Moved to Abstract Base Class
fseidl-bauradar May 4, 2023
7d26494
refactor: Use Base class as Element
fseidl-bauradar May 4, 2023
a930f98
fix: Added UnitTest to Dependencies
fseidl-bauradar May 4, 2023
9312863
sychronized with upstream
fseidl-bauradar May 4, 2023
adf131d
refactor: Adde nuget.config to gitignore
fseidl-bauradar May 4, 2023
b31aee4
Merge branch 'restsharp:dev' into dev
fseidl-bauradar May 12, 2023
7cec99f
Merge branch 'restsharp:dev' into dev
fseidl-bauradar Jun 19, 2023
565f72e
refactor: Moved OnBeforeDeserialization into async context one layer …
fseidl-bauradar Jun 28, 2023
f5616cd
refactor: Moved Interceptor Calls and surrounding Code out of try,cat…
fseidl-bauradar Jun 28, 2023
c26b6a4
refactor: Added Test for Interceptor
fseidl-bauradar Jun 28, 2023
36e688f
refactor: Adde global.json to .gitignore
fseidl-bauradar Jun 28, 2023
25b2fb4
refactor: ConfigureAwait(false) not needed to return to origin thread
fseidl-bauradar Jun 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feature: Added Implementation for Interceptors
  • Loading branch information
fseidl-bauradar committed Apr 4, 2023
commit ef1dddd229fede9123592af0ec71ab959bc30bf9
2 changes: 1 addition & 1 deletion gen/SourceGenerator/SourceGenerator.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand Down
3 changes: 3 additions & 0 deletions src/RestSharp/Options/RestClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Text;
using RestSharp.Authenticators;
using RestSharp.Extensions;
using RestSharp.Interceptors;

namespace RestSharp;

Expand Down Expand Up @@ -59,6 +60,8 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
/// </summary>
public IAuthenticator? Authenticator { get; set; }

public List<IInterceptor> Interceptors { get; set; } = new();

/// <summary>
/// Passed to <see cref="HttpMessageHandler"/> <code>Credentials</code> property
/// </summary>
Expand Down
33 changes: 32 additions & 1 deletion src/RestSharp/RestClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ async Task<HttpResponse> 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;
Expand All @@ -112,8 +114,8 @@ async Task<HttpResponse> 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);

Expand All @@ -130,6 +132,7 @@ async Task<HttpResponse> 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);
}
Expand All @@ -138,6 +141,34 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
}
}

/// <summary>
/// Will be called before the Request becomes Serialized
/// </summary>
/// <param name="request">RestRequest before it will be serialized</param>
async Task OnBeforeSerialization(RestRequest request) {
foreach (var interceptor in Options.Interceptors) {
await interceptor.InterceptBeforeSerialization(request); //.ThrowExceptionIfAvailable();
}
}
/// <summary>
/// Will be calld before the Request will be sent
/// </summary>
/// <param name="requestMessage">HttpRequestMessage ready to be sent</param>
async Task OnBeforeRequest(HttpRequestMessage requestMessage) {
foreach (var interceptor in Options.Interceptors) {
await interceptor.InterceptBeforeRequest(requestMessage);
}
}
/// <summary>
/// Will be called after the Response has been received from Server
/// </summary>
/// <param name="responseMessage">HttpResponseMessage as received from server</param>
async Task OnAfterRequest(HttpResponseMessage responseMessage) {
foreach (var interceptor in Options.Interceptors) {
await interceptor.InterceptAfterRequest(responseMessage);
}
}

record HttpResponse(
HttpResponseMessage? ResponseMessage,
Uri Url,
Expand Down
1 change: 0 additions & 1 deletion src/RestSharp/RestClient.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

using System.Runtime.CompilerServices;
using RestSharp.Extensions;
using RestSharp.Serializers;

namespace RestSharp;

Expand Down
3 changes: 3 additions & 0 deletions src/RestSharp/RestSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@
<ItemGroup>
<ProjectReference Include="..\..\gen\SourceGenerator\SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Threading.Tasks" Version="4.3.0" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions src/RestSharp/Serializers/RestSerializers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal RestResponse<T> Deserialize<T>(RestRequest request, RestResponse raw, R

try {
request.OnBeforeDeserialization?.Invoke(raw);
OnBeforeDeserialization(raw, options);
response.Data = DeserializeContent<T>(raw);
}
catch (Exception ex) {
Expand All @@ -51,6 +52,15 @@ internal RestResponse<T> Deserialize<T>(RestRequest request, RestResponse raw, R

return response;
}
/// <summary>
/// Will be called before the Data will be serialized
/// </summary>
/// <param name="raw">RestResponse with Data still in Content</param>
async Task OnBeforeDeserialization(RestResponse raw, ReadOnlyRestClientOptions options) {
foreach (var interceptor in options.Interceptors) {
await interceptor.InterceptBeforeDeserialize(raw); //.ThrowExceptionIfAvailable();
}
}

/// <summary>
/// Deserialize the response content into the specified type
Expand Down