mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-05-12 22:25:38 -04:00
Add retry policy for 429 responses
This commit is contained in:
parent
e3eac10fb8
commit
49f39c7097
4 changed files with 26 additions and 11 deletions
|
@ -23,6 +23,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
||||||
<PackageReference Include="Onova" Version="2.1.0" />
|
<PackageReference Include="Onova" Version="2.1.0" />
|
||||||
|
<PackageReference Include="Polly" Version="6.0.1" />
|
||||||
<PackageReference Include="Scriban" Version="1.2.1" />
|
<PackageReference Include="Scriban" Version="1.2.1" />
|
||||||
<PackageReference Include="Tyrrrz.Extensions" Version="1.5.1" />
|
<PackageReference Include="Tyrrrz.Extensions" Version="1.5.1" />
|
||||||
<PackageReference Include="Tyrrrz.Settings" Version="1.3.2" />
|
<PackageReference Include="Tyrrrz.Settings" Version="1.3.2" />
|
||||||
|
|
|
@ -7,9 +7,13 @@ namespace DiscordChatExporter.Core.Exceptions
|
||||||
{
|
{
|
||||||
public HttpStatusCode StatusCode { get; }
|
public HttpStatusCode StatusCode { get; }
|
||||||
|
|
||||||
public HttpErrorStatusCodeException(HttpStatusCode statusCode)
|
public string ReasonPhrase { get; }
|
||||||
|
|
||||||
|
public HttpErrorStatusCodeException(HttpStatusCode statusCode, string reasonPhrase)
|
||||||
|
: base($"Error HTTP status code: {statusCode} - {reasonPhrase}")
|
||||||
{
|
{
|
||||||
StatusCode = statusCode;
|
StatusCode = statusCode;
|
||||||
|
ReasonPhrase = reasonPhrase;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -7,6 +7,7 @@ using DiscordChatExporter.Core.Exceptions;
|
||||||
using DiscordChatExporter.Core.Models;
|
using DiscordChatExporter.Core.Models;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using DiscordChatExporter.Core.Internal;
|
using DiscordChatExporter.Core.Internal;
|
||||||
|
using Polly;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Services
|
namespace DiscordChatExporter.Core.Services
|
||||||
{
|
{
|
||||||
|
@ -25,20 +26,28 @@ namespace DiscordChatExporter.Core.Services
|
||||||
foreach (var parameter in parameters)
|
foreach (var parameter in parameters)
|
||||||
url += $"&{parameter}";
|
url += $"&{parameter}";
|
||||||
|
|
||||||
|
// Create request policy
|
||||||
|
var policy = Policy
|
||||||
|
.Handle<HttpErrorStatusCodeException>(e => (int) e.StatusCode == 429)
|
||||||
|
.WaitAndRetryAsync(10, i => TimeSpan.FromSeconds(0.4));
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
using (var response = await _httpClient.GetAsync(url))
|
return await policy.ExecuteAsync(async () =>
|
||||||
{
|
{
|
||||||
// Check status code
|
using (var response = await _httpClient.GetAsync(url))
|
||||||
// We throw our own exception here because default one doesn't have status code
|
{
|
||||||
if (!response.IsSuccessStatusCode)
|
// Check status code
|
||||||
throw new HttpErrorStatusCodeException(response.StatusCode);
|
// We throw our own exception here because default one doesn't have status code
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
throw new HttpErrorStatusCodeException(response.StatusCode, response.ReasonPhrase);
|
||||||
|
|
||||||
// Get content
|
// Get content
|
||||||
var raw = await response.Content.ReadAsStringAsync();
|
var raw = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
// Parse
|
// Parse
|
||||||
return JToken.Parse(raw);
|
return JToken.Parse(raw);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Guild> GetGuildAsync(string token, string guildId)
|
public async Task<Guild> GetGuildAsync(string token, string guildId)
|
||||||
|
|
|
@ -41,6 +41,7 @@ DiscordChatExporter can be used to export message history from a [Discord](https
|
||||||
- [MaterialDesignInXamlToolkit](https://github.com/ButchersBoy/MaterialDesignInXamlToolkit)
|
- [MaterialDesignInXamlToolkit](https://github.com/ButchersBoy/MaterialDesignInXamlToolkit)
|
||||||
- [Newtonsoft.Json](http://www.newtonsoft.com/json)
|
- [Newtonsoft.Json](http://www.newtonsoft.com/json)
|
||||||
- [Scriban](https://github.com/lunet-io/scriban)
|
- [Scriban](https://github.com/lunet-io/scriban)
|
||||||
|
- [Polly](https://github.com/App-vNext/Polly)
|
||||||
- [Onova](https://github.com/Tyrrrz/Onova)
|
- [Onova](https://github.com/Tyrrrz/Onova)
|
||||||
- [FluentCommandLineParser](https://github.com/fclp/fluent-command-line-parser)
|
- [FluentCommandLineParser](https://github.com/fclp/fluent-command-line-parser)
|
||||||
- [Tyrrrz.Extensions](https://github.com/Tyrrrz/Extensions)
|
- [Tyrrrz.Extensions](https://github.com/Tyrrrz/Extensions)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue