neon_arch
commited on
Commit
•
b72af01
1
Parent(s):
1bccffc
✨ feat: implement common engine trait
Browse files- src/engines/engine_models.rs +34 -3
src/engines/engine_models.rs
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
//! This module provides the error enum to handle different errors associated while requesting data from
|
2 |
//! the upstream search engines with the search query provided by the user.
|
3 |
|
4 |
-
use
|
5 |
-
use
|
|
|
6 |
|
7 |
/// A custom error type used for handle engine associated errors.
|
8 |
///
|
@@ -40,4 +41,34 @@ impl fmt::Display for EngineError {
|
|
40 |
}
|
41 |
}
|
42 |
|
43 |
-
impl Context for EngineError {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
//! This module provides the error enum to handle different errors associated while requesting data from
|
2 |
//! the upstream search engines with the search query provided by the user.
|
3 |
|
4 |
+
use crate::search_results_handler::aggregation_models::RawSearchResult;
|
5 |
+
use error_stack::{IntoReport, Result, ResultExt};
|
6 |
+
use std::{collections::HashMap, fmt, time::Duration};
|
7 |
|
8 |
/// A custom error type used for handle engine associated errors.
|
9 |
///
|
|
|
41 |
}
|
42 |
}
|
43 |
|
44 |
+
impl error_stack::Context for EngineError {}
|
45 |
+
|
46 |
+
#[async_trait::async_trait]
|
47 |
+
pub trait SearchEngine {
|
48 |
+
async fn fetch_html_from_upstream(
|
49 |
+
&self,
|
50 |
+
url: String,
|
51 |
+
header_map: reqwest::header::HeaderMap,
|
52 |
+
) -> Result<String, EngineError> {
|
53 |
+
// fetch the html from upstream search engine
|
54 |
+
Ok(reqwest::Client::new()
|
55 |
+
.get(url)
|
56 |
+
.timeout(Duration::from_secs(5))
|
57 |
+
.headers(header_map) // add spoofed headers to emulate human behaviour
|
58 |
+
.send()
|
59 |
+
.await
|
60 |
+
.into_report()
|
61 |
+
.change_context(EngineError::RequestError)?
|
62 |
+
.text()
|
63 |
+
.await
|
64 |
+
.into_report()
|
65 |
+
.change_context(EngineError::RequestError)?)
|
66 |
+
}
|
67 |
+
|
68 |
+
async fn results(
|
69 |
+
&self,
|
70 |
+
query: String,
|
71 |
+
page: u32,
|
72 |
+
user_agent: String,
|
73 |
+
) -> Result<HashMap<String, RawSearchResult>, EngineError>;
|
74 |
+
}
|