From 90b962e3b05acb226eeb7e6a46609231357d9572 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 20 Dec 2023 17:20:41 -0600 Subject: [PATCH] clean up code by not having the static CLIENT be passed everywhere --- src/engines/answer/calc.rs | 6 ++--- src/engines/answer/ip.rs | 2 +- src/engines/answer/useragent.rs | 2 +- src/engines/mod.rs | 40 ++++++++++++++------------------- src/engines/search/bing.rs | 6 ++--- src/engines/search/brave.rs | 6 ++--- src/engines/search/google.rs | 10 ++++----- 7 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/engines/answer/calc.rs b/src/engines/answer/calc.rs index 61aed79..58408be 100644 --- a/src/engines/answer/calc.rs +++ b/src/engines/answer/calc.rs @@ -1,6 +1,6 @@ use crate::engines::{EngineResponse, SearchQuery}; -pub fn request(_client: &reqwest::Client, query: &SearchQuery) -> EngineResponse { +pub fn request(query: &SearchQuery) -> EngineResponse { let query = query.query.as_str(); let Some(result_html) = evaluate(query, true) else { @@ -14,11 +14,11 @@ pub fn request(_client: &reqwest::Client, query: &SearchQuery) -> EngineResponse )) } -pub fn request_autocomplete(_client: &reqwest::Client, query: &str) -> Vec { +pub fn request_autocomplete(query: &str) -> Vec { let mut results = Vec::new(); if let Some(result) = evaluate(query, false) { - results.push(format!("{query} = {result}")); + results.push(format!("{query}={result}")); } return results; diff --git a/src/engines/answer/ip.rs b/src/engines/answer/ip.rs index fe9d7b4..d30bffc 100644 --- a/src/engines/answer/ip.rs +++ b/src/engines/answer/ip.rs @@ -2,7 +2,7 @@ use crate::engines::{EngineResponse, SearchQuery}; use super::regex; -pub fn request(_client: &reqwest::Client, query: &SearchQuery) -> EngineResponse { +pub fn request(query: &SearchQuery) -> EngineResponse { if !regex!("^what('s|s| is) my ip").is_match(&query.query.to_lowercase()) { return EngineResponse::new(); } diff --git a/src/engines/answer/useragent.rs b/src/engines/answer/useragent.rs index 5227d29..43a10e7 100644 --- a/src/engines/answer/useragent.rs +++ b/src/engines/answer/useragent.rs @@ -2,7 +2,7 @@ use crate::engines::{EngineResponse, SearchQuery}; use super::regex; -pub fn request(_client: &reqwest::Client, query: &SearchQuery) -> EngineResponse { +pub fn request(query: &SearchQuery) -> EngineResponse { if !regex!("^(what('s|s| is) my (user ?agent|ua)|ua|user ?agent)$") .is_match(&query.query.to_lowercase()) { diff --git a/src/engines/mod.rs b/src/engines/mod.rs index 7c1a2d5..9eaeac2 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -55,14 +55,14 @@ impl Engine { } } - pub fn request(&self, client: &reqwest::Client, query: &SearchQuery) -> RequestResponse { + pub fn request(&self, query: &SearchQuery) -> RequestResponse { match self { - Engine::Google => search::google::request(client, query).into(), - Engine::Bing => search::bing::request(client, query).into(), - Engine::Brave => search::brave::request(client, query).into(), - Engine::Useragent => answer::useragent::request(client, query).into(), - Engine::Ip => answer::ip::request(client, query).into(), - Engine::Calc => answer::calc::request(client, query).into(), + Engine::Google => search::google::request(query).into(), + Engine::Bing => search::bing::request(query).into(), + Engine::Brave => search::brave::request(query).into(), + Engine::Useragent => answer::useragent::request(query).into(), + Engine::Ip => answer::ip::request(query).into(), + Engine::Calc => answer::calc::request(query).into(), } } @@ -75,14 +75,10 @@ impl Engine { } } - pub fn request_autocomplete( - &self, - client: &reqwest::Client, - query: &str, - ) -> Option { + pub fn request_autocomplete(&self, query: &str) -> Option { match self { - Engine::Google => Some(search::google::request_autocomplete(client, query).into()), - Engine::Calc => Some(answer::calc::request_autocomplete(client, query).into()), + Engine::Google => Some(search::google::request_autocomplete(query).into()), + Engine::Calc => Some(answer::calc::request_autocomplete(query).into()), _ => None, } } @@ -198,8 +194,7 @@ impl ProgressUpdate { } } -pub async fn search_with_client_and_engines( - client: &reqwest::Client, +pub async fn search_with_engines( engines: &[Engine], query: &SearchQuery, progress_tx: mpsc::UnboundedSender, @@ -211,7 +206,7 @@ pub async fn search_with_client_and_engines( requests.push(async { let engine = *engine; - let request_response = engine.request(client, query).into(); + let request_response = engine.request(query).into(); let response = match request_response { RequestResponse::Http(request) => { @@ -266,14 +261,13 @@ pub async fn search_with_client_and_engines( Ok(merge_engine_responses(responses)) } -pub async fn autocomplete_with_client_and_engines( - client: &reqwest::Client, +pub async fn autocomplete_with_engines( engines: &[Engine], query: &str, ) -> eyre::Result> { let mut requests = Vec::new(); for engine in engines { - if let Some(request) = engine.request_autocomplete(client, query) { + if let Some(request) = engine.request_autocomplete(query) { requests.push(async { let response = match request { RequestAutocompleteResponse::Http(request) => { @@ -300,19 +294,19 @@ pub async fn autocomplete_with_client_and_engines( Ok(merge_autocomplete_responses(autocomplete_results)) } -static CLIENT: LazyLock = LazyLock::new(|| reqwest::Client::new()); +pub static CLIENT: LazyLock = LazyLock::new(|| reqwest::Client::new()); pub async fn search( query: SearchQuery, progress_tx: mpsc::UnboundedSender, ) -> eyre::Result { let engines = Engine::all(); - search_with_client_and_engines(&CLIENT, &engines, &query, progress_tx).await + search_with_engines(&engines, &query, progress_tx).await } pub async fn autocomplete(query: &str) -> eyre::Result> { let engines = Engine::all(); - autocomplete_with_client_and_engines(&CLIENT, &engines, query).await + autocomplete_with_engines(&engines, query).await } #[derive(Debug)] diff --git a/src/engines/search/bing.rs b/src/engines/search/bing.rs index 6d1184e..fda80e7 100644 --- a/src/engines/search/bing.rs +++ b/src/engines/search/bing.rs @@ -3,12 +3,12 @@ use reqwest::Url; use scraper::{ElementRef, Selector}; use crate::{ - engines::EngineResponse, + engines::{EngineResponse, CLIENT}, parse::{parse_html_response_with_opts, ParseOpts, QueryMethod}, }; -pub fn request(client: &reqwest::Client, query: &str) -> reqwest::RequestBuilder { - client +pub fn request(query: &str) -> reqwest::RequestBuilder { + CLIENT .get( Url::parse_with_params( "https://www.bing.com/search", diff --git a/src/engines/search/brave.rs b/src/engines/search/brave.rs index 2774904..920f5f8 100644 --- a/src/engines/search/brave.rs +++ b/src/engines/search/brave.rs @@ -1,12 +1,12 @@ use reqwest::Url; use crate::{ - engines::EngineResponse, + engines::{EngineResponse, CLIENT}, parse::{parse_html_response_with_opts, ParseOpts}, }; -pub fn request(client: &reqwest::Client, query: &str) -> reqwest::RequestBuilder { - client +pub fn request(query: &str) -> reqwest::RequestBuilder { + CLIENT .get(Url::parse_with_params("https://search.brave.com/search", &[("q", query)]).unwrap()) .header( "User-Agent", diff --git a/src/engines/search/google.rs b/src/engines/search/google.rs index c5ce030..50ce784 100644 --- a/src/engines/search/google.rs +++ b/src/engines/search/google.rs @@ -2,12 +2,12 @@ use reqwest::Url; use scraper::{ElementRef, Selector}; use crate::{ - engines::EngineResponse, + engines::{EngineResponse, CLIENT}, parse::{parse_html_response_with_opts, ParseOpts, QueryMethod}, }; -pub fn request(client: &reqwest::Client, query: &str) -> reqwest::RequestBuilder { - client +pub fn request(query: &str) -> reqwest::RequestBuilder { + CLIENT .get( Url::parse_with_params( "https://www.google.com/search", @@ -47,8 +47,8 @@ pub fn parse_response(body: &str) -> eyre::Result { ) } -pub fn request_autocomplete(client: &reqwest::Client, query: &str) -> reqwest::RequestBuilder { - client +pub fn request_autocomplete(query: &str) -> reqwest::RequestBuilder { + CLIENT .get( Url::parse_with_params( "https://suggestqueries.google.com/complete/search",