auto reset tokens on 429 part 2

This commit is contained in:
Vincent Cloutier 2023-03-03 12:54:19 -05:00
parent 219841e016
commit 425beb13ad
3 changed files with 17 additions and 9 deletions

View file

@ -59,7 +59,10 @@ namespace BirdsiteLive.Twitter.Tools
_twitterClients.RemoveAt(i);
_tokens.RemoveAt(i);
}
catch (IndexOutOfRangeException _) {}
catch (IndexOutOfRangeException _)
{
_logger.LogError("Error refreshing twitter token");
}
await RefreshCred();
}

View file

@ -115,6 +115,7 @@ namespace BirdsiteLive.Twitter
}
catch (HttpRequestException e)
{
_logger.LogError(e, "Error retrieving timeline of {Username}; refreshing client", username);
await _twitterAuthenticationInitializer.RefreshClient(request);
return null;
}

View file

@ -39,18 +39,16 @@ namespace BirdsiteLive.Twitter
{
JsonDocument res;
var client = await _twitterAuthenticationInitializer.MakeHttpClient();
using var request = _twitterAuthenticationInitializer.MakeHttpRequest(new HttpMethod("GET"), endpoint.Replace("elonmusk", username));
try
{
var client = await _twitterAuthenticationInitializer.MakeHttpClient();
using (var request = _twitterAuthenticationInitializer.MakeHttpRequest(new HttpMethod("GET"), endpoint.Replace("elonmusk", username)))
{
var httpResponse = await client.SendAsync(request);
httpResponse.EnsureSuccessStatusCode();
var httpResponse = await client.SendAsync(request);
httpResponse.EnsureSuccessStatusCode();
var c = await httpResponse.Content.ReadAsStringAsync();
res = JsonDocument.Parse(c);
}
var c = await httpResponse.Content.ReadAsStringAsync();
res = JsonDocument.Parse(c);
var result = res.RootElement.GetProperty("data").GetProperty("user").GetProperty("result");
return Extract(result);
}
@ -70,6 +68,12 @@ namespace BirdsiteLive.Twitter
// throw;
//}
}
catch (HttpRequestException e)
{
_logger.LogError(e, "Error retrieving user {Username}, Refreshing client", username);
await _twitterAuthenticationInitializer.RefreshClient(request);
return null;
}
catch (Exception e)
{
_logger.LogError(e, "Error retrieving user {Username}", username);