Skip to content
REST API client scaffolder for elixir
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config
lib
test
.gitignore
.travis.yml
LICENSE
README.md
mix.exs
mix.lock

README.md

Prex

Status Build Status

Tasks for generating api interfaces in elixir from ApiBlueprint files.

And you don't feel much like ridin', You just wish the trip was through...

This project is under development and in experimental stage

What is ApiBlueprint?

According to ApiBlueprint.org

API Blueprint is a powerful high-level API description language for web APIs.

So in an ApiBlueprint file (.apib) there is information on Actions and their parameters.

What Prex does?

Prex can convert .apib files to equivalant elixir code as a bootstrap with .apib documents.

It can help you spend your time mostly on your issues instead of developing API client.

How prex Does it?

  1. Simply add :prex as dev only dependency in your project like this:
def deps do
  [{:prex, "~> 0.0.2", only: :dev, runtime: false}]
end
  1. Install drafter(ApiBlueprint parser): github.com/apiaryio/drafter
  2. Run mix deps.get and mix deps.compile
  3. Now you can run mix prex.gen.from_blueprint [path to .apib file] [name for api]
  4. Tadaa! A file like this is now generated in lib directory of your project
# Created by Prex
defmodule Newapi.User do
  @moduledoc """
  Represents user details.

  ---

  **User attributes:**

  - id `(Number)` : unique identifier.

  - fname `(String)` : First Name.

  - lname `(String)` : Last Name.

  - email `(String)` : email id of the user.

  ---
  """

  @base_url "http://sample.pandurangpatil.com"

  ###
  # API Calls
  ###

  # User Collection

  @doc """
  Retrieve paginated list of users.

  ## Parameters
    - since: Timestamp in ISO 8601 format: `YYYY-MM-DDTHH:MM:SSZ` Only users updated at or after this time are returned.
    - limit: maximum number of records expected by client.
  """
  def list_all_users(since \\ nil, limit \\ nil) do
    req_url = Path.join @base_url, "/users?limit=#{limit |> URI.encode_www_form}" <>
     (if since != nil, do: "since=#{since |> URI.encode_www_form}", else: "")

    HTTPoison.request(:get, req_url, body: Poison.encode!(%{"since" => since, "limit" => limit}), headers: ["Content-Type": "application/json"])
  end

  def list_all_users!(since \\ nil, limit \\ nil) do
    {:ok, result} = list_all_users(since, limit)
    result
  end

  @doc """

  """
  def create_a_user do
    req_url = Path.join @base_url, "/users{?since,limit}"
    HTTPoison.request(:put, req_url)
  end

  def create_a_user! do
    {:ok, result} = create_a_user()
    result
  end

  # User

  @doc """


  ## Parameters
    - id: Numeric `id` of the User to perform action with.
  """
  def retrieve_a_user(id) do
    req_url = Path.join @base_url, "/users/?id=#{id |> URI.encode_www_form}"

    HTTPoison.request(:get, req_url, body: Poison.encode!(%{"id" => id}), headers: ["Content-Type": "application/json"])
  end

  def retrieve_a_user!(id) do
    {:ok, result} = retrieve_a_user(id)
    result
  end

  @doc """
  Update user details

  ## Parameters
    - id: Numeric `id` of the User to perform action with.
  """
  def update_a_user(id) do
    req_url = Path.join @base_url, "/users/?id=#{id |> URI.encode_www_form}"

    HTTPoison.request(:post, req_url, body: Poison.encode!(%{"id" => id}), headers: ["Content-Type": "application/json"])
  end

  def update_a_user!(id) do
    {:ok, result} = update_a_user(id)
    result
  end

  @doc """


  ## Parameters
    - id: Numeric `id` of the User to perform action with.
  """
  def remove_a_user(id) do
    req_url = Path.join @base_url, "/users/?id=#{id |> URI.encode_www_form}"

    HTTPoison.request(:delete, req_url, body: Poison.encode!(%{"id" => id}), headers: ["Content-Type": "application/json"])
  end

  def remove_a_user!(id) do
    {:ok, result} = remove_a_user(id)
    result
  end

end

Documents

Currently docs for mix task and usage is not available!

Full documents.

License

MIT

You can’t perform that action at this time.