summaryrefslogtreecommitdiff
path: root/lib/couch.ex
blob: fdd85795ad37337f02d182e9096a8ab3e434acd6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
defmodule Couch do
  def get(db, doc) do
    config = Application.get_env(:lsg, :couch)
    url = [Keyword.get(config, :url), db, doc] |> Enum.join("/")
    user = Keyword.get(config, :user)
    pass = Keyword.get(config, :pass)
    client_options = Keyword.get(config, :client_options, [])
    headers = [{"accept", "application/json"}, {"user-agent", "beautte"}]
    options = [hackney: [:insecure, {:basic_auth, {user, pass}}]] ++ client_options
    case HTTPoison.get(url, headers, options) do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        {:ok, Poison.decode!(body)}
      {:ok, %HTTPoison.Response{status_code: 404}} ->
        {:error, :not_found}
      error -> {:error, {:couchdb_error, error}}
    end
  end
end