Small cleanup

pull/7/head
Alexey Golub 7 years ago
parent fb3b14f1ce
commit 5e0a39df5e

@ -18,7 +18,8 @@ namespace DiscordChatExporter.Models
public IReadOnlyList<Attachment> Attachments { get; } public IReadOnlyList<Attachment> Attachments { get; }
public Message(string id, DateTime timeStamp, DateTime? editedTimeStamp, User author, string content, IEnumerable<Attachment> attachments) public Message(string id, DateTime timeStamp, DateTime? editedTimeStamp, User author, string content,
IEnumerable<Attachment> attachments)
{ {
Id = id; Id = id;
TimeStamp = timeStamp; TimeStamp = timeStamp;

@ -8,11 +8,16 @@ using Tyrrrz.Extensions;
namespace DiscordChatExporter.Services namespace DiscordChatExporter.Services
{ {
public class DiscordApiService public class DiscordApiService : IDisposable
{ {
private const string ApiRoot = "https://discordapp.com/api"; private const string ApiRoot = "https://discordapp.com/api";
private readonly HttpClient _httpClient = new HttpClient(); private readonly HttpClient _httpClient = new HttpClient();
~DiscordApiService()
{
Dispose(false);
}
private IEnumerable<Message> ParseMessages(string json) private IEnumerable<Message> ParseMessages(string json)
{ {
var messagesJson = JArray.Parse(json); var messagesJson = JArray.Parse(json);
@ -35,9 +40,8 @@ namespace DiscordChatExporter.Services
var authorAvatarHash = authorJson.Value<string>("avatar"); var authorAvatarHash = authorJson.Value<string>("avatar");
// Get attachment // Get attachment
var attachmentsJson = messageJson["attachments"];
var attachments = new List<Attachment>(); var attachments = new List<Attachment>();
foreach (var attachmentJson in attachmentsJson) foreach (var attachmentJson in messageJson["attachments"].EmptyIfNull())
{ {
var attachmentId = attachmentJson.Value<string>("id"); var attachmentId = attachmentJson.Value<string>("id");
var attachmentUrl = attachmentJson.Value<string>("url"); var attachmentUrl = attachmentJson.Value<string>("url");
@ -60,7 +64,7 @@ namespace DiscordChatExporter.Services
var result = new List<Message>(); var result = new List<Message>();
// We are going backwards from last message to first // We are going backwards from last message to first
// ...collecting everything between them in batches // collecting everything between them in batches
string beforeId = null; string beforeId = null;
while (true) while (true)
{ {
@ -95,5 +99,19 @@ namespace DiscordChatExporter.Services
return result; return result;
} }
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_httpClient.Dispose();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
} }
} }
Loading…
Cancel
Save