Skip to content

Automatic & type-safe API clients - provide the interface and i will happily do the rest :)

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

cakekindel/happi-rs

Repository files navigation

crates.io docs.rs Maintenance

happi

an auto-impl http client that will leave you happi :)

This includes a CI/CD pipeline, README templating, and cargo-make scripts.

Example

Using an example of https://reqres.in/, a happi implementation of this would look something like:

pub fn main() -> Result<(), dyn std::error::Error> {
  let reqres = ReqResApi(reqwest::blocking::Client::new());

  let user_page = reqres.get_all_users(None)?;

  println!("{:#?}", user_page);
}

// This is the *real* client that hits the API
#[happi(base_url = "https://reqres.in/api", blocking)]
pub struct ReqResApi(#[client] hyper::Client);

// This is a trait for the `user` resource that `happi`
// will implement for `ReqResApi`.
//
// When you want to use this resource, your function can
// accept an `impl reqres::UserResource`, accepting the real
// deal or a mock when you write tests.
#[happi(
  api(ReqResApi),
  resource("/users"),
  responds(200, json),
)]
pub trait UserResource {
  #[get]
  pub fn get_all_users(&self, #[query] page: Option<u32>) -> Result<UserPage, happi::Error>;

  #[get(
    "/{id}",
    responds(404, unit),
    when(status == 404, invoke = |_resp| Ok(None)),
  )]
  pub fn get_user(&self, id: u32) -> Result<Option<User>, happi::Error>;
}

#[derive(Debug, Serialize, Deserialize)]
pub struct User {
  id: u32,
  first_name: String,
  last_name: String,
  avatar: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UserPage {
  page: u32,
  per_page: u32,
  total: u32,
  total_pages: u32,
  data: Vec<User>
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Automatic & type-safe API clients - provide the interface and i will happily do the rest :)

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published