From 019b3322e79b5e549bf12222aa4964c43dae3154 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Tue, 16 May 2023 12:22:00 +0300 Subject: [PATCH] chore: add enum to handle different reqwest errors & add timeout to requests --- src/engines/duckduckgo.rs | 3 ++- src/engines/engine_models.rs | 8 ++++++++ src/engines/mod.rs | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 src/engines/engine_models.rs diff --git a/src/engines/duckduckgo.rs b/src/engines/duckduckgo.rs index 254ab16..7232165 100644 --- a/src/engines/duckduckgo.rs +++ b/src/engines/duckduckgo.rs @@ -2,7 +2,7 @@ //! by querying the upstream duckduckgo search engine with user provided query and with a page //! number if provided. -use std::collections::HashMap; +use std::{collections::HashMap, time::Duration}; use reqwest::header::{HeaderMap, CONTENT_TYPE, COOKIE, REFERER, USER_AGENT}; use scraper::{Html, Selector}; @@ -57,6 +57,7 @@ pub async fn results( // TODO: Write better error handling code to handle no results case. let results: String = reqwest::Client::new() .get(url) + .timeout(Duration::from_secs(30)) .headers(header_map) // add spoofed headers to emulate human behaviour .send() .await? diff --git a/src/engines/engine_models.rs b/src/engines/engine_models.rs new file mode 100644 index 0000000..7dc8ad6 --- /dev/null +++ b/src/engines/engine_models.rs @@ -0,0 +1,8 @@ +#[derive(Debug)] +pub enum ReqwestError{ + NotFound, + Timeout, + Forbidden, + AccessDenied, + TooManyRequests +} diff --git a/src/engines/mod.rs b/src/engines/mod.rs index 7f390b1..f9bb8ad 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -1,2 +1,3 @@ pub mod duckduckgo; +pub mod engine_models; pub mod searx;