From dacbdab7d64efe0aa35d980faaa4ae36ffdce4cf Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Fri, 30 Apr 2021 11:56:27 +0530 Subject: [PATCH] Add support for search suggestions. --- examples/Cargo.toml | 4 ++++ examples/search_suggestions.rs | 21 +++++++++++++++++++++ piped/src/lib.rs | 30 ++++++++++++++++-------------- 3 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 examples/search_suggestions.rs diff --git a/examples/Cargo.toml b/examples/Cargo.toml index a711c04..f26eb8e 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -24,3 +24,7 @@ path = "trending.rs" [[example]] name = "video" path = "video.rs" + +[[example]] +name = "search_suggestions" +path = "search_suggestions.rs" diff --git a/examples/search_suggestions.rs b/examples/search_suggestions.rs new file mode 100644 index 0000000..c0f5458 --- /dev/null +++ b/examples/search_suggestions.rs @@ -0,0 +1,21 @@ +use piped::piped::PipedClient; +use reqwest::ClientBuilder; + +#[tokio::main] +async fn main() { + let httpclient = ClientBuilder::new() + .user_agent("Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0") + .build() + .unwrap(); + + let instance = "https://pipedapi.kavin.rocks".to_string(); + + let client = PipedClient::new(httpclient, instance); + + let suggestions = client + .get_search_suggestions("techlore".to_string()) + .await + .unwrap(); + + println!("{:?}", suggestions); +} diff --git a/piped/src/lib.rs b/piped/src/lib.rs index cf6b757..b2623ef 100644 --- a/piped/src/lib.rs +++ b/piped/src/lib.rs @@ -58,13 +58,7 @@ pub mod piped { .append_pair("url", nexturl.as_str()) .append_pair("id", nextbody.as_str()); - let resp = &self - .httpclient - .get(url.as_str()) - .send() - .await? - .text() - .await?; + let resp = &self.httpclient.get(url).send().await?.text().await?; let streams: StreamsPage = serde_json::from_str(resp.as_str())?; @@ -100,13 +94,7 @@ pub mod piped { .append_pair("url", nexturl.as_str()) .append_pair("id", nextbody.as_str()); - let resp = &self - .httpclient - .get(url.as_str()) - .send() - .await? - .text() - .await?; + let resp = &self.httpclient.get(url).send().await?.text().await?; let streams: StreamsPage = serde_json::from_str(resp.as_str())?; @@ -129,6 +117,20 @@ pub mod piped { Ok(video) } + + pub async fn get_search_suggestions( + &self, + q: String, + ) -> Result, Box> { + let mut url = Url::parse(format!("{}/suggestions", &self.instance).as_str())?; + url.query_pairs_mut().append_pair("query", q.as_str()); + + let resp = &self.httpclient.get(url).send().await?.text().await?; + + let suggestions: Vec = serde_json::from_str(resp.as_str())?; + + Ok(suggestions) + } } #[derive(Deserialize, Debug)]