retroqwest

Crates.ioretroqwest
lib.rsretroqwest
version0.0.1-rc.4
sourcesrc
created_at2021-09-18 18:25:58.520708
updated_at2021-09-19 19:15:48.957733
descriptionA reqwest REST client generator.
homepage
repositoryhttps://github.com/jakeswenson/retroqwest
max_upload_size
id453379
size10,428
Jake Swenson (jakeswenson)

documentation

README

Retroqwest

This project is still a work in progress!!

A Rust proc-macro attribute HTTP Client generator from a trait. Inspired by Retrofit and Refit to bring something like them to rust.

Example

#[derive(Deserialize, Serialize, Clone)]
pub struct HttpBinResponse {
  pub url: String,
}

#[retroqwest::retroqwest]
pub trait HttpBin {
  #[http::get("/anything")]
  async fn get_anything(&self) -> Result<HttpBinResponse, RetroqwestError>;

  #[http::get("/anything/{name}")]
  async fn get_by_name(&self, name: String) -> Result<HttpBinResponse, RetroqwestError>;

  #[http::post("/anything/{name}")]
  async fn post_to_name(
    &self,
    name: String,
    #[query] q: bool,
    #[json] body: &HttpBinResponse,
  ) -> Result<HttpBinResponse, RetroqwestError>;
}

impl HttpBinClient {
  pub fn new(base_uri: String) -> Result<Self, RetroqwestError> {
    Self::from_builder(base_uri, ClientBuilder::default())
  }
}

// This method allows for better code completion
// since `impl HttpBin` is better than the generated struct...
fn build_client(uri: String) -> Result<impl HttpBin, retroqwest::RetroqwestError> {
  Ok(HttpBinClient::new(uri)?)
}

See tests for a full example

Commit count: 35

cargo fmt