solidsnack / tapioca

Type-safe REST-focused HTTP client for Rust - via the OpenAPI Specification

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

Type-safe REST-focused HTTP client for Rust - via the OpenAPI Specification

License:MIT License


Languages

Language:Rust 100.0%