diff --git a/examples/fetch_accounts.rs b/examples/fetch_accounts.rs index 3250a43..eee2c5c 100644 --- a/examples/fetch_accounts.rs +++ b/examples/fetch_accounts.rs @@ -1,11 +1,9 @@ use freedom_api::prelude::*; -use freedom_config::Config; use futures::StreamExt; #[tokio::main] async fn main() -> Result<(), Box> { - let config = Config::from_env()?; - let client = Client::from_config(config); + let client = Client::from_env()?; let accounts = client .get_accounts() .filter_map(|result| async move { result.ok() }) @@ -13,7 +11,7 @@ async fn main() -> Result<(), Box> { .collect::>() .await; - println!("{:?}", accounts); + println!("{:#?}", accounts); Ok(()) } diff --git a/src/api.rs b/src/api.rs index bd4849e..4ddf62f 100644 --- a/src/api.rs +++ b/src/api.rs @@ -23,14 +23,14 @@ use freedom_models::{ utils::Embedded, }; use reqwest::{Response, StatusCode}; -use serde::{de::DeserializeOwned, Serialize}; +use serde::de::DeserializeOwned; use serde_json::Value; use time::{format_description::well_known::Iso8601, OffsetDateTime}; use url::Url; use futures_core::Stream; -use crate::{error::Error, prelude::RuntimeError}; +use crate::error::Error; pub(crate) mod post; @@ -1185,11 +1185,9 @@ pub trait FreedomApi: Send + Sync { value .get("token") - .ok_or(RuntimeError::Response(String::from("Missing token field")))? + .ok_or(Error::Response(String::from("Missing token field")))? .as_str() - .ok_or(RuntimeError::Response(String::from( - "Invalid type for token", - ))) + .ok_or(Error::Response(String::from("Invalid type for token"))) .map(|s| s.to_owned()) .map_err(From::from) } @@ -1210,11 +1208,9 @@ pub trait FreedomApi: Send + Sync { value .get("token") - .ok_or(RuntimeError::Response(String::from("Missing token field")))? + .ok_or(Error::Response(String::from("Missing token field")))? .as_str() - .ok_or(RuntimeError::Response(String::from( - "Invalid type for token", - ))) + .ok_or(Error::Response(String::from("Invalid type for token"))) .map(|s| s.to_owned()) .map_err(From::from) } @@ -1231,7 +1227,7 @@ pub trait FreedomApi: Send + Sync { fn error_on_non_success(status: &StatusCode) -> Result<(), Error> { if !status.is_success() { - return Err(Error::Runtime(RuntimeError::Response(status.to_string()))); + return Err(Error::Response(status.to_string())); } Ok(()) diff --git a/src/caching_client.rs b/src/caching_client.rs index 04f2eb9..3e70d00 100644 --- a/src/caching_client.rs +++ b/src/caching_client.rs @@ -48,8 +48,6 @@ impl FreedomApi for CachingClient { #[tracing::instrument] async fn get(&self, url: Url) -> Result<(Bytes, StatusCode), Error> { - use crate::error::RuntimeError; - // This is a rather cheap clone. Something like 50 bytes. This is necessary since we will // be passing this to the tokio executor which has lifetime requirements of `'static` let client = &self.inner; @@ -59,7 +57,7 @@ impl FreedomApi for CachingClient { let (body, status) = client.get(url).await?; if !status.is_success() { - return Err(Error::Runtime(RuntimeError::Response(status.to_string()))); + return Err(Error::Response(status.to_string())); } Ok((body, status)) diff --git a/src/error.rs b/src/error.rs index 38bca20..22010c4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,63 +1,8 @@ -use serde::{Deserialize, Serialize}; +use serde::Serialize; /// The combined error type for the client builder and for API errors #[derive(Debug, Clone, thiserror::Error, PartialEq, Eq, Serialize)] pub enum Error { - #[error("Builder error: {0}")] - Builder(BuilderError), - - #[error("Runtime error: {0}")] - Runtime(RuntimeError), -} - -impl Error { - /// Shorthand for creating a runtime pagination error - pub(crate) fn pag_item(s: String) -> Self { - From::from(RuntimeError::PaginationItemDeserialization(s)) - } -} - -#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq, Deserialize, Serialize)] -pub enum BuilderError { - #[error("Failed to build client from environment variables, {0} is missing.")] - MissingEnv(String), - - #[error("Failed to build client from the provided environment file, could not find {0}")] - InvalidEnvPath(String), - - #[error("Failed to build client, no username was provided")] - MissingUsername, - - #[error("Failed to build client, no password was provided")] - MissingPassword, - - #[error("Failed to build request, need task type")] - TaskType, - - #[error("Failed to build request, need account ID")] - AccountId, - - #[error("Failed to build request, need minimum duration")] - Duration, - - #[error("Failed to build request, need target date")] - TargetDate, - - #[error("Failed to build request, configuration ID")] - ConfigurationId, - - #[error("Failed to build request, need site ID")] - SiteId, - - #[error("Failed to build request, need satellite ID")] - SatelliteId, - - #[error("Failed to build client, underlying client failed to build with: {0}")] - ClientBuild(String), -} - -#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq, Deserialize, Serialize)] -pub enum RuntimeError { #[error("Failed to get valid response from server: {0}")] Response(String), @@ -88,45 +33,39 @@ pub enum RuntimeError { InvalidId, } -impl From for Error { - fn from(value: RuntimeError) -> Self { - Error::Runtime(value) - } -} - -impl From for Error { - fn from(value: BuilderError) -> Self { - Error::Builder(value) +impl Error { + /// Shorthand for creating a runtime pagination error + pub(crate) fn pag_item(s: String) -> Self { + Self::PaginationItemDeserialization(s) } } impl From for Error { fn from(value: reqwest::Error) -> Self { - println!("{:?}", value); - Error::Runtime(RuntimeError::Response(value.to_string())) + Error::Response(value.to_string()) } } impl From for Error { fn from(value: serde_json::Error) -> Self { - Error::Runtime(RuntimeError::Deserialization(value.to_string())) + Error::Deserialization(value.to_string()) } } impl From for Error { fn from(value: time::error::Error) -> Self { - Error::Runtime(RuntimeError::TimeFormatError(value.to_string())) + Error::TimeFormatError(value.to_string()) } } impl From for Error { fn from(value: time::error::Format) -> Self { - Error::Runtime(RuntimeError::TimeFormatError(value.to_string())) + Error::TimeFormatError(value.to_string()) } } impl From for Error { fn from(value: url::ParseError) -> Self { - Self::Runtime(RuntimeError::InvalidUri(value.to_string())) + Self::InvalidUri(value.to_string()) } } diff --git a/src/extensions/mod.rs b/src/extensions/mod.rs index a076b39..ce512d4 100644 --- a/src/extensions/mod.rs +++ b/src/extensions/mod.rs @@ -33,19 +33,15 @@ pub use { fn get_id(reference: &'static str, links: &HashMap) -> Result { let url = links .get(reference) - .ok_or(crate::error::RuntimeError::MissingUri(reference))?; + .ok_or(crate::error::Error::MissingUri(reference))?; let id_str = url .path_segments() - .ok_or(crate::error::RuntimeError::InvalidUri( - "Missing Path".into(), - ))? + .ok_or(crate::error::Error::InvalidUri("Missing Path".into()))? .last() .unwrap(); - id_str - .parse() - .map_err(|_| From::from(crate::error::RuntimeError::InvalidId)) + id_str.parse().map_err(|_| crate::error::Error::InvalidId) } async fn get_item( @@ -59,7 +55,7 @@ where { let uri = links .get(reference) - .ok_or(crate::error::RuntimeError::MissingUri(reference))? + .ok_or(crate::error::Error::MissingUri(reference))? .clone(); client.get_json_map(uri).await diff --git a/src/lib.rs b/src/lib.rs index 0da40e4..82d7706 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,10 +23,7 @@ pub mod prelude { #[cfg(feature = "caching")] pub use crate::caching_client::CachingClient; pub use crate::extensions::*; - pub use crate::{ - client::Client, - error::{BuilderError, Error, RuntimeError}, - }; + pub use crate::{client::Client, error::Error}; pub use freedom_models::{ account::*, satellite::*, satellite_configuration::*, site::*, task::*, user::*, };