From 63b4afd7a8aa3bb8d984da51633af0417598b3f8 Mon Sep 17 00:00:00 2001 From: Niel Drummond Date: Fri, 18 Oct 2024 11:17:59 +0100 Subject: [PATCH] chore: Cleanup error handling, visibility --- src/adapters/mod.rs | 16 ++++++++-------- src/adapters/reqwest.rs | 15 ++++++++------- src/adapters/ureq.rs | 17 ++++++++--------- src/adapters/wasm.rs | 17 +++++++++-------- src/lib.rs | 4 ++-- tests/simple.rs | 18 +++++++++--------- tests/sync.rs | 22 +++++++++++----------- 7 files changed, 55 insertions(+), 54 deletions(-) diff --git a/src/adapters/mod.rs b/src/adapters/mod.rs index af07e9d..3aad6cd 100644 --- a/src/adapters/mod.rs +++ b/src/adapters/mod.rs @@ -95,7 +95,7 @@ where } } -pub(crate) trait FromJson +pub trait FromJson where A: ser::Serialize, { @@ -103,21 +103,21 @@ where } #[allow(dead_code)] -pub(crate) struct GitHubRequest { +pub struct GitHubRequest { pub uri: String, pub method: &'static str, pub body: Option, pub headers: Vec<(&'static str, &'static str)>, } -pub(crate) trait GitHubRequestBuilder +pub trait GitHubRequestBuilder where Self: Sized, { fn build(req: GitHubRequest, client: &C) -> Result; } -pub(crate) trait GitHubResponseExt { +pub trait GitHubResponseExt { fn is_success(&self) -> bool; fn status_code(&self) -> u16; fn to_json Deserialize<'de> + std::fmt::Debug>(self) -> Result; @@ -130,7 +130,7 @@ pub(crate) trait GitHubResponseExt { pub trait Client { type Req; - fn new(auth: &Auth) -> Self + fn new(auth: &Auth) -> Result where Self: Sized; fn get_auth(&self) -> &Auth; @@ -140,14 +140,14 @@ pub trait Client { } #[cfg(target_arch = "wasm32")] -pub fn client(auth: &Auth) -> impl Client { +pub fn client(auth: &Auth) -> Result, AdapterError> { wasm::Client::new(auth) } #[cfg(feature = "ureq")] -pub fn client(auth: &Auth) -> impl Client { +pub fn client(auth: &Auth) -> Result, AdapterError> { ureq::Client::new(auth) } #[cfg(feature = "reqwest")] -pub fn client(auth: &Auth) -> impl Client { +pub fn client(auth: &Auth) -> Result, AdapterError> { reqwest::Client::new(auth) } diff --git a/src/adapters/reqwest.rs b/src/adapters/reqwest.rs index 63ccc03..e40146f 100644 --- a/src/adapters/reqwest.rs +++ b/src/adapters/reqwest.rs @@ -19,16 +19,18 @@ pub enum AdapterError { Reqwest(#[from] reqwest::Error), #[error(transparent)] IOError(#[from] std::io::Error), + #[error("Reqwest adapter only has async fetch implemented")] + UnimplementedSync, } impl super::Client for Client { type Req = ::reqwest::Request; - fn new(auth: &Auth) -> Self { - Self { + fn new(auth: &Auth) -> Result { + Ok(Self { auth: auth.to_owned(), pool: reqwest::Client::new(), - } + }) } fn get_auth(&self) -> &Auth { @@ -36,8 +38,7 @@ impl super::Client for Client { } fn fetch(&self, _request: Self::Req) -> Result { - unimplemented!("Reqwest adapter only has async fetch implemented"); - Err::(std::io::Error::new(std::io::ErrorKind::Other, "oh no!").into()) + Err::(AdapterError::UnimplementedSync) } async fn fetch_async( @@ -53,8 +54,8 @@ impl super::Client for Client { } pub struct Client { - pub(crate) auth: Auth, - pub(crate) pool: reqwest::Client, + auth: Auth, + pool: reqwest::Client, } impl GitHubResponseExt for Response { diff --git a/src/adapters/ureq.rs b/src/adapters/ureq.rs index 448c7db..cef7591 100644 --- a/src/adapters/ureq.rs +++ b/src/adapters/ureq.rs @@ -31,19 +31,21 @@ pub enum AdapterError { Serde(#[from] serde_json::Error), #[error(transparent)] IOError(#[from] std::io::Error), + #[error("Ureq adapter only has sync fetch implemented")] + UnimplementedAsync, } impl super::Client for Client { type Req = RequestWithBody; - fn new(auth: &Auth) -> Self { - Self { + fn new(auth: &Auth) -> Result { + Ok(Self { auth: auth.to_owned(), agent: Agent::new_with_config(ureq::Config { http_status_as_error: false, ..Default::default() }), - } + }) } fn get_auth(&self) -> &Auth { @@ -67,16 +69,13 @@ impl super::Client for Client { &self, request: Self::Req, ) -> Result { - unimplemented!("Ureq adapter only has sync fetch implemented"); - Err::, _>( - std::io::Error::new(std::io::ErrorKind::Other, "oh no!").into(), - ) + Err::, _>(AdapterError::UnimplementedAsync) } } pub struct Client { - pub(crate) auth: Auth, - pub(crate) agent: Agent, + auth: Auth, + agent: Agent, } impl GitHubResponseExt for Response { diff --git a/src/adapters/wasm.rs b/src/adapters/wasm.rs index 68b69ef..2bd182b 100644 --- a/src/adapters/wasm.rs +++ b/src/adapters/wasm.rs @@ -48,6 +48,8 @@ pub enum AdapterError { Serde(#[from] serde_json::Error), #[error(transparent)] IOError(#[from] std::io::Error), + #[error("Wasm adapter only has async fetch implemented")] + UnimplementedSync, } #[derive(thiserror::Error, Debug)] @@ -67,11 +69,11 @@ pub struct ObjectError { impl super::Client for Client { type Req = web_sys::Request; - fn new(auth: &Auth) -> Self { - Self { + fn new(auth: &Auth) -> Result { + Ok(Self { auth: auth.to_owned(), - scope: scope().expect("Cannot fetch wasm scope"), - } + scope: scope()?, + }) } fn get_auth(&self) -> &Auth { @@ -79,8 +81,7 @@ impl super::Client for Client { } fn fetch(&self, _request: Self::Req) -> Result { - unimplemented!("Wasm adapter only has async fetch implemented"); - Err::(std::io::Error::new(std::io::ErrorKind::Other, "oh no!").into()) + Err::(AdapterError::UnimplementedSync) } async fn fetch_async( @@ -101,8 +102,8 @@ impl super::Client for Client { } pub struct Client { - pub(crate) auth: Auth, - pub(crate) scope: Box, + auth: Auth, + scope: Box, } impl From for AdapterError { diff --git a/src/lib.rs b/src/lib.rs index ac72829..3721fc3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ //! //! ```nocompile //! [dependencies] -//! roctogen = "0.12" +//! roctogen = "*" //! ``` //! //! # API @@ -78,7 +78,7 @@ //! use roctogen::{adapters::client, auth::Auth}; //! //! let auth = Auth::None; -//! let client = client(&auth); +//! let client = client(&auth).expect("Cannot create client"); //! let per_page = api::PerPage::new(10); //! //! let mut params: repos::ReposListCommitsParams = per_page.as_ref().into(); diff --git a/tests/simple.rs b/tests/simple.rs index 3d77e77..c724773 100644 --- a/tests/simple.rs +++ b/tests/simple.rs @@ -22,7 +22,7 @@ async fn get_wasm_fail() { init_log(); let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let per_page = api::PerPage::new(1); @@ -46,7 +46,7 @@ async fn get_wasm_fail() { #[test] fn get_sync_fail() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let per_page = api::PerPage::new(1); @@ -70,7 +70,7 @@ fn get_sync_fail() { #[test] fn get_sync_ok() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let per_page = api::PerPage::new(1); @@ -91,7 +91,7 @@ async fn get_wasm_ok() { init_log(); let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let per_page = api::PerPage::new(1); let mut params: repos::ReposListCommitsParams = per_page.as_ref().into(); @@ -114,7 +114,7 @@ async fn get_wasm_ok() { #[tokio::test] async fn get_async_ok() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let per_page = api::PerPage::new(1); let mut params: repos::ReposListCommitsParams = per_page.as_ref().into(); @@ -137,7 +137,7 @@ async fn get_async_ok() { #[tokio::test] async fn get_async_fail() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let per_page = api::PerPage::new(1); @@ -164,7 +164,7 @@ async fn post_wasm_fail() { init_log(); let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let body = models::PostReposAddUserAccessRestrictions { users: vec!["fussybeaver".to_string()].into(), @@ -190,7 +190,7 @@ async fn post_wasm_fail() { #[test] fn post_sync_fail() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let body = models::PostReposAddUserAccessRestrictions { users: vec!["fussybeaver".to_string()].into(), @@ -215,7 +215,7 @@ fn post_sync_fail() { #[tokio::test] async fn post_async_fail() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let body = models::PostReposAddUserAccessRestrictions { users: vec!["fussybeaver".to_string()].into(), diff --git a/tests/sync.rs b/tests/sync.rs index 61c02dc..a82dd8f 100644 --- a/tests/sync.rs +++ b/tests/sync.rs @@ -13,7 +13,7 @@ use log::{debug, info}; #[test] fn list_commits_sync_ok() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let per_page = api::PerPage::new(1); let mut params: repos::ReposListCommitsParams = per_page.as_ref().into(); @@ -28,7 +28,7 @@ fn list_commits_sync_ok() { #[test] fn search_sync_ok() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let search = search::new(&client); let opts = search::SearchReposParams::new().q("bollard"); let req = search.repos(opts); @@ -40,7 +40,7 @@ fn search_sync_ok() { #[test] fn gists_sync_ok() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let gists = gists::new(&client); let req = gists.list_public(Some(&api::PerPage::new(1))); @@ -51,7 +51,7 @@ fn gists_sync_ok() { #[test] fn users_sync_ok() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let users = users::new(&client); let req = users.list(Some(users::UsersListParams::new().per_page(1))); @@ -67,7 +67,7 @@ fn users_sync_ok() { #[test] fn meta_sync_ok() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let meta = meta::new(&client); let req = meta.get(); @@ -78,7 +78,7 @@ fn meta_sync_ok() { #[test] fn issues_sync_ok() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let issues = issues::new(&client); let per_page = api::PerPage::new(1); let req = issues.list_for_repo("fussybeaver", "bollard", Some(&per_page)); @@ -108,7 +108,7 @@ fn license_sync_ok() { use roctogen::api::licenses::LicensesGetForRepoParams; let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let license = licenses::new(&client); let req = license.get_for_repo("fussybeaver", "bollard", None::); @@ -127,7 +127,7 @@ fn license_sync_ok() { #[test] fn reactions_sync_ok() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let per_page = api::PerPage::new(1); let reactions = reactions::new(&client); let req = reactions.list_for_issue("fussybeaver", "bollard", 86, Some(&per_page)); @@ -139,7 +139,7 @@ fn reactions_sync_ok() { #[test] fn actions_sync_ok() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let per_page = api::PerPage::new(1); let activity = activity::new(&client); let req = activity.list_watchers_for_repo("fussybeaver", "bollard", Some(&per_page)); @@ -163,7 +163,7 @@ fn actions_sync_ok() { #[test] fn rate_limit_sync_ok() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let rate_limit = rate_limit::new(&client); let req = rate_limit.get(); @@ -184,7 +184,7 @@ fn rate_limit_sync_ok() { #[test] fn post_sync_fail() { let auth = Auth::None; - let client = client(&auth); + let client = client(&auth).expect("Cannot create client"); let body = models::PostReposAddUserAccessRestrictions { users: vec!["fussybeaver".to_string()].into(),