defmodule Ondix.Error do defexception [:message] end defmodule Ondix.State do use Agent def start_link(token) do Agent.start_link(fn -> token end, name: __MODULE__) end def token() do Agent.get(__MODULE__, &(&1)) end end defmodule Ondix.Client do use HTTPoison.Base def with_token(token) do Ondix.State.start_link(token) Ondix.Client end def token(), do: Ondix.State.token() @url (case System.get_env("API_URL") do nil -> "https://api.orchestre-ile.com/api/v1/" a -> a end) def process_response_body(body) do d = body |> Poison.decode case d do {:ok, res} -> Ondix.Utils.atomize_map(res) error -> {:error, error} end end def fetch(url), do: get(@url <> url, [{"X-API-TOKEN", token()}]) |> extract def fetchNoPrefix(url), do: get(url, [{"X-API-TOKEN", token()}]) |> extract def extract({:ok, %{ body: body }}), do: body def extract(a), do: a def getOne(url, relations) do url |> Ondix.Utils.prepareRelations(relations) |> fetch end def getList(url, clauses, page) do url |> Ondix.Utils.prepareClauses(clauses) |> Ondix.Utils.preparePage(page) |> fetch |> Ondix.Collection.fromResponse() end def ping() do fetch("ping") end end