Skip to content

Commit

Permalink
Merge pull request #26 from GameAnalytics/arguments
Browse files Browse the repository at this point in the history
Change argument order, add default value
  • Loading branch information
legoscia authored Oct 22, 2018
2 parents 1cb16ef + 9daddc6 commit e5a2a81
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ q = from "my_datasource",
And then send it:

```elixir
ElixirDruid.post_query :default, q
ElixirDruid.post_query q, :default
```

`:default` is a configuration profile pointing to your Druid server.
See `config/config.exs`, where you can change the profile or add new
ones.

The default value for the profile argument is `:default`, so if you
only need a single configuration you can omit it:

```elixir
ElixirDruid.post_query q
```
13 changes: 7 additions & 6 deletions lib/elixir_druid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ defmodule ElixirDruid do
Documentation for ElixirDruid.
"""

@spec post_query(atom, %ElixirDruid.Query{}) :: {:ok, term()} |
@spec post_query(%ElixirDruid.Query{}, atom()) :: {:ok, term()} |
{:error, HTTPoison.Error.t() | Jason.DecodeError.t() | ElixirDruid.Error.t()}
def post_query(profile, query) do
def post_query(query, profile \\ :default) do
url_path = "/druid/v2"
body = ElixirDruid.Query.to_json query
headers = [{"Content-Type", "application/json"}]

request_and_decode(profile, :post, url_path, body, headers)
end

@spec post_query!(atom, %ElixirDruid.Query{}) :: term()
def post_query!(profile, query) do
case post_query(profile, query) do
@spec post_query!(%ElixirDruid.Query{}, atom()) :: term()
def post_query!(query, profile \\ :default) do
case post_query(query, profile) do
{:ok, response} -> response
{:error, error} -> raise error
end
Expand All @@ -47,7 +47,8 @@ defmodule ElixirDruid do

defp request_and_decode(profile, method, url_path, body, headers) do
broker_profiles = Application.get_env(:elixir_druid, :broker_profiles)
broker_profile = broker_profiles[profile]
broker_profile = broker_profiles[profile] ||
raise ArgumentError, "no broker profile with name #{profile}"
url = broker_profile[:base_url] <> url_path
options = http_options(url, broker_profile)

Expand Down

0 comments on commit e5a2a81

Please sign in to comment.