From d42944e3b504da060920155a67e9f9ceea58221d Mon Sep 17 00:00:00 2001 From: status102 <102887808+status102@users.noreply.github.com> Date: Fri, 30 May 2025 19:39:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(wpf):=20HttpService=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E4=B8=ADuri=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8UriPartial?= =?UTF-8?q?=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Extensions/HttpResponseLoggingExtension.cs | 6 +++--- src/MaaWpfGui/Models/ResourceUpdater.cs | 2 +- src/MaaWpfGui/Services/Web/HttpService.cs | 8 ++++---- src/MaaWpfGui/Services/Web/IHttpService.cs | 7 ++++--- src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/MaaWpfGui/Extensions/HttpResponseLoggingExtension.cs b/src/MaaWpfGui/Extensions/HttpResponseLoggingExtension.cs index 54b65f6d18..1c1999ebd0 100644 --- a/src/MaaWpfGui/Extensions/HttpResponseLoggingExtension.cs +++ b/src/MaaWpfGui/Extensions/HttpResponseLoggingExtension.cs @@ -21,7 +21,7 @@ namespace MaaWpfGui.Extensions { private static readonly ILogger _logger = Serilog.Log.ForContext("SourceContext", "HttpResponseLoggingExtension"); - public static void Log(this HttpResponseMessage response, bool logQuery = true) + public static void Log(this HttpResponseMessage response, UriPartial uriPartial = UriPartial.Query) { var method = response?.RequestMessage?.Method; var uri = response?.RequestMessage?.RequestUri; @@ -29,11 +29,11 @@ namespace MaaWpfGui.Extensions if (response is { IsSuccessStatusCode: true }) { - _logger.Information("HTTP: {StatusCode} {Method} {Url}", statusCode, method, uri?.GetLeftPart(logQuery ? UriPartial.Query : UriPartial.Path)); + _logger.Information("HTTP: {StatusCode} {Method} {Url}", statusCode, method, uri?.GetLeftPart(uriPartial)); } else { - _logger.Warning("HTTP: {StatusCode} {Method} {Url}", statusCode, method, uri?.GetLeftPart(logQuery ? UriPartial.Query : UriPartial.Path)); + _logger.Warning("HTTP: {StatusCode} {Method} {Url}", statusCode, method, uri?.GetLeftPart(uriPartial)); } } } diff --git a/src/MaaWpfGui/Models/ResourceUpdater.cs b/src/MaaWpfGui/Models/ResourceUpdater.cs index af6ac17af9..ca526caba2 100644 --- a/src/MaaWpfGui/Models/ResourceUpdater.cs +++ b/src/MaaWpfGui/Models/ResourceUpdater.cs @@ -126,7 +126,7 @@ namespace MaaWpfGui.Models HttpResponseMessage? response = null; try { - response = await Instances.HttpService.GetAsync(new(url), logQuery: false); + response = await Instances.HttpService.GetAsync(new(url), uriPartial: UriPartial.Path); } catch (Exception e) { diff --git a/src/MaaWpfGui/Services/Web/HttpService.cs b/src/MaaWpfGui/Services/Web/HttpService.cs index b1fc48ef71..775291ee53 100755 --- a/src/MaaWpfGui/Services/Web/HttpService.cs +++ b/src/MaaWpfGui/Services/Web/HttpService.cs @@ -145,7 +145,7 @@ namespace MaaWpfGui.Services.Web } } - public async Task GetAsync(Uri uri, Dictionary? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, bool logQuery = true) + public async Task GetAsync(Uri uri, Dictionary? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, UriPartial uriPartial = UriPartial.Query) { var request = new HttpRequestMessage { RequestUri = uri, Method = HttpMethod.Get, Version = HttpVersion.Version20, }; if (extraHeader != null) @@ -157,7 +157,7 @@ namespace MaaWpfGui.Services.Web } var response = await _client.SendAsync(request, httpCompletionOption); - response.Log(logQuery); + response.Log(uriPartial); return response; } @@ -189,7 +189,7 @@ namespace MaaWpfGui.Services.Web } } - public async Task PostAsync(Uri uri, HttpContent content, Dictionary? extraHeader = null) + public async Task PostAsync(Uri uri, HttpContent content, Dictionary? extraHeader = null, UriPartial uriPartial = UriPartial.Query) { var message = new HttpRequestMessage(HttpMethod.Post, uri) { Version = HttpVersion.Version20 }; if (extraHeader is not null) @@ -203,7 +203,7 @@ namespace MaaWpfGui.Services.Web message.Headers.Accept.ParseAdd("application/json"); message.Content = content; var response = await _client.SendAsync(message); - response.Log(); + response.Log(uriPartial); return response; } diff --git a/src/MaaWpfGui/Services/Web/IHttpService.cs b/src/MaaWpfGui/Services/Web/IHttpService.cs index 5f51a18cd2..8e9d106745 100644 --- a/src/MaaWpfGui/Services/Web/IHttpService.cs +++ b/src/MaaWpfGui/Services/Web/IHttpService.cs @@ -55,9 +55,9 @@ namespace MaaWpfGui.Services.Web /// Target Uri /// Extra HTTP Request Headers /// The HTTP completion option - /// Whether to log uri + /// Which parts of uri to log /// object - Task GetAsync(Uri uri, Dictionary? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, bool logQuery = true); + Task GetAsync(Uri uri, Dictionary? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, UriPartial uriPartial = UriPartial.Query); /// /// Send HTTP POST request and a string response @@ -84,8 +84,9 @@ namespace MaaWpfGui.Services.Web /// Target Uri /// The POST body content /// Extra HTTP Request Headers + /// Which parts of uri to log /// HttpResponseMessage, null when failed - Task PostAsync(Uri uri, HttpContent content, Dictionary? extraHeader = null); + Task PostAsync(Uri uri, HttpContent content, Dictionary? extraHeader = null, UriPartial uriPartial = UriPartial.Query); /// /// Download a file from the Web diff --git a/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs b/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs index 1b59f22461..5e9ac03c15 100644 --- a/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs @@ -983,7 +983,7 @@ public class VersionUpdateViewModel : Screen HttpResponseMessage? response = null; try { - response = await Instances.HttpService.GetAsync(new(url), logQuery: false); + response = await Instances.HttpService.GetAsync(new(url), uriPartial: UriPartial.Path); } catch (Exception e) {