aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2024-01-12 17:27:29 +0100
committerHarald Eilertsen <haraldei@anduin.net>2024-01-12 18:58:19 +0100
commitbe04fb276aa4777f5477c579fc82f1cef9b18d65 (patch)
tree58c884d9f78245e91cb0903179bb1bac09cdfe5d /src
parent5349a0c4320949d803df0dd8596ba8aa5497c81c (diff)
downloadrust-zotapi-be04fb276aa4777f5477c579fc82f1cef9b18d65.tar.gz
rust-zotapi-be04fb276aa4777f5477c579fc82f1cef9b18d65.tar.bz2
rust-zotapi-be04fb276aa4777f5477c579fc82f1cef9b18d65.zip
Make channel_stream return error on auth failure
Also reenables the relevant tests.
Diffstat (limited to 'src')
-rw-r--r--src/error.rs30
-rw-r--r--src/lib.rs1
-rw-r--r--src/zotapi.rs15
3 files changed, 42 insertions, 4 deletions
diff --git a/src/error.rs b/src/error.rs
index 2954bea..23cc452 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -15,7 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use reqwest;
-use std;
+use std::fmt::Display;
#[derive(Debug)]
pub enum Error {
@@ -29,12 +29,40 @@ pub enum Error {
Unknown,
}
+impl Display for Error {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ Self::APIError(s) => write!(f, "APIError: {}", s)?,
+ Self::Http(e) => write!(f, "Http: {}", e.to_string())?,
+ Self::Io(e) => write!(f, "Io: {}", e.to_string())?,
+ Self::Json(e) => write!(f, "Json: {}", e.to_string())?,
+ Self::ParseError(e) => write!(f, "URL: {}", e.to_string())?,
+ Self::Qs(e) => write!(f, "Qs: {}", e.to_string())?,
+ Self::Unauthorized => write!(f, "unauthorized")?,
+ _ => write!(f, "unknown error")?,
+ };
+
+ Ok(())
+ }
+}
+
+impl std::error::Error for Error {}
+
impl From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Error {
Error::Http(e)
}
}
+impl From<reqwest::StatusCode> for Error {
+ fn from(s: reqwest::StatusCode) -> Error {
+ match s {
+ reqwest::StatusCode::UNAUTHORIZED => Error::Unauthorized,
+ _ => Error::Unknown,
+ }
+ }
+}
+
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
Error::Io(e)
diff --git a/src/lib.rs b/src/lib.rs
index d1df0ad..8f94a8d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -42,6 +42,7 @@ pub use verify::Channel;
pub use xchan::XChan;
// pub use zotapi::ZotAPI;
pub use zotapi::new;
+pub use zotapi::ZotApi;
#[cfg(test)]
mod tests {
diff --git a/src/zotapi.rs b/src/zotapi.rs
index 5a2f5cc..0a5c317 100644
--- a/src/zotapi.rs
+++ b/src/zotapi.rs
@@ -14,7 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use crate::Channel;
+use crate::{
+ Channel,
+ Error,
+};
use url::Url;
use reqwest::{
self,
@@ -73,8 +76,14 @@ impl ZotApi {
/**
* Return the channel stream as json.
*/
- pub async fn channel_stream(&self) -> Result<String, Box<dyn std::error::Error>> {
- Ok(self.get("channel/stream").send().await?.text().await?)
+ pub async fn channel_stream(&self) -> Result<String, Error> {
+ let response = self.get("channel/stream").send().await?;
+
+ if response.status().is_success() {
+ Ok(response.text().await?)
+ } else {
+ Err(response.status().into())
+ }
}
/**