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};
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<String> {
pub fn request_autocomplete(query: &str) -> Vec<String> {
let mut results = Vec::new();
if let Some(result) = evaluate(query, false) {
results.push(format!("{query} = {result}"));
results.push(format!("{query}={result}"));
}
return results;

View File

@ -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();
}

View File

@ -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())
{

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 {
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<RequestAutocompleteResponse> {
pub fn request_autocomplete(&self, query: &str) -> Option<RequestAutocompleteResponse> {
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<ProgressUpdate>,
@ -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<Vec<String>> {
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<reqwest::Client> = LazyLock::new(|| reqwest::Client::new());
pub static CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| reqwest::Client::new());
pub async fn search(
query: SearchQuery,
progress_tx: mpsc::UnboundedSender<ProgressUpdate>,
) -> eyre::Result<Response> {
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>> {
let engines = Engine::all();
autocomplete_with_client_and_engines(&CLIENT, &engines, query).await
autocomplete_with_engines(&engines, query).await
}
#[derive(Debug)]

View File

@ -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",

View File

@ -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",

View File

@ -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<EngineResponse> {
)
}
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",