Restcrab

crates.io docs.rs

Restcrab provides a procedural macro restcrab and a trait Restcrab for generating a REST client from a trait definition.

Usage

```rust no_run use std::collections::HashMap;

use serde::{Serialize, Deserialize}; use restcrab::{restcrab, Restcrab, crabs::reqwest};

[derive(Serialize, Deserialize)]

struct Request { id: i32 }

[derive(Serialize, Deserialize)]

struct Response { message: String }

[restcrab(crab = "reqwest::Reqwest")]

trait Service { #[restcrab(method = "GET", uri = "/empty")] fn urifromattribute();

#[restcrab(method = "GET")] fn urifrommethod_name();

#[restcrab(method = "GET", uri = "/staticheader", header("Content-Type", "application/json"))] fn staticheader();

#[restcrab(method = "GET", uri = "/staticheaders", header("Content-Type", "application/json"), header("User-Agen", "Restcrab"))] fn staticheaders();

#[restcrab(method = "POST", uri = "/staticbody", body = "0")] fn staticbody() -> String;

#[restcrab(method = "POST", uri = "/dynamicheaders")] fn dynamicheaders(#[headers] headers: HashMap) -> String;

#[restcrab(method = "POST", uri = "/dynamicheaders", header("Content-Type", "application/json"))] fn bothheaders(#[headers] headers: HashMap) -> String;

#[restcrab(method = "POST", uri = "/dynamicbody")] fn dynamicbody(#[body] body: Request) -> Response; }

fn main() { let client = ServiceClient::fromoptions(reqwest::Options { baseurl: "https://service.url".parse().unwrap() }).unwrap();

let mut headers = HashMap::new(); headers.insert("User-Agen".tostring(), "Restcrab".tostring());

let urifromattribute: () = client.urifromattribute().unwrap(); let urifrommethodname: () = client.urifrommethodname().unwrap(); let staticheader: () = client.staticheader().unwrap(); let staticheaders: () = client.staticheaders().unwrap(); let staticbody: String = client.staticbody().unwrap(); let dynamicheaders: String = client.dynamicheaders(headers.clone()).unwrap(); let bothheaders: String = client.bothheaders(headers).unwrap(); let dynamicbody: Response = client.dynamicbody(Request { id: 0 }).unwrap(); } ```

Modular Backends (crabs)

Because I like to use unhelpful terminology a backend for restcrab is called a crab.

Types which implement the Restcrab trait can be used as crabs.

The crate provides one crab which uses the Reqwest http client.

If you want to implement your own crab please look at the provided implementation as starting off point