4 个版本

0.0.1-rc.42021 年 9 月 19 日
0.0.1-rc.12021 年 9 月 18 日

HTTP 客户端 中有 410

MIT 许可证

5KB

Retroqwest

该项目仍在开发中!

从 trait 生成 Rust proc-macro 属性 HTTP 客户端。受 RetrofitRefit 启发,将类似功能带到 Rust 中。

示例

#[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)?)
}

查看 测试 以获取完整示例

依赖项

~3–18MB
~238K SLoC