tapioca

Crates.iotapioca
lib.rstapioca
version0.0.1
sourcesrc
created_at2017-06-08 19:07:06.100656
updated_at2017-08-02 14:52:22.704919
descriptionType-safe REST client using the OpenAPI Specification
homepage
repositoryhttps://github.com/OJFord/tapioca/tree/master/tapioca
max_upload_size
id18243
size9,732
Oliver Ford (OJFord)

documentation

README

Tapioca

Typed APIs (that Ollie Coshed into an Acronym)

Crate Build Status

tapioca is an HTTP client for rust that aims to help the compiler help you to access REST+JSON APIs in a type-safer manner.

It uses the OpenAPI Initiative's schema specification to infer types for path and query parameters, request and response bodies, et al. and then serde to de/serialise them.

infer_api!(service, "https://service.api/schema.yml")
use service::path;

fn main() {
    let auth = service::ServerAuth::new();

    match path::get(&auth) {
        Ok(response) => match response.body() {
            path::OkBody::Status200(body) => println!("Thing is: {}", body.thing),
            path::OkBody::UnspecifiedCode(body) => {
                // We're forced to handle every status code in the schema;
                //  including the possibility that the server replies off-script.
                println!("I don't know what thing is!")
            },
        },
        Err(response) => match response.body() {
            path::ErrBody::Status403(body) => println!("That's not my thing"),
            path::ErrBody::UnspecifiedCode(_)
            | path::ErrBody::MalformedJson(_)
            | path::ErrBody::NetworkFailure() => println!("Something went wrong"),
        },
    }
}

So, we can pattern-match responses by status code, and access the JSON response as a rust type. tapioca also aims to prevent you from shooting yourself in the foot with an invalid sequence of requests, such as 'GET after DELETE' on a particular resource: this is achieved by constructing resource IDs only from responses, and static values. DELETE functions cause the resource ID argument to be moved (while other methods only borrow) preventing it from being further used.

Commit count: 0

cargo fmt