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 2087bbe
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 83 deletions.
18 changes: 9 additions & 9 deletions src/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,33 @@ 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>;
#[allow(refining_impl_trait)]
#[allow(async_fn_in_trait)]
async fn to_json_async<E: for<'de> Deserialize<'de> + Unpin + 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
19 changes: 9 additions & 10 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 @@ -65,18 +67,15 @@ impl super::Client for Client {

async fn fetch_async(
&self,
request: Self::Req,
_request: Self::Req,
) -> 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
54 changes: 27 additions & 27 deletions src/endpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,42 @@ pub const GITHUB_BASE_API_URL: &str = if cfg!(feature = "mock") {
"https://api.github.com"
};

pub mod meta;
pub mod issues;
pub mod licenses;
pub mod reactions;
pub mod actions;
pub mod activity;
pub mod projects;
pub mod orgs;
pub mod users;
pub mod apps;
pub mod rate_limit;
pub mod repos;
pub mod secret_scanning;
pub mod security_advisories;
pub mod packages;
pub mod search;
pub mod billing;
pub mod checks;
pub mod classroom;
pub mod teams;
pub mod oidc;
pub mod markdown;
pub mod actions;
pub mod migrations;
pub mod gists;
pub mod code_scanning;
pub mod code_security;
pub mod dependency_graph;
pub mod codes_of_conduct;
pub mod codespaces;
pub mod copilot;
pub mod dependabot;
pub mod codes_of_conduct;
pub mod pulls;
pub mod dependency_graph;
pub mod emojis;
pub mod gists;
pub mod gitignore;
pub mod git;
pub mod code_scanning;
pub mod checks;
pub mod billing;
pub mod interactions;
pub mod codespaces;
pub mod emojis;
pub mod issues;
pub mod licenses;
pub mod markdown;
pub mod meta;
pub mod migrations;
pub mod oidc;
pub mod orgs;
pub mod packages;
pub mod projects;
pub mod pulls;
pub mod rate_limit;
pub mod reactions;
pub mod repos;
pub mod search;
pub mod secret_scanning;
pub mod security_advisories;
pub mod teams;
pub mod users;

pub struct PerPage {
per_page: u16,
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
Loading

0 comments on commit 2087bbe

Please sign in to comment.