Crate rrw

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§

authenticate
Authentication of requests
throttle
Utilities for throttling requests.

Structs§

RestConfig
A configuration to simplify REST-API-requests.
RestConfigBuilder
A builder for a RestConfig.
RestRequest
A request to a REST-API.
StandardRestError
A standard error scheme found in many REST-API responses.

Enums§

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