From 973fc966bbb332c81b4a43d50a7a8f6fa98e5c70 Mon Sep 17 00:00:00 2001 From: Julian Schmidhuber Date: Tue, 5 Oct 2021 15:27:57 +0200 Subject: [PATCH] Improved error handling --- examples/search_suggestions.rs | 2 +- piped/Cargo.toml | 3 ++ piped/src/client.rs | 89 ++++++++++++++++++++-------------- piped/src/error.rs | 13 +++++ piped/src/lib.rs | 2 + 5 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 piped/src/error.rs diff --git a/examples/search_suggestions.rs b/examples/search_suggestions.rs index 8a9b16f..9c1ecc3 100644 --- a/examples/search_suggestions.rs +++ b/examples/search_suggestions.rs @@ -7,7 +7,7 @@ const INSTANCE: &'static str = "https://pipedapi.kavin.rocks"; async fn main() { let httpclient = Client::new(); - let client = PipedClient::new(&httpclient, instance); + let client = PipedClient::new(&httpclient, INSTANCE); let suggestions = client .search_suggestions("techlore".to_string()) diff --git a/piped/Cargo.toml b/piped/Cargo.toml index 95046d4..8d7a808 100644 --- a/piped/Cargo.toml +++ b/piped/Cargo.toml @@ -12,5 +12,8 @@ version = "0.0.0" [dependencies] reqwest = "^0.11" +url = "^2.2" serde = {version = "^1.0", features = ["derive"]} serde_json = "^1.0" + +thiserror = "^1.0" diff --git a/piped/src/client.rs b/piped/src/client.rs index 6a4977f..0a43abf 100644 --- a/piped/src/client.rs +++ b/piped/src/client.rs @@ -1,13 +1,14 @@ use reqwest::{Client, Url}; -use crate::{Channel, CommentsInfo, Playlist, RelatedStream, StreamsPage, VideoInfo}; +use crate::{Channel, CommentsInfo, Playlist, RelatedStream, Result, StreamsPage, VideoInfo}; pub struct PipedClient { pub httpclient: Client, pub instance: String, } -const USER_AGENT: &'static str = "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0"; +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: S) -> PipedClient { @@ -17,23 +18,25 @@ impl PipedClient { } } - pub async fn trending>( - &self, - region: S, - ) -> Result, Box> { + pub async fn trending>(&self, region: S) -> Result> { let mut url = Url::parse(format!("{}/trending", &self.instance).as_str())?; url.query_pairs_mut().append_pair("region", region.as_ref()); - let resp = &self.httpclient.get(url) + let resp = &self + .httpclient + .get(url) .header("User-Agent", USER_AGENT) - .send().await?.text().await?; + .send() + .await? + .text() + .await?; let streams: Vec = serde_json::from_str(resp.as_ref())?; Ok(streams) } - pub async fn channel_from_id>(&self, id: S) -> Result> { + pub async fn channel_from_id>(&self, id: S) -> Result { let resp = &self .httpclient .get(format!("{}/channel/{}", &self.instance, id.as_ref())) @@ -53,25 +56,28 @@ impl PipedClient { id: S, nexturl: S, nextbody: S, - ) -> Result> { - let mut url = Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id.as_ref()).as_str())?; + ) -> Result { + let mut url = + Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id.as_ref()).as_str())?; url.query_pairs_mut() .append_pair("url", nexturl.as_ref()) .append_pair("id", nextbody.as_ref()); - let resp = &self.httpclient.get(url) + let resp = &self + .httpclient + .get(url) .header("User-Agent", USER_AGENT) - .send().await?.text().await?; + .send() + .await? + .text() + .await?; let streams: StreamsPage = serde_json::from_str(resp.as_ref())?; Ok(streams) } - pub async fn playlist_from_id>( - &self, - id: S, - ) -> Result> { + pub async fn playlist_from_id>(&self, id: S) -> Result { let resp = &self .httpclient .get(format!("{}/playlists/{}", &self.instance, id.as_ref())) @@ -91,22 +97,28 @@ impl PipedClient { id: S, nexturl: S, nextbody: S, - ) -> Result> { - let mut url = Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id.as_ref()).as_str())?; + ) -> Result { + let mut url = + Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id.as_ref()).as_str())?; url.query_pairs_mut() .append_pair("url", nexturl.as_ref()) .append_pair("id", nextbody.as_ref()); - let resp = &self.httpclient.get(url) + let resp = &self + .httpclient + .get(url) .header("User-Agent", USER_AGENT) - .send().await?.text().await?; + .send() + .await? + .text() + .await?; let streams: StreamsPage = serde_json::from_str(resp.as_str())?; Ok(streams) } - pub async fn video_from_id>(&self, id: S) -> Result> { + pub async fn video_from_id>(&self, id: S) -> Result { let resp = &self .httpclient .get(format!("{}/streams/{}", &self.instance, id.as_ref())) @@ -121,26 +133,25 @@ impl PipedClient { Ok(video) } - pub async fn search_suggestions>( - &self, - q: S, - ) -> Result, Box> { + pub async fn search_suggestions>(&self, q: S) -> Result> { let mut url = Url::parse(format!("{}/suggestions", &self.instance).as_str())?; url.query_pairs_mut().append_pair("query", q.as_ref()); - let resp = &self.httpclient.get(url) + let resp = &self + .httpclient + .get(url) .header("User-Agent", USER_AGENT) - .send().await?.text().await?; + .send() + .await? + .text() + .await?; let suggestions: Vec = serde_json::from_str(resp.as_ref())?; Ok(suggestions) } - pub async fn comments_from_id>( - &self, - id: S, - ) -> Result> { + pub async fn comments_from_id>(&self, id: S) -> Result { let resp = &self .httpclient .get(format!("{}/comments/{}", &self.instance, id.as_ref())) @@ -159,13 +170,19 @@ impl PipedClient { &self, id: S, nexturl: S, - ) -> Result> { - let mut url = Url::parse(format!("{}/nextpage/comments/{}", &self.instance, id.as_ref()).as_str())?; + ) -> Result { + 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) + let resp = &self + .httpclient + .get(url) .header("User-Agent", USER_AGENT) - .send().await?.text().await?; + .send() + .await? + .text() + .await?; let comments: CommentsInfo = serde_json::from_str(resp.as_ref())?; diff --git a/piped/src/error.rs b/piped/src/error.rs new file mode 100644 index 0000000..7e71a96 --- /dev/null +++ b/piped/src/error.rs @@ -0,0 +1,13 @@ +use thiserror::Error; + +pub type Result = std::result::Result; + +#[derive(Error, Debug)] +pub enum Error { + #[error("{0}")] + Network(#[from] reqwest::Error), + #[error("{0}")] + ParseResponse(#[from] serde_json::error::Error), + #[error("{0}")] + Parseurl(#[from] url::ParseError), +} diff --git a/piped/src/lib.rs b/piped/src/lib.rs index ea36297..4575039 100644 --- a/piped/src/lib.rs +++ b/piped/src/lib.rs @@ -1,5 +1,7 @@ mod client; +mod error; mod structure; pub use client::PipedClient; +pub use error::{Error, Result}; pub use structure::*;