diff --git a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj index 3c1d999..23e5728 100644 --- a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj +++ b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj @@ -23,6 +23,7 @@ + diff --git a/DiscordChatExporter.Core/Exceptions/HttpErrorStatusCodeException.cs b/DiscordChatExporter.Core/Exceptions/HttpErrorStatusCodeException.cs index dd4d90f..898f298 100644 --- a/DiscordChatExporter.Core/Exceptions/HttpErrorStatusCodeException.cs +++ b/DiscordChatExporter.Core/Exceptions/HttpErrorStatusCodeException.cs @@ -7,9 +7,13 @@ namespace DiscordChatExporter.Core.Exceptions { 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; + ReasonPhrase = reasonPhrase; } } } \ No newline at end of file diff --git a/DiscordChatExporter.Core/Services/DataService.cs b/DiscordChatExporter.Core/Services/DataService.cs index 2f9df87..3b57eb3 100644 --- a/DiscordChatExporter.Core/Services/DataService.cs +++ b/DiscordChatExporter.Core/Services/DataService.cs @@ -7,6 +7,7 @@ using DiscordChatExporter.Core.Exceptions; using DiscordChatExporter.Core.Models; using Newtonsoft.Json.Linq; using DiscordChatExporter.Core.Internal; +using Polly; namespace DiscordChatExporter.Core.Services { @@ -25,20 +26,28 @@ namespace DiscordChatExporter.Core.Services foreach (var parameter in parameters) url += $"&{parameter}"; + // Create request policy + var policy = Policy + .Handle(e => (int) e.StatusCode == 429) + .WaitAndRetryAsync(10, i => TimeSpan.FromSeconds(0.4)); + // Send request - using (var response = await _httpClient.GetAsync(url)) + return await policy.ExecuteAsync(async () => { - // Check status code - // We throw our own exception here because default one doesn't have status code - if (!response.IsSuccessStatusCode) - throw new HttpErrorStatusCodeException(response.StatusCode); - - // Get content - var raw = await response.Content.ReadAsStringAsync(); - - // Parse - return JToken.Parse(raw); - } + using (var response = await _httpClient.GetAsync(url)) + { + // Check status code + // 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 + var raw = await response.Content.ReadAsStringAsync(); + + // Parse + return JToken.Parse(raw); + } + }); } public async Task GetGuildAsync(string token, string guildId) diff --git a/Readme.md b/Readme.md index d093182..b97815a 100644 --- a/Readme.md +++ b/Readme.md @@ -41,6 +41,7 @@ DiscordChatExporter can be used to export message history from a [Discord](https - [MaterialDesignInXamlToolkit](https://github.com/ButchersBoy/MaterialDesignInXamlToolkit) - [Newtonsoft.Json](http://www.newtonsoft.com/json) - [Scriban](https://github.com/lunet-io/scriban) +- [Polly](https://github.com/App-vNext/Polly) - [Onova](https://github.com/Tyrrrz/Onova) - [FluentCommandLineParser](https://github.com/fclp/fluent-command-line-parser) - [Tyrrrz.Extensions](https://github.com/Tyrrrz/Extensions)