From 6ca2b77f1e64fdd69797168572cf10a7d3d6fbfa Mon Sep 17 00:00:00 2001 From: Vitalii Mikhailov Date: Fri, 11 Oct 2024 11:04:04 +0300 Subject: [PATCH] Minor fixes --- .../HttpClient/ICrashReporterClient.cs | 6 +-- .../RecreateStacktraceController.cs | 8 ++-- .../Controllers/ReportsController.cs | 2 +- .../HttpClients/ICrashReporterClient.cs | 45 ++++++++++++++----- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/BUTR.Site.NexusMods.Client/Services/HttpClient/ICrashReporterClient.cs b/src/BUTR.Site.NexusMods.Client/Services/HttpClient/ICrashReporterClient.cs index 91ac7dde..72266098 100644 --- a/src/BUTR.Site.NexusMods.Client/Services/HttpClient/ICrashReporterClient.cs +++ b/src/BUTR.Site.NexusMods.Client/Services/HttpClient/ICrashReporterClient.cs @@ -12,7 +12,7 @@ namespace BUTR.Site.NexusMods.Client.Services; public interface ICrashReporterClient { - Task GetCrashReportModelAsync(string id, CancellationToken ct); + Task GetCrashReportModelAsync(int tenant, string id, CancellationToken ct); } public sealed class CrashReporterClient : ICrashReporterClient @@ -26,9 +26,9 @@ public CrashReporterClient(HttpClient httpClient, IOptions GetCrashReportModelAsync(string id, CancellationToken ct) + public async Task GetCrashReportModelAsync(int tenant, string id, CancellationToken ct) { - using var request = new HttpRequestMessage(HttpMethod.Get, $"{id}.json"); + using var request = new HttpRequestMessage(HttpMethod.Get, $"{tenant}/{id}.json"); using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct); if (!response.IsSuccessStatusCode) return null; return await JsonSerializer.DeserializeAsync(await response.Content.ReadAsStreamAsync(ct), _jsonSerializerOptions, ct); diff --git a/src/BUTR.Site.NexusMods.Server/Controllers/RecreateStacktraceController.cs b/src/BUTR.Site.NexusMods.Server/Controllers/RecreateStacktraceController.cs index bf74ba9b..b8672a52 100644 --- a/src/BUTR.Site.NexusMods.Server/Controllers/RecreateStacktraceController.cs +++ b/src/BUTR.Site.NexusMods.Server/Controllers/RecreateStacktraceController.cs @@ -45,7 +45,7 @@ public RecreateStacktraceController(ILogger logger if (!HttpContext.OwnsTenantGame()) return ApiResultError("Game is not owned!", StatusCodes.Status401Unauthorized); - string crashReportContent; + string? crashReportContent; try { crashReportContent = await _crashReporterClient.GetCrashReportAsync(tenant, id, ct); @@ -55,7 +55,7 @@ public RecreateStacktraceController(ILogger logger return ApiOk(Enumerable.Empty()); } - if (!CrashReportParser.TryParse(crashReportContent, out var version, out var crashReport, out var json)) + if (string.IsNullOrEmpty(crashReportContent) || !CrashReportParser.TryParse(crashReportContent, out var version, out var crashReport, out var json)) return ApiBadRequest("Invalid crash report!"); var gameVersion = crashReport.Metadata.GameVersion; @@ -82,7 +82,7 @@ public async Task> GetHtmlAsync([BindTenant] TenantId tenan if (!HttpContext.OwnsTenantGame()) return Unauthorized(); - string crashReportContent; + string? crashReportContent; try { crashReportContent = await _crashReporterClient.GetCrashReportAsync(tenant, id, ct); @@ -92,7 +92,7 @@ public async Task> GetHtmlAsync([BindTenant] TenantId tenan return Content(string.Empty, "text/html", Encoding.UTF8); } - if (!CrashReportParser.TryParse(crashReportContent, out var version, out var crashReport, out var json)) + if (string.IsNullOrEmpty(crashReportContent) || !CrashReportParser.TryParse(crashReportContent, out var version, out var crashReport, out var json)) return BadRequest(); var gameVersion = crashReport.Metadata.GameVersion; diff --git a/src/BUTR.Site.NexusMods.Server/Controllers/ReportsController.cs b/src/BUTR.Site.NexusMods.Server/Controllers/ReportsController.cs index 20764c1e..df19b2bf 100644 --- a/src/BUTR.Site.NexusMods.Server/Controllers/ReportsController.cs +++ b/src/BUTR.Site.NexusMods.Server/Controllers/ReportsController.cs @@ -28,7 +28,7 @@ public ReportsController(ILogger logger, ICrashReporterClient [HttpGet("{id}.html")] [Produces("text/html")] - public async Task> GetAllAsync([BindTenant] TenantId tenant, CrashReportFileId id, CancellationToken ct) + public async Task> GetAllAsync([BindTenant] TenantId tenant, CrashReportFileId id, CancellationToken ct) { return Ok(await _crashReporterClient.GetCrashReportAsync(tenant, id, ct)); } diff --git a/src/BUTR.Site.NexusMods.Server/Services/HttpClients/ICrashReporterClient.cs b/src/BUTR.Site.NexusMods.Server/Services/HttpClients/ICrashReporterClient.cs index a72dfa15..6dd9517c 100644 --- a/src/BUTR.Site.NexusMods.Server/Services/HttpClients/ICrashReporterClient.cs +++ b/src/BUTR.Site.NexusMods.Server/Services/HttpClients/ICrashReporterClient.cs @@ -15,10 +15,10 @@ namespace BUTR.Site.NexusMods.Server.Services; public interface ICrashReporterClient { - Task GetCrashReportAsync(TenantId tenant, CrashReportFileId id, CancellationToken ct); - Task GetCrashReportJsonAsync(TenantId tenant, CrashReportFileId id, CancellationToken ct); - IAsyncEnumerable GetNewCrashReportMetadatasAsync(TenantId tenant, DateTime dateTime, CancellationToken ct); - IAsyncEnumerable GetCrashReportMetadatasAsync(TenantId tenant, IEnumerable filenames, CancellationToken ct); + Task GetCrashReportAsync(TenantId tenant, CrashReportFileId id, CancellationToken ct); + Task GetCrashReportJsonAsync(TenantId tenant, CrashReportFileId id, CancellationToken ct); + IAsyncEnumerable GetNewCrashReportMetadatasAsync(TenantId tenant, DateTime dateTime, CancellationToken ct); + IAsyncEnumerable GetCrashReportMetadatasAsync(TenantId tenant, IEnumerable filenames, CancellationToken ct); } public sealed class CrashReporterClient : ICrashReporterClient @@ -32,23 +32,48 @@ public CrashReporterClient(HttpClient httpClient, IOptions GetCrashReportAsync(TenantId tenant, CrashReportFileId id, CancellationToken ct) => await _httpClient.GetStringAsync($"{tenant}/{id}.html", ct); - public async Task GetCrashReportJsonAsync(TenantId tenant, CrashReportFileId id, CancellationToken ct) => await _httpClient.GetStringAsync($"{tenant}/{id}.json", ct); + public async Task GetCrashReportAsync(TenantId tenant, CrashReportFileId id, CancellationToken ct) + { + using var request = new HttpRequestMessage(HttpMethod.Get, $"{tenant}/{id}.html"); + using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct); + if (!response.IsSuccessStatusCode) + return null; + return await response.Content.ReadAsStringAsync(ct); + } + + public async Task GetCrashReportJsonAsync(TenantId tenant, CrashReportFileId id, CancellationToken ct) + { + using var request = new HttpRequestMessage(HttpMethod.Get, $"{tenant}/{id}.json"); + using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct); + if (!response.IsSuccessStatusCode) + return null; + return await _httpClient.GetStringAsync($"{tenant}/{id}.json", ct); + } - public async IAsyncEnumerable GetNewCrashReportMetadatasAsync(TenantId tenant, DateTime dateTime, [EnumeratorCancellation] CancellationToken ct) + public async IAsyncEnumerable GetNewCrashReportMetadatasAsync(TenantId tenant, DateTime dateTime, [EnumeratorCancellation] CancellationToken ct) { using var request = new HttpRequestMessage(HttpMethod.Post, $"{tenant}/getnewcrashreports"); request.Content = JsonContent.Create(new { DateTime = dateTime.ToString("o") }, options: _jsonSerializerOptions); using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct); + if (!response.IsSuccessStatusCode) + yield break; await foreach (var entry in JsonSerializer.DeserializeAsyncEnumerable(await response.Content.ReadAsStreamAsync(ct), _jsonSerializerOptions, ct)) - yield return entry; + { + if (entry is not null) + yield return entry; + } } - public async IAsyncEnumerable GetCrashReportMetadatasAsync(TenantId tenant, IEnumerable filenames, [EnumeratorCancellation] CancellationToken ct) + public async IAsyncEnumerable GetCrashReportMetadatasAsync(TenantId tenant, IEnumerable filenames, [EnumeratorCancellation] CancellationToken ct) { using var request = new HttpRequestMessage(HttpMethod.Post, $"{tenant}/getmetadata"); request.Content = JsonContent.Create(filenames, options: _jsonSerializerOptions); using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct); + if (!response.IsSuccessStatusCode) + yield break; await foreach (var entry in JsonSerializer.DeserializeAsyncEnumerable(await response.Content.ReadAsStreamAsync(ct), _jsonSerializerOptions, ct)) - yield return entry; + { + if (entry is not null) + yield return entry; + } } } \ No newline at end of file