10次发布
使用旧的Rust 2015
0.2.0 | 2018年7月9日 |
---|---|
0.1.1 | 2017年3月7日 |
0.0.8 | 2017年2月5日 |
0.0.6 | 2017年1月29日 |
0.0.1 | 2016年11月27日 |
#304 in HTTP客户端
每月37次下载
130KB
2K SLoC
anterofit
Anterofit是一个Rust宏集合,与一个轻量级、自包含的HTTP框架配合使用,允许您轻松创建强类型Rust包装器以调用REST API。
// See examples/post_service.rs for more details
#[macro_use] extern crate anterofit;
#[macro_use] extern crate serde_derive;
use anterofit::{Adapter, Url};
#[derive(Debug, Deserialize)]
struct Post {
pub userid: Option<u64>,
pub id: u64,
pub title: String,
pub body: String
}
service! {
trait PostService {
/// Get a Post by id.
fn get_post(&self, id: u64) -> Post {
GET("/posts/{}", id)
}
/// Get all posts.
fn get_posts(&self) -> Vec<Post> {
GET("/posts")
}
/// Create a new Post under the given user ID with the given title and body.
fn new_post(&self, userid: u64, title: &str, body: &str) -> Post {
POST("/posts/");
// We use the `EAGER:` keyword so we can use borrowed values in the body.
// This serializes the body value immediately instead of waiting to serialize
// it on the executor.
body_map!(EAGER:
"userid" => userid,
"title": title,
"body": body
)
}
}
}
fn main() {
// Navigate to this URL in your browser for details. Very useful test API.
let url = Url::parse("https://jsonplaceholder.typicode.com").unwrap();
let adapter = Adapter::builder()
.base_url(url)
// When your REST API uses JSON in both requests and responses
.serialize_json()
.build();
create_post(&adapter);
fetch_posts(&adapter);
}
/// Create a new Post.
// All service traits are implemented for `Adapter` by default; using generics like this promotes good namespacing.
fn create_post<P: PostService>(post_service: &P) {
let post = post_service.new_post(42, "Hello", "World!")
// Execute the request in the background and wait for it to complete
.exec().block()
.unwrap();
println!("{:?}", post);
}
/// Fetch the top 3 posts in the database.
// Service traits are object-safe by default
fn fetch_posts(post_service: &PostService) {
let posts = post_service.get_posts()
// Shorthand for .exec().block(), but executes the request on the current thread.
.exec_here()
.unwrap();
for post in posts.into_iter().take(3) {
println!("{:?}", post);
}
}
灵感来源于Square的Retrofit,正如其名称所示,Anterofit的类型更加严格,因为所有在编译时可以检查的都进行了检查。运行时错误,除少数例外,仅保留用于只能在实际运行时发现的错误条件。
用法
从我们的用户指南开始
或通过我们的文档深入了解
设置
Serde和JSON序列化
默认启用serde-all
功能。
Cargo.toml
:
[dependencies]
anterofit = "0.1"
# `serde` is required in the dependencies but not in the crate root.
serde = "0.9"
serde_derive = "0.9"
包根
#[macro_use] extern crate anterofit;
#[macro_use] extern crate serde_derive;
rustc-serialize
:
Cargo.toml
:
[dependencies]
rustc-serialize = "0.3"
[dependencies.anterofit]
version = "0.1"
default-features = false
features = ["rustc-serialize"]
包根
#[macro_use] extern crate anterofit;
extern crate rustc_serialize;
许可证
根据您的选择,受以下任一许可证的许可:
- Apache许可证第2版,(LICENSE-APACHE或http://www.apache.org/licenses/LICENSE-2.0)
- MIT许可证(LICENSE-MIT或http://opensource.org/licenses/MIT)
。
贡献
除非您明确声明,否则您提交的任何贡献,按照Apache-2.0许可证的定义,将按照上述方式双重许可,不附加任何其他条款或条件。
依赖关系
~6.5MB
~145K SLoC