All Projects → Cantido → Liberator

Cantido / Liberator

Licence: mit
An Elixir library for building RESTful applications.

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to Liberator

Watsonwebserver
Watson is the fastest, easiest way to build scalable RESTful web servers and services in C#.
Stars: ✭ 125 (+346.43%)
Mutual labels:  rest-api, rest, http-server
Generator Http Fake Backend
Yeoman generator for building a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 49 (+75%)
Mutual labels:  rest-api, rest, http-server
Farwest
Framework for building RESTful HATEOAS-driven applications.
Stars: ✭ 18 (-35.71%)
Mutual labels:  rest-api, rest, http-server
Http Fake Backend
Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 253 (+803.57%)
Mutual labels:  rest-api, rest, http-server
Restheart
RESTHeart - The REST API for MongoDB
Stars: ✭ 659 (+2253.57%)
Mutual labels:  rest-api, rest
Rest Api Design Guide
NBB's REST-ish API Design Guide
Stars: ✭ 643 (+2196.43%)
Mutual labels:  rest-api, rest
Restclient
🦄 Simple HTTP and REST client for Unity based on Promises, also supports Callbacks! 🎮
Stars: ✭ 675 (+2310.71%)
Mutual labels:  rest-api, rest
Apidoc
RESTful API 文档生成工具,支持 Go、Java、Swift、JavaScript、Rust、PHP、Python、Typescript、Kotlin 和 Ruby 等大部分语言。
Stars: ✭ 785 (+2703.57%)
Mutual labels:  rest-api, rest
Modular Monolith With Ddd
Full Modular Monolith application with Domain-Driven Design approach.
Stars: ✭ 6,210 (+22078.57%)
Mutual labels:  rest-api, rest
Restinio
Cross-platform, efficient, customizable, and robust asynchronous HTTP/WebSocket server C++14 library with the right balance between performance and ease of use
Stars: ✭ 694 (+2378.57%)
Mutual labels:  rest, http-server
Ngx Restangular
Restangular for Angular 2 and higher versions
Stars: ✭ 787 (+2710.71%)
Mutual labels:  rest-api, rest
Rest Assured
Java DSL for easy testing of REST services
Stars: ✭ 5,646 (+20064.29%)
Mutual labels:  rest-api, rest
Spyke
Interact with REST services in an ActiveRecord-like manner
Stars: ✭ 591 (+2010.71%)
Mutual labels:  rest-api, rest
Rest Api Nodejs Mongodb
A boilerplate for REST API Development with Node.js, Express, and MongoDB
Stars: ✭ 672 (+2300%)
Mutual labels:  rest-api, rest
Restful Api Design References
RESTful API 设计参考文献列表,可帮助你更加彻底的了解REST风格的接口设计。
Stars: ✭ 4,830 (+17150%)
Mutual labels:  rest-api, rest
Cowboy
Small, fast, modern HTTP server for Erlang/OTP.
Stars: ✭ 6,533 (+23232.14%)
Mutual labels:  rest, http-server
Gen
Converts a database into gorm structs and RESTful api
Stars: ✭ 825 (+2846.43%)
Mutual labels:  rest-api, rest
Go Book Store Api
Go Sample project to understand Mysql CRUD operation with best practises Includes logging, JWT, Swagger and Transactions
Stars: ✭ 18 (-35.71%)
Mutual labels:  rest-api, http-server
Rest Api Examples
Test and Prototype with Fake Online REST/OAuth 2 APIs Examples
Stars: ✭ 13 (-53.57%)
Mutual labels:  rest-api, rest
Cerberus
A demonstration of a completely stateless and RESTful token-based authorization system using JSON Web Tokens (JWT) and Spring Security.
Stars: ✭ 482 (+1621.43%)
Mutual labels:  rest-api, rest

Liberator

Hex.pm Elixir CI standard-readme compliant Contributor Covenant Gitter

An Elixir library for building RESTful applications.

Liberator is a port of the Liberator Clojure library that allows you to define a controller that adheres to the HTTP spec by providing just a few pieces of information. It implements a decision graph of simple boolean questions that lead your app to the correct HTTP status codes.

While Phoenix and Plug make routing easy, they don't do anything with content negotiation, or cache management, or existence checks, or anything like that, beyond calling the right controller function based on the HTTP method. There are a lot of decisions to make before returning the right HTTP status code, but Phoenix doesn't give you any additional power to do so. Liberator does.

Install

This package is available in Hex. Install it by adding liberator to your list of dependencies in mix.exs:

def deps do
  [
    {:liberator, "~> 1.3.0"}
  ]
end

Documentation can be generated with ExDoc and can be found online at https://hexdocs.pm/liberator.

Usage

For a basic GET endpoint, you can define an entire module in five lines of code. Technically you don't even need to implement these two, since sensible defaults are provided.

defmodule MyFirstResource do
  use Liberator.Resource

  def available_media_types(_), do: ["text/plain"]
  def handle_ok(_), do: "Hello world!"
end

It doesn't look like much, but behind the scenes, Liberator navigated a decision graph of content negotation, cache management, and existence checks before returning 200 OK. Liberator finds the best media type you support, and automatically encodes your return value. JSON is supported out of the box, and any additional types can be provided in a line of the config.

# in config.exs
config :liberator, media_types: %{
  "application/json" => Jason,
  "application/xml" => MyXmlCodec
}

# in your main body of code
defmodule MyJsonOrXmlResource do
  use Liberator.Resource

  def available_media_types(_), do: ["application/json", "application/xml"]
  def handle_ok(_), do: %{message: "hi!"}
end

A Liberator Resource implements the Plug spec, so you can forward requests to it in frameworks like Phoenix:

scope "/api", MyApp do
  pipe_through [:api]

  forward "/resources", MyFirstResource
end

Your results from questions are aggregated into the :assigns map on the conn, so you don't have to access data more than once.

defmodule MaybeExistingResource do
  use Liberator.Resource

  def exists?(conn) do
    case MyApp.Repo.get(MyApp.Post, conn.params["id"]) do
      nil -> false
      post -> %{post: post}
    end
  end
  def handle_ok(conn), do: conn.assigns[:post]
end

See more in the Getting Started guide, and in the documentation for Liberator.Resource.

Maintainer

This project was developed by Rosa Richter. You can get in touch with her on Keybase.io.

Thanks

Thanks to the maintainers of the original Clojure liberator project, Philipp Meier and Malcolm Sparks, for creating such a handy tool. Their great documentation was an immense help in porting it to Elixir. And thanks to the maintainers of Erlang's webmachine for inspiring them!

Contributing

Questions and pull requests are more than welcome. I follow Elixir's tenet of bad documentation being a bug, so if anything is unclear, please file an issue! Ideally, my answer to your question will be in an update to the docs.

Please see CONTRIBUTING.md for all the details you could ever want about helping me with this project.

Note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

License

MIT License

Copyright 2020 Rosa Richter

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].