{"id":13507983,"url":"https://github.com/inaka/Dayron","last_synced_at":"2025-03-30T09:33:15.501Z","repository":{"id":57488488,"uuid":"56155672","full_name":"inaka/Dayron","owner":"inaka","description":"A repository `similar` to Ecto.Repo that maps to an underlying http client, sending requests to an external rest api instead of a database","archived":false,"fork":false,"pushed_at":"2017-10-10T16:11:06.000Z","size":158,"stargazers_count":159,"open_issues_count":16,"forks_count":20,"subscribers_count":36,"default_branch":"master","last_synced_at":"2024-11-01T07:33:24.077Z","etag":null,"topics":["elixir","elixir-lang"],"latest_commit_sha":null,"homepage":"http://inaka.net/blog/2016/05/24/introducing-dayron/","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inaka.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-13T13:47:02.000Z","updated_at":"2024-08-11T07:25:33.000Z","dependencies_parsed_at":"2022-08-29T15:01:47.518Z","dependency_job_id":null,"html_url":"https://github.com/inaka/Dayron","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inaka%2FDayron","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inaka%2FDayron/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inaka%2FDayron/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inaka%2FDayron/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inaka","download_url":"https://codeload.github.com/inaka/Dayron/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246301963,"owners_count":20755512,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","elixir-lang"],"created_at":"2024-08-01T02:00:44.952Z","updated_at":"2025-03-30T09:33:15.228Z","avatar_url":"https://github.com/inaka.png","language":"Elixir","readme":"# Dayron\n\n[![Build Status](https://travis-ci.org/inaka/Dayron.svg?branch=master)](https://travis-ci.org/inaka/Dayron)\n[![Deps Status](https://beta.hexfaktor.org/badge/all/github/inaka/Dayron.svg)](https://beta.hexfaktor.org/github/inaka/Dayron)\n[![Inline docs](https://inch-ci.org/github/inaka/Dayron.svg)](https://inch-ci.org/github/inaka/Dayron)\n[![Coverage Status](https://coveralls.io/repos/github/inaka/Dayron/badge.svg?branch=master)](https://coveralls.io/github/inaka/Dayron?branch=master)\n[![Twitter](https://img.shields.io/badge/twitter-@inaka-blue.svg?style=flat)](http://twitter.com/inaka)\n\nDayron is a flexible library to interact with RESTful APIs and map resources to Elixir data structures. It works _similar_ of [Ecto.Repo](https://github.com/elixir-lang/ecto) but, instead of retrieving data from a database, it has underlying http clients to retrieve data from external HTTP servers.\n\n## Installation\n\n1. Add Dayron to your list of dependencies in `mix.exs`:\n\n  ```elixir\n  def deps do\n    [{:dayron, \"~\u003e 0.1\"}]\n  end\n  ```\n\n1. Ensure Dayron is started before your application:\n\n  ```elixir\n  def application do\n    [applications: [:dayron]]\n  end\n  ```\n\n1. Then run `mix deps.get` in your shell to fetch the dependencies.\n\n## Getting Started\n\nDayron requires a configuration entry with at least the external `url` attribute:\n\n  ```elixir\n  # In your config/config.exs file\n  config :my_app, MyApp.RestRepo,\n    url: \"http://api.example.com\"\n  ```\n\nThen you must define `MyApp.RestRepo` somewhere in your application with:\n\n\n  ```elixir\n  # Somewhere in your application\n  defmodule MyApp.RestRepo do\n    use Dayron.Repo, otp_app: :my_app\n  end\n  ```\n\n## Defining Models\n\n### With modules and structs\n\nDayron 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.\n\n  ```elixir\n  defmodule MyApp.User do\n    # api requests to http://api.example.com/users\n    use Dayron.Model, resource: \"users\"\n\n    # struct defining model attributes\n    defstruct name: \"\", age: 0\n  end\n  ```\n\n### Reusing Ecto Models\n\nDayron 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.\n\n  ```elixir\n  defmodule MyApp.User do\n    use Ecto.Schema\n    use Dayron.Model\n\n    # dayron requests to http://api.example.com/users\n    schema \"users\" do\n      field :name, :string\n      field :age, :integer, default: 0\n    end\n  end\n  ```\n\n## Retrieving Data\n\nAfter 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`:\n\n  ```elixir\n  defmodule MyApp.UsersController do\n    use MyApp.Web, :controller\n\n    alias MyApp.User\n    alias MyApp.RestRepo\n\n    def index(conn, params)\n      conn\n      |\u003e assign(:users, RestRepo.all(User))\n      |\u003e render(\"index.html\")\n    end\n\n    def show(conn, %{\"id\" =\u003e id}) do\n      case RestRepo.get(User, id) do\n        nil  -\u003e put_status(conn, :not_found)\n        user -\u003e render conn, \"show.html\", user: user\n      end\n    end\n  ```\n\n## Generating modules\n\nYou can generate Dayron modules using the task `dayron.gen.model`\n\n`mix dayron.gen.model User users name age:integer`\n\nThe first argument is the module name followed by the resource path. The generated model will contain:\n\n* a model file in lib/your_app/models\n* a test file in test/your_app/models\n\nBoth the model and the test path can be configured using the Dayron ```generators```\nconfig.\n\n```elixir\nconfig :dayron, :generators,\n  models_path: \"web/models\",\n  models_test_path: \"test/models\"\n```\n\nThe model fields are given using `name:type` syntax\nwhere types can be one of the following:\n\n    :array, :integer, :float, :boolean, :string\n\nOmitting the type makes it default to `:string`\n\n\n\n## Extra Configuration\n\n### Request Headers\n\nUsing 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:\n\n  ```elixir\n  # In your config/config.exs file\n  config :my_app, MyApp.Dayron,\n    url: \"https://api.example.com\",\n    headers: [\"access-token\": \"my-api-token\"]\n  ```\n\n### HTTP Client Adapter\n\nCurrently the only adapter available is [HTTPoisonAdapter](https://github.com/inaka/Dayron/blob/master/lib/dayron/adapters/httpoison_adapter.ex), which uses [HTTPoison](https://github.com/edgurgel/httpoison) and [hackney](https://github.com/benoitc/hackney) to manage HTTP requests.\n\nYou can also create your own adapter implementing the [Dyron.Adapter](https://github.com/inaka/Dayron/blob/master/lib/dayron/adapter.ex) behavior, and changing the configuration to something like:\n\n  ```elixir\n  # In your config/config.exs file\n  config :my_app, MyApp.Dayron,\n    url: \"https://api.example.com\",\n    adapter: MyDayronAdapter\n  ```\n\n## Important links\n\n  * [Online Documentation](http://hexdocs.pm/dayron)\n  * [Examples](https://github.com/inaka/Dayron/tree/master/examples)\n\n## Contributing\n\nPull request are very wellcome, but before opening a new one, please [open an issue](https://github.com/inaka/Dayron/issues/new) first.\n\nIf you want to send us a pull request, get the project working in you local:\n\n  ```\n  $ git clone https://github.com/inaka/Dayron.git\n  $ cd Dayron\n  $ mix deps.get\n  $ mix test\n  ```\n\nCreate a branch with the issue name and once you're ready (new additions and tests passing), submit your pull request!\n\n## Building docs\n\n  ```\n  $ MIX_ENV=docs mix docs\n  ```\n\n## Contact Us\n\nIf you find any **bugs** or have a **problem** while using this library, please [open an issue](https://github.com/inaka/Dayron/issues/new) in this repo (or a pull request).\n\nYou can also check all of our open-source projects at [inaka.github.io](https://inaka.github.io).\n\n## Copyright and License\n\nCopyright (c) 2016, Inaka.\n\nDayron source code is licensed under the [Apache 2 License](LICENSE).\n","funding_links":[],"categories":["Framework Components"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finaka%2FDayron","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finaka%2FDayron","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finaka%2FDayron/lists"}