帮酷LOGO
  • 显示原文与译文双语对照的内容
Strongly typed, asynchronous REST client framework for Rust.

  • 源代码名称:anterofit
  • 源代码网址:http://www.github.com/abonander/anterofit
  • anterofit源代码文档
  • anterofit源代码下载
  • Git URL:
    git://www.github.com/abonander/anterofit.git
  • Git Clone代码到本地:
    git clone http://www.github.com/abonander/anterofit
  • Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/abonander/anterofit
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
  • anterofit Build StatusOn Crates.io

    Anterofit是与轻量级的自包含HTTP框架耦合的Rust 宏集合,它允许你轻松创建用于调用 REST api的强类型 Rust 包装器。

    //See examples/post_service.rs for more details#[macro_use] externcrate anterofit;
    #[macro_use] externcrate serde_derive;use anterofit::{Adapter, Url};
    #[derive(Debug, Deserialize)]structPost {
     pub userid: Option<u64>,
     pub id: u64,
     pub title: String,
     pub body: String}service! {
     traitPostService {
     ///Get a Post by id.fnget_post(&self, id: u64) -> Post {
     GET("/posts/{}", id)
     }
     ///Get all posts.fnget_posts(&self) -> Vec<Post> {
     GET("/posts")
     }
     ///Create a new Post under the given user ID with the given title and body.fnnew_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
     )
     }
     }
    }fnmain() {
     //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.fncreate_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 defaultfnfetch_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);
     }
    }

    由于在名称中引用了平方,所以在编译时,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"

    root?

    #[macro_use] externcrate anterofit;
    #[macro_use] externcrate serde_derive;
    rustc-serialize:

    Cargo.toml:

    [dependencies]rustc-serialize = "0.3"[dependencies.anterofit]version = "0.1"default-features = falsefeatures = ["rustc-serialize"]

    root?

    #[macro_use] externcrate anterofit;externcrate rustc_serialize;

    许可证

    在任何情况下许可

    按你的选择。

    捐赠

    除非你明确地说明,如果你在 Apache-2.0 许可以中有意提交的任何捐赠,应当如上所示。




    Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备05059198号-3  |  如果智培  |  酷兔英语