Skip to content

Commit

Permalink
fix credo warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
werbitzky committed Apr 4, 2017
1 parent 50056c8 commit 39a7164
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
{Credo.Check.Warning.UnusedStringOperation},
{Credo.Check.Warning.UnusedTupleOperation},
{Credo.Check.Warning.OperationWithConstantResult},

{Credo.Check.Readability.Specs, false}
]
}
]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/doc
erl_crash.dump
*.ez
doc
14 changes: 10 additions & 4 deletions lib/elastix/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ defmodule Elastix.Bulk do
alias Elastix.HTTP

def post(elastic_url, lines, options \\ [], query_params \\ []) do
elastic_url <> make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params)
|> HTTP.put(Enum.reduce(lines, "", fn (line, payload) -> payload <> Poison.encode!(line) <> "\n" end))
elastic_url <> make_path(
Keyword.get(options, :index), Keyword.get(options, :type), query_params)
|> HTTP.put(
Enum.reduce(
lines, "",
fn (line, payload) -> payload <> Poison.encode!(line) <> "\n" end))
end

def post_to_iolist(elastic_url, lines, options \\ [], query_params \\ []) do
elastic_url <> make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params)
elastic_url <> make_path(
Keyword.get(options, :index), Keyword.get(options, :type), query_params)
|> HTTP.put(Enum.map(lines, fn line -> Poison.encode!(line) <> "\n" end))
end

@doc false
def post_raw(elastic_url, raw_data, options \\ [], query_params \\ []) do
elastic_url <> make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params)
elastic_url <> make_path(
Keyword.get(options, :index), Keyword.get(options, :type), query_params)
|> HTTP.put(raw_data)
end

Expand Down
22 changes: 9 additions & 13 deletions lib/elastix/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,39 @@ defmodule Elastix.HTTP do

@doc false
def request(method, url, body \\ "", headers \\ [], options \\ []) do
url =
if Keyword.has_key?(options, :params) do
query_url = if Keyword.has_key?(options, :params) do
url <> "?" <> URI.encode_query(options[:params])
else
url
end
url = process_url(to_string(url))
full_url = process_url(to_string(query_url))
body = process_request_body(body)

username = Elastix.config(:username)
password = Elastix.config(:password)

headers = headers
content_headers = headers
|> Keyword.put_new(:"Content-Type", "application/json; charset=UTF-8")

headers = cond do
# https://www.elastic.co/guide/en/shield/current/_using_elasticsearch_http_rest_clients_with_shield.html
Elastix.config(:shield) ->
Keyword.put(headers, :"Authorization", "Basic " <> Base.encode64("#{username}:#{password}"))
true ->
headers
full_headers = if Elastix.config(:shield) do
Keyword.put(content_headers, :"Authorization", "Basic " <> Base.encode64("#{username}:#{password}"))
else
content_headers
end

options = Keyword.merge(default_httpoison_options(), options)
HTTPoison.Base.request(
__MODULE__,
method,
url,
full_url,
body,
headers,
full_headers,
options,
&process_status_code/1,
&process_headers/1,
&process_response_body/1)
end


@doc false
def process_response_body(""), do: ""
def process_response_body(body) do
Expand Down
6 changes: 3 additions & 3 deletions lib/elastix/search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ defmodule Elastix.Search do
_ -> path_root <> "/" <> Enum.join types, ","
end

path = "#{path}/_search"
full_path = "#{path}/_search"

case query_params do
[] -> path
_ -> add_query_params(path, query_params)
[] -> full_path
_ -> add_query_params(full_path, query_params)
end
end

Expand Down

0 comments on commit 39a7164

Please sign in to comment.