From 7ddcc4312935a6f504c307a8dd1522d9e20768d4 Mon Sep 17 00:00:00 2001 From: Victor Benarbia Date: Thu, 28 Dec 2023 14:27:58 +0000 Subject: [PATCH] improve method comment, minor optimization --- examples/reference_example_google.rs | 68 ---------------------------- src/serpapi.rs | 30 +++++++++++- tests/serpapi-test.rs | 4 +- 3 files changed, 30 insertions(+), 72 deletions(-) delete mode 100644 examples/reference_example_google.rs diff --git a/examples/reference_example_google.rs b/examples/reference_example_google.rs deleted file mode 100644 index 26c020a..0000000 --- a/examples/reference_example_google.rs +++ /dev/null @@ -1,68 +0,0 @@ -// reference example google search -use serpapi::serpapi::Client; -use std::collections::HashMap; -use std::env; - -#[tokio::main] -async fn main() -> Result<(), Box> { - // // read secret api key from environment variable - // // To get the key simply copy/paste from https://serpapi.com/dashboard. - // let api_key = match env::var_os("API_KEY") { - // Some(v) => v.into_string().unwrap(), - // None => panic!("$API_KEY is not set"), - // }; - - // println!("let's search about coffee on google"); - // let mut default = HashMap::new(); - // default.insert("api_key".to_string(), api_key); - // default.insert("engine".to_string(), "google".to_string()); - // // initialize the search engine - // let client = Client::new(default); - - // let mut parameter = HashMap::new(); - // parameter.insert( - // "location".to_string(), - // "Austin, TX, Texas, United States".to_string(), - // ); - // parameter.insert("q".to_string(), "coffee".to_string()); - // let mut html_parameter = HashMap::new(); - // html_parameter.clone_from(¶meter); - - // // search returns a JSON as serde_json::Value which can be accessed like a HashMap. - // println!("waiting..."); - // let results = client.search(parameter).await?; - // let organic_results = results["organic_results"].as_array().unwrap(); - // println!("results received"); - // println!("--- JSON ---"); - // println!(" - number of organic results: {}", organic_results.len()); - // println!( - // " - organic_results first result description: {}", - // results["organic_results"][0]["about_this_result"]["source"]["description"] - // ); - // let places = results["local_results"]["places"].as_array().unwrap(); - // println!("number of local_results: {}", places.len()); - // println!(" - local_results first address: {}", places[0]["address"]); - - // // search returns text - // println!("--- HTML search ---"); - // let raw = client.html(html_parameter).await?; - // print!(" - raw HTML size {} bytes\n", raw.len()); - // print!( - // " - async search completed with {}\n", - // results["search_parameters"]["engine"] - // ); - - // // // edit the location in the search - // // println!("--- JSON search with a different location ---"); - // // parameter = HashMap::::new(); - // // parameter.insert("location".to_string(), "Destin, Florida, United States".to_string()); - // // client = Client::google(parameter, api_key); - // // let results = client.json().await?; - // // println!(">> search_parameters: {}", results["search_parameters"]); - // // let places = results["local_results"]["places"].as_array().unwrap(); - // // println!("number of local_results: {}\n", places.len()); - // // println!("local_results first address: {}\n", places[0]["address"]); - - print!("ok"); - Ok(()) -} diff --git a/src/serpapi.rs b/src/serpapi.rs index 618e0f4..dfe4935 100644 --- a/src/serpapi.rs +++ b/src/serpapi.rs @@ -16,6 +16,8 @@ pub struct Client { pub parameter: HashMap, } +const HOST: &str = "http://serpapi.com"; + impl Client { /// initialize a serp api client with default parameters. /// # Arguments @@ -31,6 +33,30 @@ impl Client { /// # Arguments /// * `parameter` search parameter /// + /// # Examples: + /// ``` + /// use std::collections::HashMap; + /// use serpapi::serpapi::Client; + /// + /// #[tokio::main] + /// async fn main() { + /// let mut default = std::collections::HashMap::::new(); + /// default.insert("engine".to_string(), "google".to_string()); + /// default.insert("api_key".to_string(), "secret_api_key".to_string()); + /// // initialize the serpapi client + /// let client = Client::new(default); + /// let mut parameter = HashMap::::new(); + /// parameter.insert("q".to_string(), "coffee".to_string()); + /// parameter.insert( + /// "location".to_string(), + /// "Austin, TX, Texas, United States".to_string(), + /// ); + /// // search returns a JSON as serde_json::Value which can be accessed like a HashMap. + /// let results = client.search(parameter).await.expect("request"); + /// // let organic_results = results["organic_results"].as_array().unwrap(); + /// // assert!(organic_results.len() > 1); + /// } + /// ``` pub async fn search( &self, parameter: HashMap, @@ -123,7 +149,7 @@ impl Client { parameter: HashMap, ) -> Result> { let body = self.get(endpoint, parameter).await?; - println!("Body:\n{}", body); + //debug: println!("Body:\n{}", body); let value: serde_json::Value = serde_json::from_str(&body).unwrap(); Ok(value) } @@ -144,7 +170,7 @@ impl Client { query.insert(key.to_string(), value.to_string()); } - let mut url = "http://serpapi.com".to_string(); + let mut url = HOST.to_string(); url.push_str(endpoint); let client = reqwest::Client::builder().build()?; let res = client.get(url).query(&query).send().await?; diff --git a/tests/serpapi-test.rs b/tests/serpapi-test.rs index 5fb833c..bafdbdc 100644 --- a/tests/serpapi-test.rs +++ b/tests/serpapi-test.rs @@ -13,7 +13,7 @@ fn api_key() -> String { } #[tokio::test] -async fn json() { +async fn search() { let mut default = std::collections::HashMap::::new(); default.insert("engine".to_string(), "google".to_string()); default.insert("api_key".to_string(), api_key()); @@ -72,7 +72,7 @@ async fn location() { } #[tokio::test] -async fn get_account() { +async fn account() { let client = Client::new(HashMap::::new()); let mut parameter = HashMap::::new(); parameter.insert("api_key".to_string(), api_key());