3 releases

0.1.2 Apr 14, 2022
0.1.1 Mar 14, 2022
0.1.0 Mar 10, 2022

#14 in #rest-api

25 downloads per month
Used in hafas-rest

GPL-3.0-or-later

34KB
486 lines

RRW (Rust REST Wrapper)

A crate to easily build clients for REST-APIs.

Example

#[derive(Serialize)]
struct LocationQuery {
    query: String,
}

#[derive(Deserialize, Debug, PartialEq, Eq)]
struct Location {
    id: String,
    name: String,
}

#[rest]
impl Bahn {
    async fn location(&self, location: &LocationQuery) -> Result<Vec<Location>, reqwest::Error> {
        RestRequest::<&LocationQuery, ()>::get("/locations").query(location)
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    env_logger::init();
    let bahn = Bahn::new(RestConfig::new("https://v5.db.transport.rest"));
    let berlin = LocationQuery {
        query: "Berlin".to_string(),
    };

    let results = bahn.location(&berlin).await?;

    for location in results {
        println!("{}: {}", location.id, location.name);
    }

    Ok(())
}

Features

  • Easily build a REST-Wrapper
  • Included throttle mechanism (currently not very advanced, but expandable).

Future Features

  • More advanced throttle mechanisms.
  • Authentication (Username/Password, OAuth).

Dependencies

~6–18MB
~246K SLoC