From 5e0a39df5ef8294f804343caa151e52665bdee09 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Sat, 9 Sep 2017 22:22:15 +0300 Subject: [PATCH] Small cleanup --- DiscordChatExporter/Models/Message.cs | 3 ++- .../Services/DiscordApiService.cs | 26 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/DiscordChatExporter/Models/Message.cs b/DiscordChatExporter/Models/Message.cs index 3bfaa6a6..40cf824c 100644 --- a/DiscordChatExporter/Models/Message.cs +++ b/DiscordChatExporter/Models/Message.cs @@ -18,7 +18,8 @@ namespace DiscordChatExporter.Models public IReadOnlyList Attachments { get; } - public Message(string id, DateTime timeStamp, DateTime? editedTimeStamp, User author, string content, IEnumerable attachments) + public Message(string id, DateTime timeStamp, DateTime? editedTimeStamp, User author, string content, + IEnumerable attachments) { Id = id; TimeStamp = timeStamp; diff --git a/DiscordChatExporter/Services/DiscordApiService.cs b/DiscordChatExporter/Services/DiscordApiService.cs index 0b00bae4..7bf87401 100644 --- a/DiscordChatExporter/Services/DiscordApiService.cs +++ b/DiscordChatExporter/Services/DiscordApiService.cs @@ -8,11 +8,16 @@ using Tyrrrz.Extensions; namespace DiscordChatExporter.Services { - public class DiscordApiService + public class DiscordApiService : IDisposable { private const string ApiRoot = "https://discordapp.com/api"; private readonly HttpClient _httpClient = new HttpClient(); + ~DiscordApiService() + { + Dispose(false); + } + private IEnumerable ParseMessages(string json) { var messagesJson = JArray.Parse(json); @@ -35,9 +40,8 @@ namespace DiscordChatExporter.Services var authorAvatarHash = authorJson.Value("avatar"); // Get attachment - var attachmentsJson = messageJson["attachments"]; var attachments = new List(); - foreach (var attachmentJson in attachmentsJson) + foreach (var attachmentJson in messageJson["attachments"].EmptyIfNull()) { var attachmentId = attachmentJson.Value("id"); var attachmentUrl = attachmentJson.Value("url"); @@ -60,7 +64,7 @@ namespace DiscordChatExporter.Services var result = new List(); // We are going backwards from last message to first - // ...collecting everything between them in batches + // collecting everything between them in batches string beforeId = null; while (true) { @@ -95,5 +99,19 @@ namespace DiscordChatExporter.Services return result; } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + _httpClient.Dispose(); + } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } } } \ No newline at end of file