• Stars
    star
    159
  • Rank 235,916 (Top 5 %)
  • Language
    Elixir
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A repository `similar` to Ecto.Repo that maps to an underlying http client, sending requests to an external rest api instead of a database

Dayron

Build Status Deps Status Inline docs Coverage Status Twitter

Dayron is a flexible library to interact with RESTful APIs and map resources to Elixir data structures. It works similar of Ecto.Repo but, instead of retrieving data from a database, it has underlying http clients to retrieve data from external HTTP servers.

Installation

  1. Add Dayron to your list of dependencies in mix.exs:
def deps do
  [{:dayron, "~> 0.1"}]
end
  1. Ensure Dayron is started before your application:
def application do
  [applications: [:dayron]]
end
  1. Then run mix deps.get in your shell to fetch the dependencies.

Getting Started

Dayron requires a configuration entry with at least the external url attribute:

# In your config/config.exs file
config :my_app, MyApp.RestRepo,
  url: "http://api.example.com"

Then you must define MyApp.RestRepo somewhere in your application with:

# Somewhere in your application
defmodule MyApp.RestRepo do
  use Dayron.Repo, otp_app: :my_app
end

Defining Models

With modules and structs

Dayron Models are simple modules with use Dayron.Model to implement the required protocol. The resource option defines the path to be used by the HTTP client to retrieve data. A struct must be defined with defstruct to allow json responses mapping.

defmodule MyApp.User do
  # api requests to http://api.example.com/users
  use Dayron.Model, resource: "users"

  # struct defining model attributes
  defstruct name: "", age: 0
end

Reusing Ecto Models

Dayron Models can work together with Ecto Models, allowing data loading from Database and External APIs just selecting the desired Repo. The defined schema will be used by Dayron when parsing server responses. If no resource option is present, the schema source is used as resource name.

defmodule MyApp.User do
  use Ecto.Schema
  use Dayron.Model

  # dayron requests to http://api.example.com/users
  schema "users" do
    field :name, :string
    field :age, :integer, default: 0
  end
end

Retrieving Data

After defining the configuration and model, you are allowed to retrieve data from an external API in a similar way when compared to an Ecto Repo. The example below presents a UsersController where an index action retrieves a list of users from the server, and a show action retrieves a single User:

defmodule MyApp.UsersController do
  use MyApp.Web, :controller

  alias MyApp.User
  alias MyApp.RestRepo

  def index(conn, params)
    conn
    |> assign(:users, RestRepo.all(User))
    |> render("index.html")
  end

  def show(conn, %{"id" => id}) do
    case RestRepo.get(User, id) do
      nil  -> put_status(conn, :not_found)
      user -> render conn, "show.html", user: user
    end
  end

Generating modules

You can generate Dayron modules using the task dayron.gen.model

mix dayron.gen.model User users name age:integer

The first argument is the module name followed by the resource path. The generated model will contain:

  • a model file in lib/your_app/models
  • a test file in test/your_app/models

Both the model and the test path can be configured using the Dayron generators config.

config :dayron, :generators,
  models_path: "web/models",
  models_test_path: "test/models"

The model fields are given using name:type syntax where types can be one of the following:

:array, :integer, :float, :boolean, :string

Omitting the type makes it default to :string

Extra Configuration

Request Headers

Using the configuration you're allowed to set headers that will be sent on every HTTP request made by Dyron. In the configuration example below, the access-token header is sent on every request:

# In your config/config.exs file
config :my_app, MyApp.Dayron,
  url: "https://api.example.com",
  headers: ["access-token": "my-api-token"]

HTTP Client Adapter

Currently the only adapter available is HTTPoisonAdapter, which uses HTTPoison and hackney to manage HTTP requests.

You can also create your own adapter implementing the Dyron.Adapter behavior, and changing the configuration to something like:

# In your config/config.exs file
config :my_app, MyApp.Dayron,
  url: "https://api.example.com",
  adapter: MyDayronAdapter

Important links

Contributing

Pull request are very wellcome, but before opening a new one, please open an issue first.

If you want to send us a pull request, get the project working in you local:

$ git clone https://github.com/inaka/Dayron.git
$ cd Dayron
$ mix deps.get
$ mix test

Create a branch with the issue name and once you're ready (new additions and tests passing), submit your pull request!

Building docs

$ MIX_ENV=docs mix docs

Contact Us

If you find any bugs or have a problem while using this library, please open an issue in this repo (or a pull request).

You can also check all of our open-source projects at inaka.github.io.

Copyright and License

Copyright (c) 2016, Inaka.

Dayron source code is licensed under the Apache 2 License.

More Repositories

1

erlang_guidelines

Inaka's Erlang Coding Guidelines
Erlang
619
star
2

EventSource

A simple Swift client library for the Server Sent Events (SSE)
Swift
483
star
3

galgo

When you want your logs to be displayed on screen
Java
427
star
4

elvis

Erlang Style Reviewer
Erlang
424
star
5

apns4erl

Apple Push Notification Server for Erlang
Erlang
369
star
6

TinyTask

A Tiny Task Library
Java
324
star
7

worker_pool

Erlang worker pool
Erlang
274
star
8

sumo_db

Erlang Persistency Framework
Erlang
173
star
9

shotgun

For the times you need more than just a gun.
Erlang
166
star
10

cowboy_swagger

Swagger integration for Cowboy (built on trails)
Erlang
120
star
11

gold_fever

A Treasure Hunt for Erlangers
Erlang
86
star
12

Jayme

Abstraction layer that eases RESTful interconnections in Swift
Swift
81
star
13

guidelines

General Inaka Guidelines
75
star
14

cowboy-trails

A couple of improvements over Cowboy Routes
Erlang
71
star
15

jem.js

Just Erlang Maps for Javascript
JavaScript
69
star
16

sheldon

Very Simple Erlang Spell Checker
Erlang
62
star
17

elvis_core

The core of an Erlang linter
Erlang
61
star
18

niffy

Inline C code in Erlang modules to build NIFs
Erlang
60
star
19

sumo_rest

Generic cowboy handlers to work with Sumo
Erlang
59
star
20

serpents

Multi-Player Game on top of HDP protocol
Erlang
56
star
21

xref_runner

Erlang Xref Runner (inspired in rebar xref)
Erlang
50
star
22

erlang-github

Github API client
Erlang
46
star
23

lasse

SSE handler for Cowboy
Erlang
45
star
24

beam_olympics

Let's find the fastest beamer!
Erlang
39
star
25

fiar

Four in a Row - A game to learn Erlang
Erlang
36
star
26

zipper

Generic Zipper implementation in Erlang
Erlang
34
star
27

ios-xmpp-sample

Blog post sample project.
Swift
33
star
28

kotlillon

Android Kotlin Examples
Kotlin
33
star
29

katana-test

Meta Testing Utilities for common_test
Erlang
32
star
30

match_stream

A sample project to show in our scale blog post
JavaScript
30
star
31

phoenix_passwordless_login

Phoenix Passwordless Login
Elixir
29
star
32

KillerTask

Android AsyncTask wrapper library, written in Kotlin
Kotlin
26
star
33

canillita

Simple Paperboy-themed PubSub
Erlang
26
star
34

lewis

Rock your Android
Java
22
star
35

tirerl

Erlang interface to Elastic Search
Erlang
19
star
36

itweet

Twitter Stream API on ibrowse
Erlang
18
star
37

katana-code

Code Utilities for Erlang
Erlang
17
star
38

pusherman

queuing system for push notifications
Erlang
17
star
39

galgo-ios

When you want your logs to be displayed on screen
Objective-C
16
star
40

lsl

NIM in Erlang
Erlang
15
star
41

credo_server

Credo Server
Elixir
15
star
42

FadeButton

Fading effects for UIButtons made simple
Swift
15
star
43

Jolly

Jolly Chimp that keeps track of our Github Repos
Swift
12
star
44

rpsls

Rock Paper Scissors Lizzard Spock World Championship in Erlang
Erlang
12
star
45

ikbot

An elixir based customizable hipchat bot
Elixir
12
star
46

nconf

Nested Configuration Manager for Erlang Applications
Erlang
12
star
47

pushito

APNS over HTTP/2
Elixir
11
star
48

rest_guidelines

REST API Design Guidelines
11
star
49

fetjaba

From Erlang To Java and Back Again
Erlang
9
star
50

sumo_db_pgsql

PostgreSQL adapter for sumo_db.
Erlang
9
star
51

jinterface_stdlib

Erlang stdlib implementation on Java, based on JInterface
Java
9
star
52

IKCapture

Snapchat-Like Image Capture Library
Objective-C
8
star
53

MediaPickerController

Neat API for presenting the classical action sheet for picking an image or video from the device or camera.
Swift
8
star
54

PictureViewMaster

Interactive image projector.
Swift
8
star
55

toy_kv

A simple and reduced Key-Value Store written in Erlang
Erlang
7
star
56

ColorPicker

Color Picker for Swift
Swift
7
star
57

swift_guidelines

Inaka's Swift Coding Guidelines
7
star
58

spellingci

Spelling CI Server
Erlang
7
star
59

talks

Sources and pdfs of our talks and speeches
TeX
6
star
60

bookmarks

A collection of bookmarks for Inakos
6
star
61

IKJayma

RESTful API abstraction for Server Interconnection
Objective-C
6
star
62

hexer

Hex.pm integration in escript format.
Erlang
6
star
63

hexer.mk

erlang.mk plugin for hexer
Makefile
5
star
64

android_guidelines

Inaka's Android Development Guidelines
5
star
65

elvis.mk

3rd party erlang.mk plug-in for Elvis
Shell
5
star
66

plixir

Poker + Elixir + Phoenix
CSS
5
star
67

ios_guidelines

Inaka's iOS Coding Guidelines
Objective-C
4
star
68

sumo_db_elasticsearch

ElasticSearch adapter for sumo_db
Erlang
4
star
69

tele_sign

Node.js library to send messages through http://www.telesign.com/
JavaScript
4
star
70

inaka.github.io

Inaka's Open Source Projects
HTML
3
star
71

sumo_db_riak

Riak adapter for sumo_db
Erlang
3
star
72

android-excercises

Quick test for Android candidates
3
star
73

sumo_db_mongo

MongoDB adapter for sumo_db
Erlang
2
star
74

Otec

A swift app to showcase our best open-source libraries
Swift
2
star
75

gold_fever-solver

A solver for the http://github.com/inaka/gold_fever game
Erlang
2
star
76

inaka.mk

erlang.mk extras that we generally use in all of our projects
Makefile
2
star
77

g2x

Graffle to XCode
Objective-C
2
star
78

beam_olympics-extended

Internal repo to keep secret beam_olympics tasks from the public view
Erlang
2
star
79

beam_olympics-solver

Solutions for beam_olympics
Elixir
2
star
80

emarkdown

Based on https://github.com/devinus/markdown - but for Erlang :)
C
2
star
81

sumo_db_mysql

MySQL adapter for sumo_db
Erlang
2
star
82

ruby_guidelines

Our own guidelines when it comes to ruby development
1
star
83

pokedex

Dumb repo to prove what we can do with sumo{_db|rest_}
Erlang
1
star
84

homebrew-formulas

Homebrew formulas for some of our tools
Ruby
1
star
85

updike

Run, rabbit, run
1
star
86

ios-scripts

Helper scripts that you can use in your iOS apps
Shell
1
star
87

INSocketListener

SSE Socket Listener for Objective-C
Objective-C
1
star