ApiCommons

Coverage StatusBuild Status

This library aims to ease the development of REST API's, by providing functions to parse Parameters and generate outputs.


Ease creation of REST APIs. This library provides functions to


Parse received parameters against ecto schemas or manual parameter definitions. Automatically generate error responses from errors occured while parsing.

Mongoose-rest-api

Index

  1. Roadmap
  2. Installation
  3. Dependencies
  4. Examples
  5. Parameter check
  6. Contribution

Roadmap

Installation

If available in Hex, the package can be installed by adding api_commons to your list of dependencies in mix.exs:

def deps do
  [
    {:api_commons, "~> 0.1.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/api_commons.

Dependencies

Parameter check

| Function | Description | --- | --- | &check/3 | Check received parameter list for a single parameter | &like_schema/3 | Check received parameters against ecto schema


defmodule AppWeb.CommentController do
  use AppWeb, :controller
  alias ApiCommons.Parameter

  def create(conn, params) do
    param_checks = conn
    |> Parameter.check(:user, type: :integer, position: :body)
    |> Parameter.check(:title, position: :body)
    |> Parameter.check(:content, position: :body)
    |> Parameter.check(:response_on, type: :integer, required?: false)

    # Render either error view or the entity
    if param_checks.valid? do
      render("comment.json", params: param_checks.parsed)
    else
      render("error.json", errors: param_checks.errors)
    end
  end
end

defmodule AppWeb.CommentView do
  use AppWeb, :view
  alias ApiCommons.Response

  def render("error.json", params) do
    # Render the error
  end

  def render("comment.json", params) do
    
  end
end

defmodule AppWeb.CommentController do
  use AppWeb, :controller

  alias AppRepo.Comment
  alias ApiCommons.Parameter

  def create(conn, params) do
    param_checks = params
    |> Parameter.like_json(Comment)

    # Render either error view or the entity
    if param_checks.valid? do
      render("comment.json", params: param_checks.parsed)
    else
      render("error.json", errors: param_checks.errors)
    end
  end
end

HATEOAS

Contribution