This commit is contained in:
Alexey Golub
2019-09-26 20:44:28 +03:00
parent 69cfe1845b
commit 05c551f80c
5 changed files with 49 additions and 103 deletions

View File

@@ -32,41 +32,37 @@ namespace DiscordChatExporter.Core.Services
{
// Create request
const string apiRoot = "https://discordapp.com/api/v6";
using (var request = new HttpRequestMessage(HttpMethod.Get, $"{apiRoot}/{resource}/{endpoint}"))
using var request = new HttpRequestMessage(HttpMethod.Get, $"{apiRoot}/{resource}/{endpoint}");
// Set authorization header
request.Headers.Authorization = token.Type == AuthTokenType.Bot
? new AuthenticationHeaderValue("Bot", token.Value)
: new AuthenticationHeaderValue(token.Value);
// Add parameters
foreach (var parameter in parameters)
{
// Set authorization header
request.Headers.Authorization = token.Type == AuthTokenType.Bot
? new AuthenticationHeaderValue("Bot", token.Value)
: new AuthenticationHeaderValue(token.Value);
var key = parameter.SubstringUntil("=");
var value = parameter.SubstringAfter("=");
// Add parameters
foreach (var parameter in parameters)
{
var key = parameter.SubstringUntil("=");
var value = parameter.SubstringAfter("=");
// Skip empty values
if (value.IsNullOrWhiteSpace())
continue;
// Skip empty values
if (value.IsNullOrWhiteSpace())
continue;
request.RequestUri = request.RequestUri.SetQueryParameter(key, value);
}
// Get response
using (var response = await _httpClient.SendAsync(request))
{
// 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);
}
request.RequestUri = request.RequestUri.SetQueryParameter(key, value);
}
// Get response
using var response = await _httpClient.SendAsync(request);
// 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);
});
}

View File

@@ -42,8 +42,8 @@ namespace DiscordChatExporter.Core.Services
Directory.CreateDirectory(dirPath);
// Render chat log to output file
using (var writer = File.CreateText(filePath))
await CreateRenderer(chatLog, format).RenderAsync(writer);
await using var writer = File.CreateText(filePath);
await CreateRenderer(chatLog, format).RenderAsync(writer);
}
public async Task ExportChatLogAsync(ChatLog chatLog, string filePath, ExportFormat format, int? partitionLimit)

View File

@@ -12,7 +12,7 @@ namespace DiscordChatExporter.Core.Services.Helpers
public static bool IsDirectoryPath(string path) =>
path.Last() == Path.DirectorySeparatorChar ||
path.Last() == Path.AltDirectorySeparatorChar ||
(Path.GetExtension(path).IsNullOrWhiteSpace() && !File.Exists(path));
Path.GetExtension(path).IsNullOrWhiteSpace() && !File.Exists(path);
public static string GetDefaultExportFileName(ExportFormat format, Guild guild, Channel channel,
DateTimeOffset? after = null, DateTimeOffset? before = null)