work on HttpClientFactory in twitter client part 2

This commit is contained in:
Vincent Cloutier 2023-02-10 16:45:13 -05:00
parent 210b820e90
commit fec1aa1977
3 changed files with 17 additions and 6 deletions

View file

@ -14,6 +14,7 @@ namespace BirdsiteLive.Twitter.Tools
{
Task EnsureAuthenticationIsInitialized();
Task<HttpClient> MakeHttpClient();
HttpRequestMessage MakeHttpRequest(HttpMethod m, string endpoint);
}
public class TwitterAuthenticationInitializer : ITwitterAuthenticationInitializer
@ -54,8 +55,8 @@ namespace BirdsiteLive.Twitter.Tools
{
(string bearer, string guest) = await GetCred();
// HttpClient client = _httpClientFactory.CreateClient();
HttpClient client = new HttpClient();
HttpClient client = _httpClientFactory.CreateClient();
//HttpClient client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Bearer " + bearer);
client.DefaultRequestHeaders.TryAddWithoutValidation("x-guest-token", guest);
client.DefaultRequestHeaders.TryAddWithoutValidation("Referer", "https://twitter.com/");
@ -115,5 +116,16 @@ namespace BirdsiteLive.Twitter.Tools
int r = rnd.Next(_twitterClients.Count);
return _twitterClients[r];
}
public HttpRequestMessage MakeHttpRequest(HttpMethod m, string endpoint)
{
var client = _httpClientFactory.CreateClient();
var request = new HttpRequestMessage(m, endpoint);
int r = rnd.Next(_twitterClients.Count);
request.Headers.TryAddWithoutValidation("Authorization", $"Bearer " + BearerToken);
request.Headers.TryAddWithoutValidation("x-guest-token", _tokens[r]);
request.Headers.TryAddWithoutValidation("Referer", "https://twitter.com/");
request.Headers.TryAddWithoutValidation("x-twitter-active-user", "yes");
return request;
}
}
}

View file

@ -48,7 +48,6 @@ namespace BirdsiteLive.Twitter
public async Task<ExtractedTweet> GetTweetAsync(long statusId)
{
var client = await _twitterAuthenticationInitializer.MakeHttpClient();
@ -58,7 +57,7 @@ namespace BirdsiteLive.Twitter
try
{
JsonDocument tweet;
using (var request = new HttpRequestMessage(new HttpMethod("GET"), reqURL))
using (var request = _twitterAuthenticationInitializer.MakeHttpRequest(new HttpMethod("GET"), reqURL))
{
var httpResponse = await client.SendAsync(request);
httpResponse.EnsureSuccessStatusCode();
@ -105,7 +104,7 @@ namespace BirdsiteLive.Twitter
List<ExtractedTweet> extractedTweets = new List<ExtractedTweet>();
try
{
using (var request = new HttpRequestMessage(new HttpMethod("GET"), reqURL))
using (var request = _twitterAuthenticationInitializer.MakeHttpRequest(new HttpMethod("GET"), reqURL))
{
var httpResponse = await client.SendAsync(request);

View file

@ -43,7 +43,7 @@ namespace BirdsiteLive.Twitter
{
var client = await _twitterAuthenticationInitializer.MakeHttpClient();
using (var request = new HttpRequestMessage(new HttpMethod("GET"), endpoint.Replace("elonmusk", username)))
using (var request = _twitterAuthenticationInitializer.MakeHttpRequest(new HttpMethod("GET"), endpoint.Replace("elonmusk", username)))
{
var httpResponse = await client.SendAsync(request);
httpResponse.EnsureSuccessStatusCode();