Skip to content
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

Closed
wants to merge 0 commits into from

Conversation

fseidl-bauradar
Copy link
Contributor

@fseidl-bauradar fseidl-bauradar commented Jun 28, 2023

Description

Added Possibility to add Client wide Interceptors, to check Request and Responses on multiple positions

Purpose

This pull request is a:

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@fseidl-bauradar
Copy link
Contributor Author

Added because of changes #2076

.gitignore Outdated
.temp/
global.json
Copy link
Member

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.

Copy link
Contributor Author

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}"
Copy link
Member

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.

Copy link
Contributor Author

@fseidl-bauradar fseidl-bauradar Jul 18, 2023

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();
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

@fseidl-bauradar fseidl-bauradar Jul 18, 2023

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?

Copy link
Contributor Author

@fseidl-bauradar fseidl-bauradar Jul 18, 2023

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.

Copy link
Contributor Author

@fseidl-bauradar fseidl-bauradar Jul 19, 2023

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);
			}
	}
}

Copy link
Contributor Author

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.

Copy link
Contributor Author

@fseidl-bauradar fseidl-bauradar Jul 19, 2023

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;
		} 
	}
}

Copy link
Contributor Author

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?

Copy link
Member

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.


var responseMessage = await HttpClient.SendAsync(message, request.CompletionOption, ct).ConfigureAwait(false);

responseMessage = await HttpClient.SendAsync(message, request.CompletionOption, ct).ConfigureAwait(false);
Copy link
Member

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?

Copy link
Contributor Author

@fseidl-bauradar fseidl-bauradar Jul 18, 2023

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.

@@ -39,4 +39,7 @@
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\gen\SourceGenerator\SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup>
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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" />
Copy link
Member

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?

Copy link
Contributor Author

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")]
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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

@alexeyzimarev
Copy link
Member

@fseidl-bauradar I am ready to merge this if you find time to address my comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants