-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Option to add Interceptors on Client Level #2108
Conversation
Added because of changes #2076 |
.gitignore
Outdated
.temp/ | ||
global.json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
global.json
should not be ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will keep this in mind. I thought that it would be a IDE config file
RestSharp.sln
Outdated
@@ -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("{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}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes to this file aren't required. Also, adding Appveyor-specific build targets need to be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like my ide is doing this by itself, I will try to keep this out from git in the future
@@ -64,6 +65,8 @@ public class RestClientOptions { | |||
/// </summary> | |||
public IAuthenticator? Authenticator { get; set; } | |||
|
|||
public List<Interceptor> Interceptors { get; set; } = new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should not be a list as lists are mutable, and the list of interceptors being mutable will make the client not thread safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that's a point I didn't had in mind, is there a way to allow adding a interceptor after creating a inititing the client.
Currently I am using a Interceptor to refresh the jwt Token if it's outdated. For this I need a RestClient to access the backend over a Service.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can convert it to array or IReadOnlyCollection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But doesn't mean this that it is impossible to add an interceptor after initiating the client?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I checked the Microsoft Reference, as I understand now you can not change the list directly, but you can change the list that is wrapped by ReadOnlyCollection.
But how do we get the wrapped List? Should we add a Methode for adding a interceptor.
Or is there a better possibility doing this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As fas as I see there are two ways to solve this.
class RestClientOptions {
private ImmutableList<InterceptorBase> _interceptors;
public void AddInterceptor(InterceptorBase interceptor){
object mutex = new object();
lock(mutex) {
if(_interceptors.Contains) return;
_interceptors = _interceptors.Add(interceptor);
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I see there should be a solutiong for ensuring that the AuthenticationInterceptor is the first in the list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Therefore I would suggest provide an option to define the Authenticator, after the client have been created.
class RestClientOptions {
public void SetAuthenticator(IAutenticator authenticator){
object mutex = new object;
lock(mutex){
Authenticator = authenticator;
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you overtake functions defined in the RestClientOptions into the RestClientReadonlyOptions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure adding mutation functions to the options class is a great idea. It's just a property bag, and the current source generator treats it like that. But I get your point. Let me think about for a bit.
src/RestSharp/RestClient.Async.cs
Outdated
|
||
var responseMessage = await HttpClient.SendAsync(message, request.CompletionOption, ct).ConfigureAwait(false); | ||
|
||
responseMessage = await HttpClient.SendAsync(message, request.CompletionOption, ct).ConfigureAwait(false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the reason to move the declaration of responseMessage
to line 100?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason, was that I handle the response inside the interceptor (line 123) and prevent the tryCatch-Block from, catching exceptions thrown inside the interceptor.
For example I use an interceptor, to preprocess ErrorMessage to convert them back into Exceptions.
src/RestSharp/RestSharp.csproj
Outdated
@@ -39,4 +39,7 @@ | |||
<ItemGroup> | |||
<ProjectReference Include="$(RepoRoot)\gen\SourceGenerator\SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> | |||
</ItemGroup> | |||
<ItemGroup> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't sound right, adding this package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, we can remove it, seems like I added it by accident, is still building after removing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you commit the change, I'll mark this as resolved. Then I will merge your PR and figure out what to do with mutability.
@@ -8,4 +8,7 @@ | |||
<ProjectReference Include="$(RepoRoot)\src\RestSharp\RestSharp.csproj" /> | |||
<ProjectReference Include="..\RestSharp.Tests.Shared\RestSharp.Tests.Shared.csproj" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<PackageReference Include="xunit.assert" Version="2.4.2" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This project doesn't change, why did you add a reference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think this happed during checking, why the failing tests not working, wanted to find the reason for this.
@@ -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")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's wrong as the message format is camel case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This, was just a thing I found, I just changed the name of the User-Class to starting UpperCase, according to the C# Convention for Classes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I get it. Have the same urge to fix things sometimes. But, it is better to keep the PR scope to its purpose as things might get break unexpectedly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can let it unchanged if you want, it's not as important for me
@fseidl-bauradar I am ready to merge this if you find time to address my comments. |
901e5c3
to
6197f31
Compare
Description
Added Possibility to add Client wide Interceptors, to check Request and Responses on multiple positions
Purpose
This pull request is a:
Checklist