Need help with anterofit?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

abonander
125 Stars 12 Forks Other 300 Commits 12 Opened issues

Description

Strongly typed, asynchronous REST client framework for Rust.

Services available

!
?

Need anything else?

Contributors list

# 39,080
Rust
crates
multipa...
mariadb
280 commits
# 58,707
rust-la...
Docker
python-...
testing...
9 commits
# 141,568
spotify
Clojure
mariadb
concate...
1 commit
# 171,097
Rust
jupyter
R
bandwid...
1 commit

anterofit Build Status On Crates.io

Anterofit is a collection of Rust macros coupled to a lightweight, self-contained HTTP framework that allows you to easily create strongly-typed Rust wrappers for calling REST APIs.

// 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, 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(&amp;self) -&gt; Vec<post> {
        GET("/posts")
    }

    /// Create a new Post under the given user ID with the given title and body.
    fn new_post(&amp;self, userid: u64, title: &amp;str, body: &amp;str) -&gt; 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" =&gt; 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(&amp;adapter);
fetch_posts(&amp;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);
}

}

Inspired by Square's Retrofit, as referenced in the name, Anterofit is even more strongly typed as everything that is feasible to check at compile-time, is. Runtime errors are, with few exceptions, reserved for error conditions that can only be discovered at runtime.

Usage

Get started with our User Guide

Or an in-depth look with our Documentation

Setup

Serde and JSON serialization:

Enabled by default with the

serde-all
feature.

Cargo.toml
: ```toml [dependencies] anterofit = "0.1"

serde
is required in the dependencies but not in the crate root.

serde = "0.9" serde_derive = "0.9" ```

Crate Root: ```rust

[macro_use] extern crate anterofit;

[macrouse] extern crate serdederive;

#### [`rustc-serialize`](https://crates.io/crates/rustc-serialize):

Cargo.toml:

```toml [dependencies] rustc-serialize = "0.3"

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

Crate Root: ```rust

[macro_use] extern crate anterofit;

extern crate rustc_serialize; ```

License

Licensed under either of

  • Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

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.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.