Reputation: 175
I migrate to the new version of RestSharp (v107) and in the documentation says that this version uses HttpClient and must reuse the same instance for multiples requests.
On the other hand, IAuthenticator interface implements the "Authenticate" method with the RestClient and RestRequest as parameters.
RestRequest is one per request and is OK, but RestClient is shared with multiples other request running at same time.
I only use the RestClient object for calling the .BuildUri(RestRequest) method to get the Uri, but my HttpClient single instance is used and shared for different hosts requests, so that is not a problem when concurrency occur?
Also, a side question.. The new IAuthenticator "Authenticate" method implementation returns ValueTask. What should I return? The old one return void.
Upvotes: 1
Views: 2782
Reputation: 19630
The Authenticate
method supposes to add the necessary authentication headers to the RestRequest
instance. It doesn't change anything inside the provided RestClient
instance, so it's totally thread-safe.
The reason for Authenticate
to return ValueTask
as an authenticator can do an async call to the authentication API. As an example, I described how you can build a Twitter authenticator, which makes an async call to Twitter API to get the bearer token. It only does it once, so all the consequent calls will use the token retrieved by the first call.
There's a discussion to add Authenticator
as a request property as well, if you have a multi-tenant environment, and need to get a token per request. It's now work in progress.
Also, you don't need to use RestClient
and HttpClient
on the outside. RestClient
instantiates an HttpClient
instance anyway. You can provide your own HttpClient
instance to the RestClient
constructor if you wish.
If you only need a URL builder, you might get better off with Flurl.
Upvotes: 2