Restcrab provides a procedural macro restcrab
and a trait Restcrab
for generating a REST client from a trait definition.
```rust no_run use std::collections::HashMap;
use serde::{Serialize, Deserialize}; use restcrab::{restcrab, Restcrab, crabs::reqwest};
struct Request { id: i32 }
struct Response { message: String }
trait Service { #[restcrab(method = "GET", uri = "/empty")] fn urifromattribute();
#[restcrab(method = "GET", uri = "/empty/{name}")] fn urifromattributewithparameter(#[parameter] name: &str);
#[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 = "GET", uri = "/staticquery", query("some", "query"), query("another", "one"))] fn staticquery();
#[restcrab(method = "POST", uri = "/staticbody", body = "0")] fn staticbody() -> String;
#[restcrab(method = "POST", uri = "/dynamicheaders")]
fn dynamicheaders(#[headers] headers: HashMap
#[restcrab(method = "POST", uri = "/dynamicqueries")]
fn dynamicqueries(#[queries] queries: HashMap
#[restcrab(method = "POST", uri = "/dynamicheaders", header("Content-Type", "application/json"))]
fn bothheaders(#[headers] headers: HashMap
#[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(); } ```
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