From 39a7164a380b22562c479040f405f650f861f85e Mon Sep 17 00:00:00 2001 From: El Werbitzky Date: Tue, 4 Apr 2017 16:45:55 +0200 Subject: [PATCH] fix credo warnings --- .credo.exs | 2 ++ .gitignore | 1 + lib/elastix/bulk.ex | 14 ++++++++++---- lib/elastix/http.ex | 22 +++++++++------------- lib/elastix/search.ex | 6 +++--- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/.credo.exs b/.credo.exs index a7a8fd7..2657a0c 100644 --- a/.credo.exs +++ b/.credo.exs @@ -85,6 +85,8 @@ {Credo.Check.Warning.UnusedStringOperation}, {Credo.Check.Warning.UnusedTupleOperation}, {Credo.Check.Warning.OperationWithConstantResult}, + + {Credo.Check.Readability.Specs, false} ] } ] diff --git a/.gitignore b/.gitignore index 98f8244..eca81d3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /doc erl_crash.dump *.ez +doc diff --git a/lib/elastix/bulk.ex b/lib/elastix/bulk.ex index 0bccd81..9bb13b9 100644 --- a/lib/elastix/bulk.ex +++ b/lib/elastix/bulk.ex @@ -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 diff --git a/lib/elastix/http.ex b/lib/elastix/http.ex index 03a3fda..fb806da 100644 --- a/lib/elastix/http.ex +++ b/lib/elastix/http.ex @@ -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 diff --git a/lib/elastix/search.ex b/lib/elastix/search.ex index c29e86e..1de8935 100644 --- a/lib/elastix/search.ex +++ b/lib/elastix/search.ex @@ -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