Skip to content

Commit

Permalink
chore: Cleanup error handling, visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
fussybeaver committed Oct 18, 2024
1 parent 6850e24 commit 63b4afd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 54 deletions.
16 changes: 8 additions & 8 deletions src/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,29 @@ where
}
}

pub(crate) trait FromJson<A, T>
pub trait FromJson<A, T>
where
A: ser::Serialize,
{
fn from_json(model: A) -> Result<T, serde_json::Error>;
}

#[allow(dead_code)]
pub(crate) struct GitHubRequest<T> {
pub struct GitHubRequest<T> {
pub uri: String,
pub method: &'static str,
pub body: Option<T>,
pub headers: Vec<(&'static str, &'static str)>,
}

pub(crate) trait GitHubRequestBuilder<T, C: Client>
pub trait GitHubRequestBuilder<T, C: Client>
where
Self: Sized,
{
fn build(req: GitHubRequest<T>, client: &C) -> Result<Self, AdapterError>;
}

pub(crate) trait GitHubResponseExt {
pub trait GitHubResponseExt {
fn is_success(&self) -> bool;
fn status_code(&self) -> u16;
fn to_json<E: for<'de> Deserialize<'de> + std::fmt::Debug>(self) -> Result<E, AdapterError>;
Expand All @@ -130,7 +130,7 @@ pub(crate) trait GitHubResponseExt {
pub trait Client {
type Req;

fn new(auth: &Auth) -> Self
fn new(auth: &Auth) -> Result<Self, AdapterError>
where
Self: Sized;
fn get_auth(&self) -> &Auth;
Expand All @@ -140,14 +140,14 @@ pub trait Client {
}

#[cfg(target_arch = "wasm32")]
pub fn client(auth: &Auth) -> impl Client<Req = Req> {
pub fn client(auth: &Auth) -> Result<impl Client<Req = Req>, AdapterError> {
wasm::Client::new(auth)
}
#[cfg(feature = "ureq")]
pub fn client(auth: &Auth) -> impl Client<Req = Req> {
pub fn client(auth: &Auth) -> Result<impl Client<Req = Req>, AdapterError> {
ureq::Client::new(auth)
}
#[cfg(feature = "reqwest")]
pub fn client(auth: &Auth) -> impl Client<Req = Req> {
pub fn client(auth: &Auth) -> Result<impl Client<Req = Req>, AdapterError> {
reqwest::Client::new(auth)
}
15 changes: 8 additions & 7 deletions src/adapters/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,26 @@ 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<Self, AdapterError> {
Ok(Self {
auth: auth.to_owned(),
pool: reqwest::Client::new(),
}
})
}

fn get_auth(&self) -> &Auth {
&self.auth
}

fn fetch(&self, _request: Self::Req) -> Result<impl GitHubResponseExt, AdapterError> {
unimplemented!("Reqwest adapter only has async fetch implemented");
Err::<Response, _>(std::io::Error::new(std::io::ErrorKind::Other, "oh no!").into())
Err::<Response, _>(AdapterError::UnimplementedSync)
}

async fn fetch_async(
Expand All @@ -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 {
Expand Down
17 changes: 8 additions & 9 deletions src/adapters/ureq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, AdapterError> {
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 {
Expand All @@ -67,16 +69,13 @@ impl super::Client for Client {
&self,
request: Self::Req,

Check warning on line 70 in src/adapters/ureq.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `request`

Check warning on line 70 in src/adapters/ureq.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `request`

Check warning on line 70 in src/adapters/ureq.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `request`

Check warning on line 70 in src/adapters/ureq.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `request`
) -> Result<impl GitHubResponseExt, AdapterError> {
unimplemented!("Ureq adapter only has sync fetch implemented");
Err::<Response<ureq::Body>, _>(
std::io::Error::new(std::io::ErrorKind::Other, "oh no!").into(),
)
Err::<Response<ureq::Body>, _>(AdapterError::UnimplementedAsync)
}
}

pub struct Client {
pub(crate) auth: Auth,
pub(crate) agent: Agent,
auth: Auth,
agent: Agent,
}

impl GitHubResponseExt for Response<ureq::Body> {
Expand Down
17 changes: 9 additions & 8 deletions src/adapters/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -67,20 +69,19 @@ pub struct ObjectError {
impl super::Client for Client {
type Req = web_sys::Request;

fn new(auth: &Auth) -> Self {
Self {
fn new(auth: &Auth) -> Result<Self, AdapterError> {
Ok(Self {
auth: auth.to_owned(),
scope: scope().expect("Cannot fetch wasm scope"),
}
scope: scope()?,
})
}

fn get_auth(&self) -> &Auth {
&self.auth
}

fn fetch(&self, _request: Self::Req) -> Result<impl GitHubResponseExt, AdapterError> {
unimplemented!("Wasm adapter only has async fetch implemented");
Err::<GitHubResponse, _>(std::io::Error::new(std::io::ErrorKind::Other, "oh no!").into())
Err::<GitHubResponse, _>(AdapterError::UnimplementedSync)
}

async fn fetch_async(
Expand All @@ -101,8 +102,8 @@ impl super::Client for Client {
}

pub struct Client {
pub(crate) auth: Auth,
pub(crate) scope: Box<dyn WasmScope>,
auth: Auth,
scope: Box<dyn WasmScope>,
}

impl From<JsValue> for AdapterError {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//!
//! ```nocompile
//! [dependencies]
//! roctogen = "0.12"
//! roctogen = "*"
//! ```
//!
//! # API
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 9 additions & 9 deletions tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);

Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
22 changes: 11 additions & 11 deletions tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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)));

Expand All @@ -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)));
Expand All @@ -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();

Expand All @@ -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));
Expand Down Expand Up @@ -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::<LicensesGetForRepoParams>);

Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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();

Expand All @@ -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(),
Expand Down

0 comments on commit 63b4afd

Please sign in to comment.