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() {
|
||||
let httpclient = Client::new();
|
||||
|
||||
let client = PipedClient::new(&httpclient, instance);
|
||||
let client = PipedClient::new(&httpclient, INSTANCE);
|
||||
|
||||
let suggestions = client
|
||||
.search_suggestions("techlore".to_string())
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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<S: AsRef<str>>(httpclient: &Client, instance: S) -> PipedClient {
|
||||
|
@ -17,23 +18,25 @@ impl PipedClient {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn trending<S: AsRef<str>>(
|
||||
&self,
|
||||
region: S,
|
||||
) -> Result<Vec<RelatedStream>, Box<dyn std::error::Error>> {
|
||||
pub async fn trending<S: AsRef<str>>(&self, region: S) -> Result<Vec<RelatedStream>> {
|
||||
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<RelatedStream> = serde_json::from_str(resp.as_ref())?;
|
||||
|
||||
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
|
||||
.httpclient
|
||||
.get(format!("{}/channel/{}", &self.instance, id.as_ref()))
|
||||
|
@ -53,25 +56,28 @@ impl PipedClient {
|
|||
id: S,
|
||||
nexturl: S,
|
||||
nextbody: S,
|
||||
) -> Result<StreamsPage, Box<dyn std::error::Error>> {
|
||||
let mut url = Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id.as_ref()).as_str())?;
|
||||
) -> Result<StreamsPage> {
|
||||
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<S: AsRef<str>>(
|
||||
&self,
|
||||
id: S,
|
||||
) -> Result<Playlist, Box<dyn std::error::Error>> {
|
||||
pub async fn playlist_from_id<S: AsRef<str>>(&self, id: S) -> Result<Playlist> {
|
||||
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<StreamsPage, Box<dyn std::error::Error>> {
|
||||
let mut url = Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id.as_ref()).as_str())?;
|
||||
) -> Result<StreamsPage> {
|
||||
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<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
|
||||
.httpclient
|
||||
.get(format!("{}/streams/{}", &self.instance, id.as_ref()))
|
||||
|
@ -121,26 +133,25 @@ impl PipedClient {
|
|||
Ok(video)
|
||||
}
|
||||
|
||||
pub async fn search_suggestions<S: AsRef<str>>(
|
||||
&self,
|
||||
q: S,
|
||||
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
|
||||
pub async fn search_suggestions<S: AsRef<str>>(&self, q: S) -> Result<Vec<String>> {
|
||||
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<String> = serde_json::from_str(resp.as_ref())?;
|
||||
|
||||
Ok(suggestions)
|
||||
}
|
||||
|
||||
pub async fn comments_from_id<S: AsRef<str>>(
|
||||
&self,
|
||||
id: S,
|
||||
) -> Result<CommentsInfo, Box<dyn std::error::Error>> {
|
||||
pub async fn comments_from_id<S: AsRef<str>>(&self, id: S) -> Result<CommentsInfo> {
|
||||
let resp = &self
|
||||
.httpclient
|
||||
.get(format!("{}/comments/{}", &self.instance, id.as_ref()))
|
||||
|
@ -159,13 +170,19 @@ impl PipedClient {
|
|||
&self,
|
||||
id: S,
|
||||
nexturl: S,
|
||||
) -> Result<CommentsInfo, Box<dyn std::error::Error>> {
|
||||
let mut url = Url::parse(format!("{}/nextpage/comments/{}", &self.instance, id.as_ref()).as_str())?;
|
||||
) -> Result<CommentsInfo> {
|
||||
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())?;
|
||||
|
||||
|
|
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 error;
|
||||
mod structure;
|
||||
|
||||
pub use client::PipedClient;
|
||||
pub use error::{Error, Result};
|
||||
pub use structure::*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue