mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-02-23 17:54:19 +00:00
Refactor
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using Superpower;
|
||||
using Superpower.Parsers;
|
||||
|
||||
namespace DiscordChatExporter.Core.Utils.Extensions
|
||||
{
|
||||
public static class SuperpowerExtensions
|
||||
{
|
||||
public static TextParser<string> Text(this TextParser<char[]> parser) =>
|
||||
parser.Select(chars => new string(chars));
|
||||
|
||||
public static TextParser<T> Token<T>(this TextParser<T> parser) =>
|
||||
parser.Between(Character.WhiteSpace.IgnoreMany(), Character.WhiteSpace.IgnoreMany());
|
||||
|
||||
// From: https://twitter.com/nblumhardt/status/1389349059786264578
|
||||
public static TextParser<T> Log<T>(this TextParser<T> parser, string description) => i =>
|
||||
{
|
||||
Console.WriteLine($"Trying {description} ->");
|
||||
var r = parser(i);
|
||||
Console.WriteLine($"Result was {r}");
|
||||
return r;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -21,14 +21,15 @@ namespace DiscordChatExporter.Core.Utils
|
||||
.OrResult<HttpResponseMessage>(m => m.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
.OrResult(m => m.StatusCode == HttpStatusCode.RequestTimeout)
|
||||
.OrResult(m => m.StatusCode >= HttpStatusCode.InternalServerError)
|
||||
.WaitAndRetryAsync(8,
|
||||
.WaitAndRetryAsync(
|
||||
8,
|
||||
(i, result, _) =>
|
||||
{
|
||||
// If rate-limited, use retry-after as a guide
|
||||
if (result.Result?.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
// Only start respecting retry-after after a few attempts.
|
||||
// The reason is that Discord often sends unreasonable (20+ minutes) retry-after
|
||||
// Only start respecting retry-after after a few attempts, because
|
||||
// Discord often sends unreasonable (20+ minutes) retry-after
|
||||
// on the very first request.
|
||||
if (i > 3)
|
||||
{
|
||||
@@ -40,7 +41,8 @@ namespace DiscordChatExporter.Core.Utils
|
||||
|
||||
return TimeSpan.FromSeconds(Math.Pow(2, i) + 1);
|
||||
},
|
||||
(_, _, _, _) => Task.CompletedTask);
|
||||
(_, _, _, _) => Task.CompletedTask
|
||||
);
|
||||
|
||||
private static HttpStatusCode? TryGetStatusCodeFromException(HttpRequestException ex) =>
|
||||
// This is extremely frail, but there's no other way
|
||||
|
||||
Reference in New Issue
Block a user