#http #siren #hypermedia #api-bindings #hateoas

http_siren

Support for Siren responses in HTTP APIs

1 unstable release

0.1.0 Jul 23, 2022

#927 in HTTP server

MIT license

45KB
1K SLoC

Problem Details

Build status Crates.io Documentation

This crate provides an implementation of the Siren Hypermedia specification.

Example Usage

The following is a valid handler for Axum that returns a subset of the example from the Siren specification:

async fn example() -> http_siren::Response<OrderProperties> {
    http_siren::Document::new(OrderProperties {
        order_number: 42,
        item_count: 3,
        status: "pending".to_owned(),
    })
    .with_class("order")
    .with_embedded_link(
        http_siren::Link::new("http://api.x.io/orders/42/items")
            .with_class("items")
            .with_class("collection")
            .with_rel("http://x.io/rels/order-items"),
    ).into()
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct OrderProperties {
    pub order_number: u32,
    pub item_count: u32,
    pub status: String,
}

When used with a supported HTTP Server, this will automatically generate the correct JSON response and set the Content-Type header to the correct value of application/vnd.siren+json.

Supported HTTP Servers

Currently this is only supported with the following HTTP Servers:

Examples of use with the different HTTP Servers can be found in the examples directory.

Features

HTTP Server support is behind feature flags for the appropriate HTTP Server. As such, you will need to enable the correct feature for the HTTP Server that you are using.

Currently supported features are:

  • axum - For the Axum HTTP Server.

Safety

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% safe Rust.

Minimum supported Rust version

The MSRV for http_siren is 1.60.0. However, the HTTP Servers that are used with it might need a higher version.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in http_siren by you, shall be licensed as MIT, without any additional terms or conditions.

Dependencies

~3–14MB
~154K SLoC