From 3de8005297fcba6e517ea674e3b2dc1129aea8e8 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 2 Apr 2025 18:26:22 +0800 Subject: [PATCH] Improve WebSearch project code quality --- .../SuggestionSources/Baidu.cs | 8 +++----- .../SuggestionSources/Bing.cs | 16 +++++----------- .../SuggestionSources/DuckDuckGo.cs | 8 +++----- .../SuggestionSources/Google.cs | 8 +++----- 4 files changed, 14 insertions(+), 26 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs index 51f81b718..590666af7 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs @@ -4,8 +4,6 @@ using System.Linq; using System.Text.Json; using System.Text.RegularExpressions; using System.Threading.Tasks; -using Flow.Launcher.Infrastructure.Http; -using Flow.Launcher.Infrastructure.Logger; using System.Net.Http; using System.Threading; @@ -22,11 +20,11 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources try { const string api = "http://suggestion.baidu.com/su?json=1&wd="; - result = await Http.GetAsync(api + Uri.EscapeDataString(query), token).ConfigureAwait(false); + result = await Main._context.API.HttpGetStringAsync(api + Uri.EscapeDataString(query), token).ConfigureAwait(false); } catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException}) { - Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); + Main._context.API.LogException(nameof(Baidu), "Can't get suggestion from baidu", e); return null; } @@ -41,7 +39,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources } catch (JsonException e) { - Log.Exception("|Baidu.Suggestions|can't parse suggestions", e); + Main._context.API.LogException(nameof(Baidu), "Can't parse suggestions", e); return new List(); } diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs index 640674243..938f7d387 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs @@ -1,6 +1,4 @@ -using Flow.Launcher.Infrastructure.Http; -using Flow.Launcher.Infrastructure.Logger; -using System; +using System; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; @@ -10,16 +8,15 @@ using System.Threading; namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources { - class Bing : SuggestionSource + public class Bing : SuggestionSource { public override async Task> SuggestionsAsync(string query, CancellationToken token) { - try { const string api = "https://api.bing.com/qsonhs.aspx?q="; - await using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeDataString(query), token).ConfigureAwait(false); + await using var resultStream = await Main._context.API.HttpGetStreamAsync(api + Uri.EscapeDataString(query), token).ConfigureAwait(false); using var json = (await JsonDocument.ParseAsync(resultStream, cancellationToken: token)); var root = json.RootElement.GetProperty("AS"); @@ -33,18 +30,15 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources .EnumerateArray() .Select(s => s.GetProperty("Txt").GetString())) .ToList(); - - - } catch (Exception e) when (e is HttpRequestException or { InnerException: TimeoutException }) { - Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); + Main._context.API.LogException(nameof(Bing), "Can't get suggestion from baidu", e); return null; } catch (JsonException e) { - Log.Exception("|Bing.Suggestions|can't parse suggestions", e); + Main._context.API.LogException(nameof(Bing), "Can't parse suggestions", e); return new List(); } } diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/DuckDuckGo.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/DuckDuckGo.cs index 8fafb44cc..1d248caf3 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/DuckDuckGo.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/DuckDuckGo.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Flow.Launcher.Infrastructure.Http; -using Flow.Launcher.Infrastructure.Logger; using System.Net.Http; using System.Threading; using System.Text.Json; @@ -25,7 +23,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources { const string api = "https://duckduckgo.com/ac/?type=list&q="; - await using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeDataString(query), token: token).ConfigureAwait(false); + await using var resultStream = await Main._context.API.HttpGetStreamAsync(api + Uri.EscapeDataString(query), token: token).ConfigureAwait(false); using var json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token); @@ -36,12 +34,12 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources } catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException}) { - Log.Exception("|DuckDuckGo.Suggestions|Can't get suggestion from DuckDuckGo", e); + Main._context.API.LogException(nameof(DuckDuckGo), "Can't get suggestion from DuckDuckGo", e); return null; } catch (JsonException e) { - Log.Exception("|DuckDuckGo.Suggestions|can't parse suggestions", e); + Main._context.API.LogException(nameof(DuckDuckGo), "Can't parse suggestions", e); return new List(); } } diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs index 265de4a98..5f2504009 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Flow.Launcher.Infrastructure.Http; -using Flow.Launcher.Infrastructure.Logger; using System.Net.Http; using System.Threading; using System.Text.Json; @@ -18,7 +16,7 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources { const string api = "https://www.google.com/complete/search?output=chrome&q="; - await using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeDataString(query), token: token).ConfigureAwait(false); + await using var resultStream = await Main._context.API.HttpGetStreamAsync(api + Uri.EscapeDataString(query), token: token).ConfigureAwait(false); using var json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token); @@ -29,12 +27,12 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources } catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException}) { - Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); + Main._context.API.LogException(nameof(Google), "Can't get suggestion from baidu", e); return null; } catch (JsonException e) { - Log.Exception("|Google.Suggestions|can't parse suggestions", e); + Main._context.API.LogException(nameof(Google), "Can't parse suggestions", e); return new List(); } }