Crate rrw

source · []
Expand description

RRW (Rust REST Wrapper)

A crate to easily build clients for REST-APIs.

There is a auxiliary crate, rrw_macro available that simplifies the generation of REST-APIs even more.

Example

Structs for request parameters and result types:

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

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

The REST-Wrapper without macros.

struct Bahn {
    rest: RestConfig,
}

impl Bahn {
    pub fn new(rest: RestConfig) -> Self {
        Self { rest }
    }
}

impl Bahn {
    async fn location(
        &self, location: &LocationQuery
    ) -> Result<Vec<Location>, Error<StandardRestError>> {
        Ok(self
            .rest
            .execute(&RestRequest::<&LocationQuery, ()>::get("/locations").query(location))
            .await?)
    }
}

The REST-Wrapper with macros.

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

Usage:

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);
}

More examples can be found in the examples directory.

Modules

Authentication of requests

Utilities for throttling requests.

Structs

A configuration to simplify REST-API-requests.

A builder for a RestConfig.

A request to a REST-API.

A standard error scheme found in many REST-API responses.

Enums

The errors that may occure when accessing a REST-API.