mirror of
				https://github.com/TeamPiped/piped-rust-sdk.git
				synced 2024-08-14 23:56:06 +00:00 
			
		
		
		
	Improved error handling
This commit is contained in:
		
							parent
							
								
									431bf31fcc
								
							
						
					
					
						commit
						973fc966bb
					
				
					 5 changed files with 72 additions and 37 deletions
				
			
		| 
						 | 
					@ -7,7 +7,7 @@ const INSTANCE: &'static str = "https://pipedapi.kavin.rocks";
 | 
				
			||||||
async fn main() {
 | 
					async fn main() {
 | 
				
			||||||
    let httpclient = Client::new();
 | 
					    let httpclient = Client::new();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let client = PipedClient::new(&httpclient, instance);
 | 
					    let client = PipedClient::new(&httpclient, INSTANCE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let suggestions = client
 | 
					    let suggestions = client
 | 
				
			||||||
        .search_suggestions("techlore".to_string())
 | 
					        .search_suggestions("techlore".to_string())
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,5 +12,8 @@ version = "0.0.0"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[dependencies]
 | 
					[dependencies]
 | 
				
			||||||
reqwest = "^0.11"
 | 
					reqwest = "^0.11"
 | 
				
			||||||
 | 
					url = "^2.2"
 | 
				
			||||||
serde = {version = "^1.0", features = ["derive"]}
 | 
					serde = {version = "^1.0", features = ["derive"]}
 | 
				
			||||||
serde_json = "^1.0"
 | 
					serde_json = "^1.0"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					thiserror = "^1.0"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,13 +1,14 @@
 | 
				
			||||||
use reqwest::{Client, Url};
 | 
					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 struct PipedClient {
 | 
				
			||||||
    pub httpclient: Client,
 | 
					    pub httpclient: Client,
 | 
				
			||||||
    pub instance: String,
 | 
					    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 {
 | 
					impl PipedClient {
 | 
				
			||||||
    pub fn new<S: AsRef<str>>(httpclient: &Client, instance: S) -> PipedClient {
 | 
					    pub fn new<S: AsRef<str>>(httpclient: &Client, instance: S) -> PipedClient {
 | 
				
			||||||
| 
						 | 
					@ -17,23 +18,25 @@ impl PipedClient {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub async fn trending<S: AsRef<str>>(
 | 
					    pub async fn trending<S: AsRef<str>>(&self, region: S) -> Result<Vec<RelatedStream>> {
 | 
				
			||||||
        &self,
 | 
					 | 
				
			||||||
        region: S,
 | 
					 | 
				
			||||||
    ) -> Result<Vec<RelatedStream>, Box<dyn std::error::Error>> {
 | 
					 | 
				
			||||||
        let mut url = Url::parse(format!("{}/trending", &self.instance).as_str())?;
 | 
					        let mut url = Url::parse(format!("{}/trending", &self.instance).as_str())?;
 | 
				
			||||||
        url.query_pairs_mut().append_pair("region", region.as_ref());
 | 
					        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)
 | 
					            .header("User-Agent", USER_AGENT)
 | 
				
			||||||
            .send().await?.text().await?;
 | 
					            .send()
 | 
				
			||||||
 | 
					            .await?
 | 
				
			||||||
 | 
					            .text()
 | 
				
			||||||
 | 
					            .await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let streams: Vec<RelatedStream> = serde_json::from_str(resp.as_ref())?;
 | 
					        let streams: Vec<RelatedStream> = serde_json::from_str(resp.as_ref())?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Ok(streams)
 | 
					        Ok(streams)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub async fn channel_from_id<S: AsRef<str>>(&self, id: S) -> Result<Channel, Box<dyn std::error::Error>> {
 | 
					    pub async fn channel_from_id<S: AsRef<str>>(&self, id: S) -> Result<Channel> {
 | 
				
			||||||
        let resp = &self
 | 
					        let resp = &self
 | 
				
			||||||
            .httpclient
 | 
					            .httpclient
 | 
				
			||||||
            .get(format!("{}/channel/{}", &self.instance, id.as_ref()))
 | 
					            .get(format!("{}/channel/{}", &self.instance, id.as_ref()))
 | 
				
			||||||
| 
						 | 
					@ -53,25 +56,28 @@ impl PipedClient {
 | 
				
			||||||
        id: S,
 | 
					        id: S,
 | 
				
			||||||
        nexturl: S,
 | 
					        nexturl: S,
 | 
				
			||||||
        nextbody: S,
 | 
					        nextbody: S,
 | 
				
			||||||
    ) -> Result<StreamsPage, Box<dyn std::error::Error>> {
 | 
					    ) -> Result<StreamsPage> {
 | 
				
			||||||
        let mut url = Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id.as_ref()).as_str())?;
 | 
					        let mut url =
 | 
				
			||||||
 | 
					            Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id.as_ref()).as_str())?;
 | 
				
			||||||
        url.query_pairs_mut()
 | 
					        url.query_pairs_mut()
 | 
				
			||||||
            .append_pair("url", nexturl.as_ref())
 | 
					            .append_pair("url", nexturl.as_ref())
 | 
				
			||||||
            .append_pair("id", nextbody.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)
 | 
					            .header("User-Agent", USER_AGENT)
 | 
				
			||||||
            .send().await?.text().await?;
 | 
					            .send()
 | 
				
			||||||
 | 
					            .await?
 | 
				
			||||||
 | 
					            .text()
 | 
				
			||||||
 | 
					            .await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let streams: StreamsPage = serde_json::from_str(resp.as_ref())?;
 | 
					        let streams: StreamsPage = serde_json::from_str(resp.as_ref())?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Ok(streams)
 | 
					        Ok(streams)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub async fn playlist_from_id<S: AsRef<str>>(
 | 
					    pub async fn playlist_from_id<S: AsRef<str>>(&self, id: S) -> Result<Playlist> {
 | 
				
			||||||
        &self,
 | 
					 | 
				
			||||||
        id: S,
 | 
					 | 
				
			||||||
    ) -> Result<Playlist, Box<dyn std::error::Error>> {
 | 
					 | 
				
			||||||
        let resp = &self
 | 
					        let resp = &self
 | 
				
			||||||
            .httpclient
 | 
					            .httpclient
 | 
				
			||||||
            .get(format!("{}/playlists/{}", &self.instance, id.as_ref()))
 | 
					            .get(format!("{}/playlists/{}", &self.instance, id.as_ref()))
 | 
				
			||||||
| 
						 | 
					@ -91,22 +97,28 @@ impl PipedClient {
 | 
				
			||||||
        id: S,
 | 
					        id: S,
 | 
				
			||||||
        nexturl: S,
 | 
					        nexturl: S,
 | 
				
			||||||
        nextbody: S,
 | 
					        nextbody: S,
 | 
				
			||||||
    ) -> Result<StreamsPage, Box<dyn std::error::Error>> {
 | 
					    ) -> Result<StreamsPage> {
 | 
				
			||||||
        let mut url = Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id.as_ref()).as_str())?;
 | 
					        let mut url =
 | 
				
			||||||
 | 
					            Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id.as_ref()).as_str())?;
 | 
				
			||||||
        url.query_pairs_mut()
 | 
					        url.query_pairs_mut()
 | 
				
			||||||
            .append_pair("url", nexturl.as_ref())
 | 
					            .append_pair("url", nexturl.as_ref())
 | 
				
			||||||
            .append_pair("id", nextbody.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)
 | 
					            .header("User-Agent", USER_AGENT)
 | 
				
			||||||
            .send().await?.text().await?;
 | 
					            .send()
 | 
				
			||||||
 | 
					            .await?
 | 
				
			||||||
 | 
					            .text()
 | 
				
			||||||
 | 
					            .await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let streams: StreamsPage = serde_json::from_str(resp.as_str())?;
 | 
					        let streams: StreamsPage = serde_json::from_str(resp.as_str())?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Ok(streams)
 | 
					        Ok(streams)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub async fn video_from_id<S: AsRef<str>>(&self, id: S) -> Result<VideoInfo, Box<dyn std::error::Error>> {
 | 
					    pub async fn video_from_id<S: AsRef<str>>(&self, id: S) -> Result<VideoInfo> {
 | 
				
			||||||
        let resp = &self
 | 
					        let resp = &self
 | 
				
			||||||
            .httpclient
 | 
					            .httpclient
 | 
				
			||||||
            .get(format!("{}/streams/{}", &self.instance, id.as_ref()))
 | 
					            .get(format!("{}/streams/{}", &self.instance, id.as_ref()))
 | 
				
			||||||
| 
						 | 
					@ -121,26 +133,25 @@ impl PipedClient {
 | 
				
			||||||
        Ok(video)
 | 
					        Ok(video)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub async fn search_suggestions<S: AsRef<str>>(
 | 
					    pub async fn search_suggestions<S: AsRef<str>>(&self, q: S) -> Result<Vec<String>> {
 | 
				
			||||||
        &self,
 | 
					 | 
				
			||||||
        q: S,
 | 
					 | 
				
			||||||
    ) -> Result<Vec<String>, Box<dyn std::error::Error>> {
 | 
					 | 
				
			||||||
        let mut url = Url::parse(format!("{}/suggestions", &self.instance).as_str())?;
 | 
					        let mut url = Url::parse(format!("{}/suggestions", &self.instance).as_str())?;
 | 
				
			||||||
        url.query_pairs_mut().append_pair("query", q.as_ref());
 | 
					        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)
 | 
					            .header("User-Agent", USER_AGENT)
 | 
				
			||||||
            .send().await?.text().await?;
 | 
					            .send()
 | 
				
			||||||
 | 
					            .await?
 | 
				
			||||||
 | 
					            .text()
 | 
				
			||||||
 | 
					            .await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let suggestions: Vec<String> = serde_json::from_str(resp.as_ref())?;
 | 
					        let suggestions: Vec<String> = serde_json::from_str(resp.as_ref())?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Ok(suggestions)
 | 
					        Ok(suggestions)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub async fn comments_from_id<S: AsRef<str>>(
 | 
					    pub async fn comments_from_id<S: AsRef<str>>(&self, id: S) -> Result<CommentsInfo> {
 | 
				
			||||||
        &self,
 | 
					 | 
				
			||||||
        id: S,
 | 
					 | 
				
			||||||
    ) -> Result<CommentsInfo, Box<dyn std::error::Error>> {
 | 
					 | 
				
			||||||
        let resp = &self
 | 
					        let resp = &self
 | 
				
			||||||
            .httpclient
 | 
					            .httpclient
 | 
				
			||||||
            .get(format!("{}/comments/{}", &self.instance, id.as_ref()))
 | 
					            .get(format!("{}/comments/{}", &self.instance, id.as_ref()))
 | 
				
			||||||
| 
						 | 
					@ -159,13 +170,19 @@ impl PipedClient {
 | 
				
			||||||
        &self,
 | 
					        &self,
 | 
				
			||||||
        id: S,
 | 
					        id: S,
 | 
				
			||||||
        nexturl: S,
 | 
					        nexturl: S,
 | 
				
			||||||
    ) -> Result<CommentsInfo, Box<dyn std::error::Error>> {
 | 
					    ) -> Result<CommentsInfo> {
 | 
				
			||||||
        let mut url = Url::parse(format!("{}/nextpage/comments/{}", &self.instance, id.as_ref()).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());
 | 
					        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)
 | 
					            .header("User-Agent", USER_AGENT)
 | 
				
			||||||
            .send().await?.text().await?;
 | 
					            .send()
 | 
				
			||||||
 | 
					            .await?
 | 
				
			||||||
 | 
					            .text()
 | 
				
			||||||
 | 
					            .await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let comments: CommentsInfo = serde_json::from_str(resp.as_ref())?;
 | 
					        let comments: CommentsInfo = serde_json::from_str(resp.as_ref())?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										13
									
								
								piped/src/error.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								piped/src/error.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,13 @@
 | 
				
			||||||
 | 
					use thiserror::Error;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub type Result<T> = std::result::Result<T, Error>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[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),
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,7 @@
 | 
				
			||||||
mod client;
 | 
					mod client;
 | 
				
			||||||
 | 
					mod error;
 | 
				
			||||||
mod structure;
 | 
					mod structure;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub use client::PipedClient;
 | 
					pub use client::PipedClient;
 | 
				
			||||||
 | 
					pub use error::{Error, Result};
 | 
				
			||||||
pub use structure::*;
 | 
					pub use structure::*;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue