Jann Marc Villablanca neon_arch commited on
Commit
669e365
1 Parent(s): b2cbc5e

feat: add new helper function to fetch upstream search engine JSON response (#504)

Browse files
Files changed (1) hide show
  1. src/models/engine_models.rs +36 -0
src/models/engine_models.rs CHANGED
@@ -86,6 +86,42 @@ pub trait SearchEngine: Sync + Send {
86
  .change_context(EngineError::RequestError)?)
87
  }
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  /// This function scrapes results from the upstream engine and puts all the scraped results like
90
  /// title, visiting_url (href in html),engine (from which engine it was fetched from) and description
91
  /// in a RawSearchResult and then adds that to HashMap whose keys are url and values are RawSearchResult
 
86
  .change_context(EngineError::RequestError)?)
87
  }
88
 
89
+ /// This helper function fetches/requests the json search results from the upstream search engine as a vector of bytes.
90
+ ///
91
+ /// # Arguments
92
+ ///
93
+ /// * `url` - It takes the url of the upstream search engine with the user requested search
94
+ /// query appended in the search parameters.
95
+ /// * `header_map` - It takes the http request headers to be sent to the upstream engine in
96
+ /// order to prevent being detected as a bot. It takes the header as a HeaderMap type.
97
+ /// * `request_timeout` - It takes the request timeout value as seconds which is used to limit
98
+ /// the amount of time for each request to remain connected when until the results can be provided
99
+ /// by the upstream engine.
100
+ ///
101
+ /// # Error
102
+ ///
103
+ /// It returns the html data as a vector of bytes if the upstream engine provides the data as expected
104
+ /// otherwise it returns a custom `EngineError`.
105
+ async fn fetch_json_as_bytes_from_upstream(
106
+ &self,
107
+ url: &str,
108
+ header_map: reqwest::header::HeaderMap,
109
+ client: &Client,
110
+ ) -> Result<Vec<u8>, EngineError> {
111
+ // fetch the json response from upstream search engine
112
+
113
+ Ok(client
114
+ .get(url)
115
+ .headers(header_map) // add spoofed headers to emulate human behavior
116
+ .send()
117
+ .await
118
+ .change_context(EngineError::RequestError)?
119
+ .bytes()
120
+ .await
121
+ .change_context(EngineError::RequestError)?
122
+ .to_vec())
123
+ }
124
+
125
  /// This function scrapes results from the upstream engine and puts all the scraped results like
126
  /// title, visiting_url (href in html),engine (from which engine it was fetched from) and description
127
  /// in a RawSearchResult and then adds that to HashMap whose keys are url and values are RawSearchResult