clean up code by not having the static CLIENT be passed everywhere

This commit is contained in:
mat 2023-12-20 17:20:41 -06:00
parent c656e65614
commit 90b962e3b0
7 changed files with 33 additions and 39 deletions

View File

@ -1,6 +1,6 @@
use crate::engines::{EngineResponse, SearchQuery}; 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 query = query.query.as_str();
let Some(result_html) = evaluate(query, true) else { 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<String> { pub fn request_autocomplete(query: &str) -> Vec<String> {
let mut results = Vec::new(); let mut results = Vec::new();
if let Some(result) = evaluate(query, false) { if let Some(result) = evaluate(query, false) {
results.push(format!("{query} = {result}")); results.push(format!("{query}={result}"));
} }
return results; return results;

View File

@ -2,7 +2,7 @@ use crate::engines::{EngineResponse, SearchQuery};
use super::regex; 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()) { if !regex!("^what('s|s| is) my ip").is_match(&query.query.to_lowercase()) {
return EngineResponse::new(); return EngineResponse::new();
} }

View File

@ -2,7 +2,7 @@ use crate::engines::{EngineResponse, SearchQuery};
use super::regex; 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)$") if !regex!("^(what('s|s| is) my (user ?agent|ua)|ua|user ?agent)$")
.is_match(&query.query.to_lowercase()) .is_match(&query.query.to_lowercase())
{ {

View File

@ -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 { match self {
Engine::Google => search::google::request(client, query).into(), Engine::Google => search::google::request(query).into(),
Engine::Bing => search::bing::request(client, query).into(), Engine::Bing => search::bing::request(query).into(),
Engine::Brave => search::brave::request(client, query).into(), Engine::Brave => search::brave::request(query).into(),
Engine::Useragent => answer::useragent::request(client, query).into(), Engine::Useragent => answer::useragent::request(query).into(),
Engine::Ip => answer::ip::request(client, query).into(), Engine::Ip => answer::ip::request(query).into(),
Engine::Calc => answer::calc::request(client, query).into(), Engine::Calc => answer::calc::request(query).into(),
} }
} }
@ -75,14 +75,10 @@ impl Engine {
} }
} }
pub fn request_autocomplete( pub fn request_autocomplete(&self, query: &str) -> Option<RequestAutocompleteResponse> {
&self,
client: &reqwest::Client,
query: &str,
) -> Option<RequestAutocompleteResponse> {
match self { match self {
Engine::Google => Some(search::google::request_autocomplete(client, query).into()), Engine::Google => Some(search::google::request_autocomplete(query).into()),
Engine::Calc => Some(answer::calc::request_autocomplete(client, query).into()), Engine::Calc => Some(answer::calc::request_autocomplete(query).into()),
_ => None, _ => None,
} }
} }
@ -198,8 +194,7 @@ impl ProgressUpdate {
} }
} }
pub async fn search_with_client_and_engines( pub async fn search_with_engines(
client: &reqwest::Client,
engines: &[Engine], engines: &[Engine],
query: &SearchQuery, query: &SearchQuery,
progress_tx: mpsc::UnboundedSender<ProgressUpdate>, progress_tx: mpsc::UnboundedSender<ProgressUpdate>,
@ -211,7 +206,7 @@ pub async fn search_with_client_and_engines(
requests.push(async { requests.push(async {
let engine = *engine; let engine = *engine;
let request_response = engine.request(client, query).into(); let request_response = engine.request(query).into();
let response = match request_response { let response = match request_response {
RequestResponse::Http(request) => { RequestResponse::Http(request) => {
@ -266,14 +261,13 @@ pub async fn search_with_client_and_engines(
Ok(merge_engine_responses(responses)) Ok(merge_engine_responses(responses))
} }
pub async fn autocomplete_with_client_and_engines( pub async fn autocomplete_with_engines(
client: &reqwest::Client,
engines: &[Engine], engines: &[Engine],
query: &str, query: &str,
) -> eyre::Result<Vec<String>> { ) -> eyre::Result<Vec<String>> {
let mut requests = Vec::new(); let mut requests = Vec::new();
for engine in engines { for engine in engines {
if let Some(request) = engine.request_autocomplete(client, query) { if let Some(request) = engine.request_autocomplete(query) {
requests.push(async { requests.push(async {
let response = match request { let response = match request {
RequestAutocompleteResponse::Http(request) => { RequestAutocompleteResponse::Http(request) => {
@ -300,19 +294,19 @@ pub async fn autocomplete_with_client_and_engines(
Ok(merge_autocomplete_responses(autocomplete_results)) Ok(merge_autocomplete_responses(autocomplete_results))
} }
static CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| reqwest::Client::new()); pub static CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| reqwest::Client::new());
pub async fn search( pub async fn search(
query: SearchQuery, query: SearchQuery,
progress_tx: mpsc::UnboundedSender<ProgressUpdate>, progress_tx: mpsc::UnboundedSender<ProgressUpdate>,
) -> eyre::Result<Response> { ) -> eyre::Result<Response> {
let engines = Engine::all(); 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<Vec<String>> { pub async fn autocomplete(query: &str) -> eyre::Result<Vec<String>> {
let engines = Engine::all(); let engines = Engine::all();
autocomplete_with_client_and_engines(&CLIENT, &engines, query).await autocomplete_with_engines(&engines, query).await
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -3,12 +3,12 @@ use reqwest::Url;
use scraper::{ElementRef, Selector}; use scraper::{ElementRef, Selector};
use crate::{ use crate::{
engines::EngineResponse, engines::{EngineResponse, CLIENT},
parse::{parse_html_response_with_opts, ParseOpts, QueryMethod}, parse::{parse_html_response_with_opts, ParseOpts, QueryMethod},
}; };
pub fn request(client: &reqwest::Client, query: &str) -> reqwest::RequestBuilder { pub fn request(query: &str) -> reqwest::RequestBuilder {
client CLIENT
.get( .get(
Url::parse_with_params( Url::parse_with_params(
"https://www.bing.com/search", "https://www.bing.com/search",

View File

@ -1,12 +1,12 @@
use reqwest::Url; use reqwest::Url;
use crate::{ use crate::{
engines::EngineResponse, engines::{EngineResponse, CLIENT},
parse::{parse_html_response_with_opts, ParseOpts}, parse::{parse_html_response_with_opts, ParseOpts},
}; };
pub fn request(client: &reqwest::Client, query: &str) -> reqwest::RequestBuilder { pub fn request(query: &str) -> reqwest::RequestBuilder {
client CLIENT
.get(Url::parse_with_params("https://search.brave.com/search", &[("q", query)]).unwrap()) .get(Url::parse_with_params("https://search.brave.com/search", &[("q", query)]).unwrap())
.header( .header(
"User-Agent", "User-Agent",

View File

@ -2,12 +2,12 @@ use reqwest::Url;
use scraper::{ElementRef, Selector}; use scraper::{ElementRef, Selector};
use crate::{ use crate::{
engines::EngineResponse, engines::{EngineResponse, CLIENT},
parse::{parse_html_response_with_opts, ParseOpts, QueryMethod}, parse::{parse_html_response_with_opts, ParseOpts, QueryMethod},
}; };
pub fn request(client: &reqwest::Client, query: &str) -> reqwest::RequestBuilder { pub fn request(query: &str) -> reqwest::RequestBuilder {
client CLIENT
.get( .get(
Url::parse_with_params( Url::parse_with_params(
"https://www.google.com/search", "https://www.google.com/search",
@ -47,8 +47,8 @@ pub fn parse_response(body: &str) -> eyre::Result<EngineResponse> {
) )
} }
pub fn request_autocomplete(client: &reqwest::Client, query: &str) -> reqwest::RequestBuilder { pub fn request_autocomplete(query: &str) -> reqwest::RequestBuilder {
client CLIENT
.get( .get(
Url::parse_with_params( Url::parse_with_params(
"https://suggestqueries.google.com/complete/search", "https://suggestqueries.google.com/complete/search",