From 431bf31fcc361d0122941ea1a58079c7697919ce Mon Sep 17 00:00:00 2001 From: Julian Schmidhuber Date: Mon, 4 Oct 2021 17:37:00 +0200 Subject: [PATCH] Make functions take references, moved user agent into lib --- examples/channel.rs | 13 ++-- examples/comments.rs | 13 ++-- examples/playlist.rs | 13 ++-- examples/search_suggestions.rs | 13 ++-- examples/trending.rs | 13 ++-- examples/video.rs | 13 ++-- piped/src/client.rs | 114 +++++++++++++++++++-------------- 7 files changed, 95 insertions(+), 97 deletions(-) diff --git a/examples/channel.rs b/examples/channel.rs index 0919422..d767ca6 100644 --- a/examples/channel.rs +++ b/examples/channel.rs @@ -1,16 +1,13 @@ use piped::PipedClient; -use reqwest::ClientBuilder; +use reqwest::Client; + +const INSTANCE: &'static str = "https://pipedapi.kavin.rocks"; #[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 httpclient = Client::new(); - let instance = "https://pipedapi.kavin.rocks".to_string(); - - let client = PipedClient::new(httpclient, instance); + let client = PipedClient::new(&httpclient, INSTANCE); let channel = client .channel_from_id("UCXuqSBlHAE6Xw-yeJA0Tunw".to_string()) diff --git a/examples/comments.rs b/examples/comments.rs index 98d869f..e12a1f8 100644 --- a/examples/comments.rs +++ b/examples/comments.rs @@ -1,16 +1,13 @@ use piped::PipedClient; -use reqwest::ClientBuilder; +use reqwest::Client; + +const INSTANCE: &'static str = "https://pipedapi.kavin.rocks"; #[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 httpclient = Client::new(); - let instance = "https://pipedapi.kavin.rocks".to_string(); - - let client = PipedClient::new(httpclient, instance); + let client = PipedClient::new(&httpclient, INSTANCE); let comments = client .comments_from_id("__hYx6ZzFbQ".to_string()) diff --git a/examples/playlist.rs b/examples/playlist.rs index 45a2887..cd3def6 100644 --- a/examples/playlist.rs +++ b/examples/playlist.rs @@ -1,16 +1,13 @@ use piped::PipedClient; -use reqwest::ClientBuilder; +use reqwest::Client; + +const INSTANCE: &'static str = "https://pipedapi.kavin.rocks"; #[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 httpclient = Client::new(); - let instance = "https://pipedapi.kavin.rocks".to_string(); - - let client = PipedClient::new(httpclient, instance); + let client = PipedClient::new(&httpclient, INSTANCE); let playlist = client .playlist_from_id("PLQSoWXSpjA38FIQCvwnVNPlGPVA63WTD8".to_string()) diff --git a/examples/search_suggestions.rs b/examples/search_suggestions.rs index e3f237a..8a9b16f 100644 --- a/examples/search_suggestions.rs +++ b/examples/search_suggestions.rs @@ -1,16 +1,13 @@ use piped::PipedClient; -use reqwest::ClientBuilder; +use reqwest::Client; + +const INSTANCE: &'static str = "https://pipedapi.kavin.rocks"; #[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 httpclient = Client::new(); - let instance = "https://pipedapi.kavin.rocks".to_string(); - - let client = PipedClient::new(httpclient, instance); + let client = PipedClient::new(&httpclient, instance); let suggestions = client .search_suggestions("techlore".to_string()) diff --git a/examples/trending.rs b/examples/trending.rs index a7fb0f1..4575362 100644 --- a/examples/trending.rs +++ b/examples/trending.rs @@ -1,16 +1,13 @@ use piped::PipedClient; -use reqwest::ClientBuilder; +use reqwest::Client; + +const INSTANCE: &'static str = "https://pipedapi.kavin.rocks"; #[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 httpclient = Client::new(); - let instance = "https://pipedapi.kavin.rocks".to_string(); - - let client = PipedClient::new(httpclient, instance); + let client = PipedClient::new(&httpclient, INSTANCE); let streams = client.trending("US".to_string()).await.unwrap(); diff --git a/examples/video.rs b/examples/video.rs index 72a0e17..922465d 100644 --- a/examples/video.rs +++ b/examples/video.rs @@ -1,16 +1,13 @@ use piped::PipedClient; -use reqwest::ClientBuilder; +use reqwest::Client; + +const INSTANCE: &'static str = "https://pipedapi.kavin.rocks"; #[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 httpclient = Client::new(); - let instance = "https://pipedapi.kavin.rocks".to_string(); - - let client = PipedClient::new(httpclient, instance); + let client = PipedClient::new(&httpclient, INSTANCE); let video = client .video_from_id("__hYx6ZzFbQ".to_string()) diff --git a/piped/src/client.rs b/piped/src/client.rs index ddd096e..6a4977f 100644 --- a/piped/src/client.rs +++ b/piped/src/client.rs @@ -7,32 +7,37 @@ pub struct PipedClient { pub instance: String, } +const USER_AGENT: &'static str = "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0"; + impl PipedClient { - pub fn new(httpclient: Client, instance: String) -> PipedClient { + pub fn new>(httpclient: &Client, instance: S) -> PipedClient { PipedClient { - httpclient, - instance, + httpclient: httpclient.clone(), + instance: instance.as_ref().to_string(), } } - pub async fn trending( + pub async fn trending>( &self, - region: String, + region: S, ) -> Result, Box> { let mut url = Url::parse(format!("{}/trending", &self.instance).as_str())?; - url.query_pairs_mut().append_pair("region", region.as_str()); + url.query_pairs_mut().append_pair("region", region.as_ref()); - let resp = &self.httpclient.get(url).send().await?.text().await?; + let resp = &self.httpclient.get(url) + .header("User-Agent", USER_AGENT) + .send().await?.text().await?; - let streams: Vec = serde_json::from_str(resp.as_str())?; + let streams: Vec = serde_json::from_str(resp.as_ref())?; Ok(streams) } - pub async fn channel_from_id(&self, id: String) -> Result> { + pub async fn channel_from_id>(&self, id: S) -> Result> { let resp = &self .httpclient - .get(format!("{}/channel/{}", &self.instance, id)) + .get(format!("{}/channel/{}", &self.instance, id.as_ref())) + .header("User-Agent", USER_AGENT) .send() .await? .text() @@ -43,63 +48,69 @@ impl PipedClient { Ok(channel) } - pub async fn channel_continuation( + pub async fn channel_continuation>( &self, - id: String, - nexturl: String, - nextbody: String, + id: S, + nexturl: S, + nextbody: S, ) -> Result> { - let mut url = Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id).as_str())?; + let mut url = Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id.as_ref()).as_str())?; url.query_pairs_mut() - .append_pair("url", nexturl.as_str()) - .append_pair("id", nextbody.as_str()); + .append_pair("url", nexturl.as_ref()) + .append_pair("id", nextbody.as_ref()); - let resp = &self.httpclient.get(url).send().await?.text().await?; + let resp = &self.httpclient.get(url) + .header("User-Agent", USER_AGENT) + .send().await?.text().await?; - let streams: StreamsPage = serde_json::from_str(resp.as_str())?; + let streams: StreamsPage = serde_json::from_str(resp.as_ref())?; Ok(streams) } - pub async fn playlist_from_id( + pub async fn playlist_from_id>( &self, - id: String, + id: S, ) -> Result> { let resp = &self .httpclient - .get(format!("{}/playlists/{}", &self.instance, id)) + .get(format!("{}/playlists/{}", &self.instance, id.as_ref())) + .header("User-Agent", USER_AGENT) .send() .await? .text() .await?; - let playlist: Playlist = serde_json::from_str(resp.as_str())?; + let playlist: Playlist = serde_json::from_str(resp.as_ref())?; Ok(playlist) } - pub async fn playlist_continuation( + pub async fn playlist_continuation>( &self, - id: String, - nexturl: String, - nextbody: String, + id: S, + nexturl: S, + nextbody: S, ) -> Result> { - let mut url = Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id).as_str())?; + let mut url = Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id.as_ref()).as_str())?; url.query_pairs_mut() - .append_pair("url", nexturl.as_str()) - .append_pair("id", nextbody.as_str()); + .append_pair("url", nexturl.as_ref()) + .append_pair("id", nextbody.as_ref()); - let resp = &self.httpclient.get(url).send().await?.text().await?; + let resp = &self.httpclient.get(url) + .header("User-Agent", USER_AGENT) + .send().await?.text().await?; let streams: StreamsPage = serde_json::from_str(resp.as_str())?; Ok(streams) } - pub async fn video_from_id(&self, id: String) -> Result> { + pub async fn video_from_id>(&self, id: S) -> Result> { let resp = &self .httpclient - .get(format!("{}/streams/{}", &self.instance, id)) + .get(format!("{}/streams/{}", &self.instance, id.as_ref())) + .header("User-Agent", USER_AGENT) .send() .await? .text() @@ -110,48 +121,53 @@ impl PipedClient { Ok(video) } - pub async fn search_suggestions( + pub async fn search_suggestions>( &self, - q: String, + q: S, ) -> Result, Box> { let mut url = Url::parse(format!("{}/suggestions", &self.instance).as_str())?; - url.query_pairs_mut().append_pair("query", q.as_str()); + url.query_pairs_mut().append_pair("query", q.as_ref()); - let resp = &self.httpclient.get(url).send().await?.text().await?; + let resp = &self.httpclient.get(url) + .header("User-Agent", USER_AGENT) + .send().await?.text().await?; - let suggestions: Vec = serde_json::from_str(resp.as_str())?; + let suggestions: Vec = serde_json::from_str(resp.as_ref())?; Ok(suggestions) } - pub async fn comments_from_id( + pub async fn comments_from_id>( &self, - id: String, + id: S, ) -> Result> { let resp = &self .httpclient - .get(format!("{}/comments/{}", &self.instance, id)) + .get(format!("{}/comments/{}", &self.instance, id.as_ref())) + .header("User-Agent", USER_AGENT) .send() .await? .text() .await?; - let comments: CommentsInfo = serde_json::from_str(resp.as_str())?; + let comments: CommentsInfo = serde_json::from_str(resp.as_ref())?; Ok(comments) } - pub async fn comments_continuation( + pub async fn comments_continuation>( &self, - id: String, - nexturl: String, + id: S, + nexturl: S, ) -> Result> { - let mut url = Url::parse(format!("{}/nextpage/comments/{}", &self.instance, id).as_str())?; - url.query_pairs_mut().append_pair("url", nexturl.as_str()); + let mut url = Url::parse(format!("{}/nextpage/comments/{}", &self.instance, id.as_ref()).as_str())?; + url.query_pairs_mut().append_pair("url", nexturl.as_ref()); - let resp = &self.httpclient.get(url).send().await?.text().await?; + let resp = &self.httpclient.get(url) + .header("User-Agent", USER_AGENT) + .send().await?.text().await?; - let comments: CommentsInfo = serde_json::from_str(resp.as_str())?; + let comments: CommentsInfo = serde_json::from_str(resp.as_ref())?; Ok(comments) }