From dd244d77db5cc4f069f5968c329f0a1dc0340443 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Tue, 25 Feb 2025 14:38:20 +0100 Subject: [PATCH] Format codebase with styler and remove credo closes #78 --- .credo.exs | 143 ---------------------- .formatter.exs | 3 +- .github/workflows/ci.yml | 7 +- .tool-versions | 2 + lib/elastix/alias.ex | 10 +- lib/elastix/bulk.ex | 51 ++++---- lib/elastix/document.ex | 28 +++-- lib/elastix/http.ex | 9 +- lib/elastix/index.ex | 24 ++-- lib/elastix/json.ex | 20 +-- lib/elastix/mapping.ex | 31 ++--- lib/elastix/search.ex | 34 ++--- lib/elastix/snapshot/repository.ex | 15 +-- lib/elastix/snapshot/snapshot.ex | 30 ++--- mix.exs | 3 +- mix.lock | 1 + test/elastix/alias_test.exs | 1 + test/elastix/bulk_test.exs | 3 +- test/elastix/document_test.exs | 8 +- test/elastix/http_test.exs | 3 +- test/elastix/index_test.exs | 1 + test/elastix/mapping_test.exs | 3 +- test/elastix/search_test.exs | 7 +- test/elastix/snapshot/repository_test.exs | 1 + test/elastix/snapshot/snapshot_test.exs | 22 ++-- 25 files changed, 172 insertions(+), 288 deletions(-) delete mode 100644 .credo.exs create mode 100644 .tool-versions diff --git a/.credo.exs b/.credo.exs deleted file mode 100644 index 16925df..0000000 --- a/.credo.exs +++ /dev/null @@ -1,143 +0,0 @@ -# This file contains the configuration for Credo and you are probably reading -# this after creating it with `mix credo.gen.config`. -# -# If you find anything wrong or unclear in this file, please report an -# issue on GitHub: https://github.com/rrrene/credo/issues -# -%{ - # - # You can have as many configs as you like in the `configs:` field. - configs: [ - %{ - # - # Run any exec using `mix credo -C `. If no exec name is given - # "default" is used. - # - name: "default", - # - # These are the files included in the analysis: - files: %{ - # - # You can give explicit globs or simply directories. - # In the latter case `**/*.{ex,exs}` will be used. - # - included: ["lib/", "src/", "web/", "apps/"], - excluded: [~r"/_build/", ~r"/deps/"] - }, - # - # If you create your own checks, you must specify the source files for - # them here, so they can be loaded by Credo before running the analysis. - # - requires: [], - # - # If you want to enforce a style guide and need a more traditional linting - # experience, you can change `strict` to `true` below: - # - strict: false, - # - # If you want to use uncolored output by default, you can change `color` - # to `false` below: - # - color: true, - # - # You can customize the parameters of any check by adding a second element - # to the tuple. - # - # To disable a check put `false` as second element: - # - # {Credo.Check.Design.DuplicatedCode, false} - # - checks: [ - {Credo.Check.Consistency.ExceptionNames}, - {Credo.Check.Consistency.LineEndings}, - {Credo.Check.Consistency.ParameterPatternMatching}, - {Credo.Check.Consistency.SpaceAroundOperators}, - {Credo.Check.Consistency.SpaceInParentheses}, - {Credo.Check.Consistency.TabsOrSpaces}, - - # You can customize the priority of any check - # Priority values are: `low, normal, high, higher` - # - {Credo.Check.Design.AliasUsage, priority: :low}, - - # For some checks, you can also set other parameters - # - # If you don't want the `setup` and `test` macro calls in ExUnit tests - # or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just - # set the `excluded_macros` parameter to `[:schema, :setup, :test]`. - # - {Credo.Check.Design.DuplicatedCode, excluded_macros: []}, - - # You can also customize the exit_status of each check. - # If you don't want TODO comments to cause `mix credo` to fail, just - # set this value to 0 (zero). - # - {Credo.Check.Design.TagTODO, exit_status: 2}, - {Credo.Check.Design.TagFIXME}, - {Credo.Check.Readability.FunctionNames}, - {Credo.Check.Readability.LargeNumbers}, - {Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 100}, - {Credo.Check.Readability.ModuleAttributeNames}, - {Credo.Check.Readability.ModuleDoc}, - {Credo.Check.Readability.ModuleNames}, - {Credo.Check.Readability.ParenthesesOnZeroArityDefs}, - {Credo.Check.Readability.ParenthesesInCondition}, - {Credo.Check.Readability.PredicateFunctionNames}, - {Credo.Check.Readability.PreferImplicitTry}, - {Credo.Check.Readability.RedundantBlankLines}, - {Credo.Check.Readability.StringSigils}, - {Credo.Check.Readability.TrailingBlankLine}, - {Credo.Check.Readability.TrailingWhiteSpace}, - {Credo.Check.Readability.VariableNames}, - {Credo.Check.Readability.Semicolons}, - {Credo.Check.Readability.SpaceAfterCommas}, - {Credo.Check.Refactor.DoubleBooleanNegation}, - {Credo.Check.Refactor.CondStatements}, - {Credo.Check.Refactor.CyclomaticComplexity}, - {Credo.Check.Refactor.FunctionArity, max_arity: 6}, - {Credo.Check.Refactor.LongQuoteBlocks}, - {Credo.Check.Refactor.MatchInCondition}, - {Credo.Check.Refactor.NegatedConditionsInUnless}, - {Credo.Check.Refactor.NegatedConditionsWithElse}, - {Credo.Check.Refactor.Nesting}, - {Credo.Check.Refactor.PipeChainStart, false}, - {Credo.Check.Refactor.UnlessWithElse}, - {Credo.Check.Warning.BoolOperationOnSameValues}, - {Credo.Check.Warning.ExpensiveEmptyEnumCheck}, - {Credo.Check.Warning.IExPry}, - {Credo.Check.Warning.IoInspect}, - {Credo.Check.Warning.LazyLogging}, - {Credo.Check.Warning.OperationOnSameValues}, - {Credo.Check.Warning.OperationWithConstantResult}, - {Credo.Check.Warning.UnusedEnumOperation}, - {Credo.Check.Warning.UnusedFileOperation}, - {Credo.Check.Warning.UnusedKeywordOperation}, - {Credo.Check.Warning.UnusedListOperation}, - {Credo.Check.Warning.UnusedPathOperation}, - {Credo.Check.Warning.UnusedRegexOperation}, - {Credo.Check.Warning.UnusedStringOperation}, - {Credo.Check.Warning.UnusedTupleOperation}, - {Credo.Check.Warning.RaiseInsideRescue}, - - # Controversial and experimental checks (opt-in, just remove `, false`) - # - {Credo.Check.Refactor.ABCSize, false}, - {Credo.Check.Refactor.AppendSingleItem, false}, - {Credo.Check.Refactor.VariableRebinding, false}, - {Credo.Check.Warning.MapGetUnsafePass, false}, - {Credo.Check.Consistency.MultiAliasImportRequireUse, false}, - - # Deprecated checks (these will be deleted after a grace period) - # - {Credo.Check.Readability.Specs, false}, - {Credo.Check.Warning.NameRedeclarationByAssignment, false}, - {Credo.Check.Warning.NameRedeclarationByCase, false}, - {Credo.Check.Warning.NameRedeclarationByDef, false}, - {Credo.Check.Warning.NameRedeclarationByFn, false} - - # Custom checks can be created using `mix credo.gen.check`. - # - ] - } - ] -} diff --git a/.formatter.exs b/.formatter.exs index 4364510..7ea486c 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -1,5 +1,6 @@ # Used by "mix format" [ inputs: ["{mix,.credo,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], - line_length: 90 + line_length: 120, + plugins: [Styler] ] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0bb81ea..276bf64 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,4 +33,9 @@ jobs: elixir-version: ${{matrix.elixir}} - run: mix deps.get - run: mix compile --warnings-as-errors - - run: mix test + - name: Check formatting + run: mix format --check-formatted + - name: Run credo + run: mix credo --strict + - name: Run tests + run: mix test diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..0b6770c --- /dev/null +++ b/.tool-versions @@ -0,0 +1,2 @@ +elixir 1.18.2-otp-27 +erlang 27.2.3 diff --git a/lib/elastix/alias.ex b/lib/elastix/alias.ex index b8212ce..2347d42 100644 --- a/lib/elastix/alias.ex +++ b/lib/elastix/alias.ex @@ -5,7 +5,9 @@ defmodule Elastix.Alias do [Aliases documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html) """ import Elastix.HTTP, only: [prepare_url: 2] - alias Elastix.{HTTP, JSON} + + alias Elastix.HTTP + alias Elastix.JSON @doc """ Excepts a list of actions for the `actions` parameter. @@ -17,7 +19,8 @@ defmodule Elastix.Alias do """ @spec post(elastic_url :: String.t(), actions :: list) :: HTTP.resp() def post(elastic_url, actions) do - prepare_url(elastic_url, ["_aliases"]) + elastic_url + |> prepare_url(["_aliases"]) |> HTTP.post(JSON.encode!(%{actions: actions})) end @@ -30,7 +33,8 @@ defmodule Elastix.Alias do """ @spec get(elastic_url :: String.t(), name :: String.t()) :: HTTP.resp() def get(elastic_url, name \\ "") do - prepare_url(elastic_url, ["_alias", name]) + elastic_url + |> prepare_url(["_alias", name]) |> HTTP.get() end end diff --git a/lib/elastix/bulk.ex b/lib/elastix/bulk.ex index c5c83eb..5144bb9 100644 --- a/lib/elastix/bulk.ex +++ b/lib/elastix/bulk.ex @@ -5,7 +5,9 @@ defmodule Elastix.Bulk do [Elastic documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html) """ import Elastix.HTTP, only: [prepare_url: 2] - alias Elastix.{HTTP, JSON} + + alias Elastix.HTTP + alias Elastix.JSON @doc """ Excepts a list of actions and sources for the `lines` parameter. @@ -15,20 +17,17 @@ defmodule Elastix.Bulk do iex> Elastix.Bulk.post("http://localhost:9200", [%{index: %{_id: "1"}}, %{user: "kimchy"}], index: "twitter", type: "tweet") {:ok, %HTTPoison.Response{...}} """ - @spec post( - elastic_url :: String.t(), - lines :: list, - opts :: Keyword.t(), - query_params :: Keyword.t() - ) :: HTTP.resp() + @spec post(elastic_url :: String.t(), lines :: list, opts :: Keyword.t(), query_params :: Keyword.t()) :: HTTP.resp() def post(elastic_url, lines, options \\ [], query_params \\ []) do data = - Enum.reduce(lines, [], fn l, acc -> ["\n", JSON.encode!(l) | acc] end) + lines + |> Enum.reduce([], fn l, acc -> ["\n", JSON.encode!(l) | acc] end) |> Enum.reverse() |> IO.iodata_to_binary() path = - Keyword.get(options, :index) + options + |> Keyword.get(:index) |> make_path(Keyword.get(options, :type), query_params) httpoison_options = Keyword.get(options, :httpoison_options, []) @@ -41,22 +40,15 @@ defmodule Elastix.Bulk do @doc """ Deprecated: use `post/4` instead. """ - @spec post_to_iolist( - elastic_url :: String.t(), - lines :: list, - opts :: Keyword.t(), - query_params :: Keyword.t() - ) :: HTTP.resp() + @spec post_to_iolist(elastic_url :: String.t(), lines :: list, opts :: Keyword.t(), query_params :: Keyword.t()) :: + HTTP.resp() def post_to_iolist(elastic_url, lines, options \\ [], query_params \\ []) do - IO.warn( - "This function is deprecated and will be removed in future releases; use Elastix.Bulk.post/4 instead." - ) + IO.warn("This function is deprecated and will be removed in future releases; use Elastix.Bulk.post/4 instead.") httpoison_options = Keyword.get(options, :httpoison_options, []) - (elastic_url <> - make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params)) - |> HTTP.put( + HTTP.put( + elastic_url <> make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params), Enum.map(lines, fn line -> JSON.encode!(line) <> "\n" end), [], httpoison_options @@ -67,18 +59,17 @@ defmodule Elastix.Bulk do Same as `post/4` but instead of sending a list of maps you must send raw binary data in the format described in the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html). """ - @spec post_raw( - elastic_url :: String.t(), - raw_data :: String.t(), - opts :: Keyword.t(), - query_params :: Keyword.t() - ) :: HTTP.resp() + @spec post_raw(elastic_url :: String.t(), raw_data :: String.t(), opts :: Keyword.t(), query_params :: Keyword.t()) :: + HTTP.resp() def post_raw(elastic_url, raw_data, options \\ [], query_params \\ []) do httpoison_options = Keyword.get(options, :httpoison_options, []) - (elastic_url <> - make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params)) - |> HTTP.put(raw_data, [], httpoison_options) + HTTP.put( + elastic_url <> make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params), + raw_data, + [], + httpoison_options + ) end @doc false diff --git a/lib/elastix/document.ex b/lib/elastix/document.ex index b0a2565..524495b 100644 --- a/lib/elastix/document.ex +++ b/lib/elastix/document.ex @@ -5,7 +5,9 @@ defmodule Elastix.Document do [Elastic documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html) """ import Elastix.HTTP, only: [prepare_url: 2] - alias Elastix.{HTTP, JSON} + + alias Elastix.HTTP + alias Elastix.JSON @doc """ (Re)Indexes a document with the given `id`. @@ -24,7 +26,8 @@ defmodule Elastix.Document do query_params :: Keyword.t() ) :: HTTP.resp() def index(elastic_url, index_name, type_name, id, data, query_params \\ []) do - prepare_url(elastic_url, make_path(index_name, type_name, query_params, id)) + elastic_url + |> prepare_url(make_path(index_name, type_name, query_params, id)) |> HTTP.put(JSON.encode!(data)) end @@ -44,7 +47,8 @@ defmodule Elastix.Document do query_params :: Keyword.t() ) :: HTTP.resp() def index_new(elastic_url, index_name, type_name, data, query_params \\ []) do - prepare_url(elastic_url, make_path(index_name, type_name, query_params)) + elastic_url + |> prepare_url(make_path(index_name, type_name, query_params)) |> HTTP.post(JSON.encode!(data)) end @@ -64,7 +68,8 @@ defmodule Elastix.Document do query_params :: Keyword.t() ) :: HTTP.resp() def get(elastic_url, index_name, type_name, id, query_params \\ []) do - prepare_url(elastic_url, make_path(index_name, type_name, query_params, id)) + elastic_url + |> prepare_url(make_path(index_name, type_name, query_params, id)) |> HTTP.get() end @@ -86,7 +91,8 @@ defmodule Elastix.Document do |> Enum.join("/") url = - prepare_url(elastic_url, [path, "_mget"]) + elastic_url + |> prepare_url([path, "_mget"]) |> HTTP.append_query_string(query_params) # HTTPoison does not provide an API for a GET request with a body. @@ -109,7 +115,8 @@ defmodule Elastix.Document do query_params :: Keyword.t() ) :: HTTP.resp() def delete(elastic_url, index_name, type_name, id, query_params \\ []) do - prepare_url(elastic_url, make_path(index_name, type_name, query_params, id)) + elastic_url + |> prepare_url(make_path(index_name, type_name, query_params, id)) |> HTTP.delete() end @@ -124,7 +131,8 @@ defmodule Elastix.Document do query_params :: Keyword.t() ) :: HTTP.resp() def delete_matching(elastic_url, index_name, %{} = query, query_params \\ []) do - prepare_url(elastic_url, [index_name, "_delete_by_query"]) + elastic_url + |> prepare_url([index_name, "_delete_by_query"]) |> HTTP.append_query_string(query_params) |> HTTP.post(JSON.encode!(query)) end @@ -181,13 +189,11 @@ defmodule Elastix.Document do @doc false def make_path(index_name, type_name, query_params) do - "/#{index_name}/#{type_name}" - |> HTTP.append_query_string(query_params) + HTTP.append_query_string("/#{index_name}/#{type_name}", query_params) end @doc false def make_path(index_name, type_name, query_params, id, suffix \\ nil) do - "/#{index_name}/#{type_name}/#{id}/#{suffix}" - |> HTTP.append_query_string(query_params) + HTTP.append_query_string("/#{index_name}/#{type_name}/#{id}/#{suffix}", query_params) end end diff --git a/lib/elastix/http.ex b/lib/elastix/http.ex index 8f1c00b..d7a95ea 100644 --- a/lib/elastix/http.ex +++ b/lib/elastix/http.ex @@ -3,12 +3,13 @@ defmodule Elastix.HTTP do A thin [HTTPoison](https://github.com/edgurgel/httpoison) wrapper. """ use HTTPoison.Base + alias Elastix.JSON @type resp :: {:ok, HTTPoison.Response.t()} | {:error, HTTPoison.Error.t()} @doc false - def prepare_url(url, path) when is_binary(path), do: URI.merge(url, path) |> to_string + def prepare_url(url, path) when is_binary(path), do: url |> URI.merge(path) |> to_string() def prepare_url(url, parts) when is_list(parts), do: prepare_url(url, Path.join(parts)) @doc false @@ -25,8 +26,8 @@ defmodule Elastix.HTTP do full_headers = headers - |> add_content_type_header - |> add_shield_header + |> add_content_type_header() + |> add_shield_header() |> add_custom_headers(method, full_url, body) options = Keyword.merge(default_httpoison_options(), options) @@ -54,7 +55,7 @@ defmodule Elastix.HTTP do def process_response_body(""), do: "" def process_response_body(body) do - case body |> to_string |> JSON.decode() do + case body |> to_string() |> JSON.decode() do {:error, _} -> body {:ok, decoded} -> decoded end diff --git a/lib/elastix/index.ex b/lib/elastix/index.ex index 0bf208f..fdc5bbc 100644 --- a/lib/elastix/index.ex +++ b/lib/elastix/index.ex @@ -5,7 +5,9 @@ defmodule Elastix.Index do [Elastic documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html) """ import Elastix.HTTP, only: [prepare_url: 2] - alias Elastix.{HTTP, JSON} + + alias Elastix.HTTP + alias Elastix.JSON @doc """ Creates a new index. @@ -17,7 +19,8 @@ defmodule Elastix.Index do """ @spec create(elastic_url :: String.t(), name :: String.t(), data :: map) :: HTTP.resp() def create(elastic_url, name, data) do - prepare_url(elastic_url, name) + elastic_url + |> prepare_url(name) |> HTTP.put(JSON.encode!(data)) end @@ -31,7 +34,8 @@ defmodule Elastix.Index do """ @spec delete(elastic_url :: String.t(), name :: String.t()) :: HTTP.resp() def delete(elastic_url, name) do - prepare_url(elastic_url, name) + elastic_url + |> prepare_url(name) |> HTTP.delete() end @@ -45,7 +49,8 @@ defmodule Elastix.Index do """ @spec get(elastic_url :: String.t(), name :: String.t()) :: HTTP.resp() def get(elastic_url, name) do - prepare_url(elastic_url, name) + elastic_url + |> prepare_url(name) |> HTTP.get() end @@ -64,7 +69,7 @@ defmodule Elastix.Index do @spec exists?(elastic_url :: String.t(), name :: String.t()) :: {:ok, boolean()} | {:error, HTTPoison.Error.t()} def exists?(elastic_url, name) do - case prepare_url(elastic_url, name) |> HTTP.head() do + case elastic_url |> prepare_url(name) |> HTTP.head() do {:ok, response} -> case response.status_code do 200 -> {:ok, true} @@ -87,7 +92,8 @@ defmodule Elastix.Index do """ @spec refresh(elastic_url :: String.t(), name :: String.t()) :: HTTP.resp() def refresh(elastic_url, name) do - prepare_url(elastic_url, [name, "_refresh"]) + elastic_url + |> prepare_url([name, "_refresh"]) |> HTTP.post("") end @@ -102,7 +108,8 @@ defmodule Elastix.Index do """ @spec open(elastic_url :: String.t(), name :: String.t()) :: HTTP.resp() def open(elastic_url, name) do - prepare_url(elastic_url, [name, "_open"]) + elastic_url + |> prepare_url([name, "_open"]) |> HTTP.post("") end @@ -117,7 +124,8 @@ defmodule Elastix.Index do """ @spec close(elastic_url :: String.t(), name :: String.t()) :: HTTP.resp() def close(elastic_url, name) do - prepare_url(elastic_url, [name, "_close"]) + elastic_url + |> prepare_url([name, "_close"]) |> HTTP.post("") end end diff --git a/lib/elastix/json.ex b/lib/elastix/json.ex index b621a0c..67a1d31 100644 --- a/lib/elastix/json.ex +++ b/lib/elastix/json.ex @@ -1,14 +1,4 @@ defmodule Elastix.JSON do - defmodule Codec do - @moduledoc """ - A behaviour for JSON serialization. - """ - - @callback encode!(data :: any) :: iodata - - @callback decode(json :: iodata, opts :: []) :: {:ok, any} | {:error, :invalid} - end - @moduledoc """ A wrapper for JSON libraries with Poison as the default implementation. @@ -35,6 +25,16 @@ defmodule Elastix.JSON do ``` """ + defmodule Codec do + @moduledoc """ + A behaviour for JSON serialization. + """ + + @callback encode!(data :: any) :: iodata + + @callback decode(json :: iodata, opts :: []) :: {:ok, any} | {:error, :invalid} + end + @doc false def encode!(data) do codec().encode!(data) diff --git a/lib/elastix/mapping.ex b/lib/elastix/mapping.ex index aa24ad6..767e3c9 100644 --- a/lib/elastix/mapping.ex +++ b/lib/elastix/mapping.ex @@ -5,7 +5,9 @@ defmodule Elastix.Mapping do [Elastic documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html) """ import Elastix.HTTP, only: [prepare_url: 2] - alias Elastix.{HTTP, JSON} + + alias Elastix.HTTP + alias Elastix.JSON @doc """ Creates a new mapping. @@ -25,9 +27,9 @@ defmodule Elastix.Mapping do ) :: HTTP.resp() def put(elastic_url, index_names, type_name, data, query_params \\ []) - def put(elastic_url, index_names, type_name, data, query_params) - when is_list(index_names) do - prepare_url(elastic_url, make_path(index_names, [type_name], query_params)) + def put(elastic_url, index_names, type_name, data, query_params) when is_list(index_names) do + elastic_url + |> prepare_url(make_path(index_names, [type_name], query_params)) |> HTTP.put(JSON.encode!(data)) end @@ -50,19 +52,17 @@ defmodule Elastix.Mapping do ) :: HTTP.resp() def get(elastic_url, index_names, type_names, query_params \\ []) - def get(elastic_url, index_names, type_names, query_params) - when is_list(type_names) and is_list(index_names) do - prepare_url(elastic_url, make_path(index_names, type_names, query_params)) + def get(elastic_url, index_names, type_names, query_params) when is_list(type_names) and is_list(index_names) do + elastic_url + |> prepare_url(make_path(index_names, type_names, query_params)) |> HTTP.get() end - def get(elastic_url, index_names, type_name, query_params) - when is_list(index_names) do + def get(elastic_url, index_names, type_name, query_params) when is_list(index_names) do get(elastic_url, index_names, [type_name], query_params) end - def get(elastic_url, index_name, type_names, query_params) - when is_list(type_names) do + def get(elastic_url, index_name, type_names, query_params) when is_list(type_names) do get(elastic_url, [index_name], type_names, query_params) end @@ -79,7 +79,8 @@ defmodule Elastix.Mapping do """ @spec get_all(elastic_url :: String.t(), query_params :: Keyword.t()) :: HTTP.resp() def get_all(elastic_url, query_params \\ []) do - prepare_url(elastic_url, make_all_path(query_params)) + elastic_url + |> prepare_url(make_all_path(query_params)) |> HTTP.get() end @@ -98,9 +99,9 @@ defmodule Elastix.Mapping do ) :: HTTP.resp() def get_all_with_type(elastic_url, type_names, query_params \\ []) - def get_all_with_type(elastic_url, type_names, query_params) - when is_list(type_names) do - prepare_url(elastic_url, make_all_path(type_names, query_params)) + def get_all_with_type(elastic_url, type_names, query_params) when is_list(type_names) do + elastic_url + |> prepare_url(make_all_path(type_names, query_params)) |> HTTP.get() end diff --git a/lib/elastix/search.ex b/lib/elastix/search.ex index 7b22260..6360e53 100644 --- a/lib/elastix/search.ex +++ b/lib/elastix/search.ex @@ -5,7 +5,9 @@ defmodule Elastix.Search do [Elastic documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html) """ import Elastix.HTTP, only: [prepare_url: 2] - alias Elastix.{HTTP, JSON} + + alias Elastix.HTTP + alias Elastix.JSON @doc """ Makes a request to the `_search` or the `_msearch` endpoint depending on the type of @@ -28,11 +30,9 @@ defmodule Elastix.Search do types :: list, data :: map | list ) :: HTTP.resp() - def search(elastic_url, index, types, data) when is_list(data), - do: search(elastic_url, index, types, data, []) + def search(elastic_url, index, types, data) when is_list(data), do: search(elastic_url, index, types, data, []) - def search(elastic_url, index, types, data), - do: search(elastic_url, index, types, data, []) + def search(elastic_url, index, types, data), do: search(elastic_url, index, types, data, []) @doc """ Same as `search/4` but allows to specify query params and options for @@ -48,24 +48,25 @@ defmodule Elastix.Search do ) :: HTTP.resp() def search(elastic_url, index, types, data, query_params, options \\ []) - def search(elastic_url, index, types, data, query_params, options) - when is_list(index) do + def search(elastic_url, index, types, data, query_params, options) when is_list(index) do search(elastic_url, Enum.join(index, ","), types, data, query_params, options) end - def search(elastic_url, index, types, data, query_params, options) - when is_list(data) do + def search(elastic_url, index, types, data, query_params, options) when is_list(data) do data = - Enum.reduce(data, [], fn d, acc -> ["\n", JSON.encode!(d) | acc] end) + data + |> Enum.reduce([], fn d, acc -> ["\n", JSON.encode!(d) | acc] end) |> Enum.reverse() |> IO.iodata_to_binary() - prepare_url(elastic_url, make_path(index, types, query_params, "_msearch")) + elastic_url + |> prepare_url(make_path(index, types, query_params, "_msearch")) |> HTTP.post(data, [], options) end def search(elastic_url, index, types, data, query_params, options) do - prepare_url(elastic_url, make_path(index, types, query_params)) + elastic_url + |> prepare_url(make_path(index, types, query_params)) |> HTTP.post(JSON.encode!(data), [], options) end @@ -81,7 +82,8 @@ defmodule Elastix.Search do @spec scroll(elastic_url :: String.t(), data :: map, options :: Keyword.t()) :: HTTP.resp() def scroll(elastic_url, data, options \\ []) do - prepare_url(elastic_url, "_search/scroll") + elastic_url + |> prepare_url("_search/scroll") |> HTTP.post(JSON.encode!(data), [], options) end @@ -96,8 +98,7 @@ defmodule Elastix.Search do """ @spec count(elastic_url :: String.t(), index :: String.t(), types :: list, data :: map) :: HTTP.resp() - def count(elastic_url, index, types, data), - do: count(elastic_url, index, types, data, []) + def count(elastic_url, index, types, data), do: count(elastic_url, index, types, data, []) @doc """ Same as `count/4` but allows to specify query params and options for @@ -112,8 +113,7 @@ defmodule Elastix.Search do options :: Keyword.t() ) :: HTTP.resp() def count(elastic_url, index, types, data, query_params, options \\ []) do - (elastic_url <> make_path(index, types, query_params, "_count")) - |> HTTP.post(JSON.encode!(data), [], options) + HTTP.post(elastic_url <> make_path(index, types, query_params, "_count"), JSON.encode!(data), [], options) end @doc false diff --git a/lib/elastix/snapshot/repository.ex b/lib/elastix/snapshot/repository.ex index 2280e28..79efc84 100644 --- a/lib/elastix/snapshot/repository.ex +++ b/lib/elastix/snapshot/repository.ex @@ -6,13 +6,15 @@ defmodule Elastix.Snapshot.Repository do """ import Elastix.HTTP, only: [prepare_url: 2] - alias Elastix.{HTTP, JSON} + + alias Elastix.HTTP + alias Elastix.JSON @doc """ Registers a repository. """ @spec register(String.t(), String.t(), Map.t(), [tuple()]) :: - {:ok, %HTTPoison.Response{}} + {:ok, HTTPoison.Response.t()} def register(elastic_url, repo_name, data, query_params \\ []) do elastic_url |> prepare_url(make_path(repo_name, query_params)) @@ -22,7 +24,7 @@ defmodule Elastix.Snapshot.Repository do @doc """ Verifies a registered but unverified repository. """ - @spec verify(String.t(), String.t()) :: {:ok, %HTTPoison.Response{}} + @spec verify(String.t(), String.t()) :: {:ok, HTTPoison.Response.t()} def verify(elastic_url, repo_name) do elastic_url |> prepare_url([make_path(repo_name), "_verify"]) @@ -33,7 +35,7 @@ defmodule Elastix.Snapshot.Repository do If repo_name specified, will retrieve information about a registered repository. Otherwise, will retrieve information about all repositories. """ - @spec get(String.t(), String.t()) :: {:ok, %HTTPoison.Response{}} + @spec get(String.t(), String.t()) :: {:ok, HTTPoison.Response.t()} def get(elastic_url, repo_name \\ "_all") do elastic_url |> prepare_url(make_path(repo_name)) @@ -43,7 +45,7 @@ defmodule Elastix.Snapshot.Repository do @doc """ Removes the reference to the location where the snapshots are stored. """ - @spec delete(String.t(), String.t()) :: {:ok, %HTTPoison.Response{}} + @spec delete(String.t(), String.t()) :: {:ok, HTTPoison.Response.t()} def delete(elastic_url, repo_name) do elastic_url |> prepare_url(make_path(repo_name)) @@ -66,8 +68,7 @@ defmodule Elastix.Snapshot.Repository do defp _add_query_params(path, query_params) do query_string = - query_params - |> Enum.map_join("&", fn param -> + Enum.map_join(query_params, "&", fn param -> "#{elem(param, 0)}=#{elem(param, 1)}" end) diff --git a/lib/elastix/snapshot/snapshot.ex b/lib/elastix/snapshot/snapshot.ex index bcde965..2d91cc8 100644 --- a/lib/elastix/snapshot/snapshot.ex +++ b/lib/elastix/snapshot/snapshot.ex @@ -6,21 +6,16 @@ defmodule Elastix.Snapshot.Snapshot do """ import Elastix.HTTP, only: [prepare_url: 2] - alias Elastix.{HTTP, JSON} + + alias Elastix.HTTP + alias Elastix.JSON @doc """ Creates a snapshot. """ @spec create(String.t(), String.t(), String.t(), Map.t(), [tuple()], Keyword.t()) :: - {:ok, %HTTPoison.Response{}} - def create( - elastic_url, - repo_name, - snapshot_name, - data \\ %{}, - query_params \\ [], - options \\ [] - ) do + {:ok, HTTPoison.Response.t()} + def create(elastic_url, repo_name, snapshot_name, data \\ %{}, query_params \\ [], options \\ []) do elastic_url |> prepare_url(make_path(repo_name, snapshot_name, query_params)) |> HTTP.put(JSON.encode!(data), [], _make_httpoison_options(options)) @@ -30,7 +25,7 @@ defmodule Elastix.Snapshot.Snapshot do Restores a previously created snapshot. """ @spec restore(String.t(), String.t(), String.t(), Map.t(), Keyword.t()) :: - {:ok, %HTTPoison.Response{}} + {:ok, HTTPoison.Response.t()} def restore(elastic_url, repo_name, snapshot_name, data \\ %{}, options \\ []) do elastic_url |> prepare_url([make_path(repo_name, snapshot_name), "_restore"]) @@ -43,7 +38,7 @@ defmodule Elastix.Snapshot.Snapshot do in that repository. Otherwise, will retrieve the status of all snapshots. """ @spec status(String.t(), String.t(), String.t(), Keyword.t()) :: - {:ok, %HTTPoison.Response{}} + {:ok, HTTPoison.Response.t()} def status(elastic_url, repo_name \\ "", snapshot_name \\ "", options \\ []) do elastic_url |> prepare_url([make_path(repo_name, snapshot_name), "_status"]) @@ -57,7 +52,7 @@ defmodule Elastix.Snapshot.Snapshot do all snapshots. """ @spec get(String.t(), String.t(), String.t(), Keyword.t()) :: - {:ok, %HTTPoison.Response{}} + {:ok, HTTPoison.Response.t()} def get(elastic_url, repo_name \\ "", snapshot_name \\ "_all", options \\ []) do elastic_url |> prepare_url(make_path(repo_name, snapshot_name)) @@ -78,8 +73,7 @@ defmodule Elastix.Snapshot.Snapshot do {:ok, %HTTPoison.Response{...}} """ - @spec delete(String.t(), String.t(), String.t(), Keyword.t()) :: - {:ok, %HTTPoison.Response{}} + @spec delete(String.t(), String.t(), String.t(), Keyword.t()) :: {:ok, HTTPoison.Response.t()} def delete(elastic_url, repo_name, snapshot_name, options \\ []) do elastic_url |> prepare_url(make_path(repo_name, snapshot_name)) @@ -102,13 +96,11 @@ defmodule Elastix.Snapshot.Snapshot do defp _make_base_path(nil, nil), do: "/_snapshot" defp _make_base_path(repo_name, nil), do: "/_snapshot/#{repo_name}" - defp _make_base_path(repo_name, snapshot_name), - do: "/_snapshot/#{repo_name}/#{snapshot_name}" + defp _make_base_path(repo_name, snapshot_name), do: "/_snapshot/#{repo_name}/#{snapshot_name}" defp _add_query_params(path, query_params) do query_string = - query_params - |> Enum.map_join("&", fn param -> + Enum.map_join(query_params, "&", fn param -> "#{elem(param, 0)}=#{elem(param, 1)}" end) diff --git a/mix.exs b/mix.exs index 95c55e1..4f02b06 100644 --- a/mix.exs +++ b/mix.exs @@ -31,7 +31,8 @@ defmodule Elastix.Mixfile do {:credo, "~> 1.7", only: [:dev, :test]}, {:jason, "~> 1.4", optional: true}, {:httpoison, "~> 2.2"}, - {:retry, "~> 0.8", only: [:dev, :test]} + {:retry, "~> 0.8", only: [:dev, :test]}, + {:styler, ">= 0.0.0", only: [:dev, :test], runtime: false} ] end diff --git a/mix.lock b/mix.lock index 6260c0c..62eb1e8 100644 --- a/mix.lock +++ b/mix.lock @@ -19,5 +19,6 @@ "parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"}, "retry": {:hex, :retry, "0.18.0", "dc58ebe22c95aa00bc2459f9e0c5400e6005541cf8539925af0aa027dc860543", [:mix], [], "hexpm", "9483959cc7bf69c9e576d9dfb2b678b71c045d3e6f39ab7c9aa1489df4492d73"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, + "styler": {:hex, :styler, "1.4.0", "5944723d08afe4d38210b674d7e97dd1137a75968a85a633983cc308e86dc5f2", [:mix], [], "hexpm", "07de0e89c27490c8e469bb814d77ddaaa3283d7d8038501021d80a7705cf13e9"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, } diff --git a/test/elastix/alias_test.exs b/test/elastix/alias_test.exs index 24bb5bb..61f2ac7 100644 --- a/test/elastix/alias_test.exs +++ b/test/elastix/alias_test.exs @@ -1,5 +1,6 @@ defmodule Elastix.AliasTest do use ExUnit.Case + alias Elastix.Alias alias Elastix.Index diff --git a/test/elastix/bulk_test.exs b/test/elastix/bulk_test.exs index 5d04c4d..bbc6487 100644 --- a/test/elastix/bulk_test.exs +++ b/test/elastix/bulk_test.exs @@ -1,8 +1,9 @@ defmodule Elastix.BulkTest do use ExUnit.Case - alias Elastix.Index + alias Elastix.Bulk alias Elastix.Document + alias Elastix.Index @test_url Elastix.config(:test_url) @test_index Elastix.config(:test_index) diff --git a/test/elastix/document_test.exs b/test/elastix/document_test.exs index 3c5c614..06a3b6e 100644 --- a/test/elastix/document_test.exs +++ b/test/elastix/document_test.exs @@ -1,7 +1,11 @@ defmodule Elastix.DocumentTest do - require Logger use ExUnit.Case - alias Elastix.{Document, Index, Search} + + alias Elastix.Document + alias Elastix.Index + alias Elastix.Search + + require Logger @test_url Elastix.config(:test_url) @test_index Elastix.config(:test_index) diff --git a/test/elastix/http_test.exs b/test/elastix/http_test.exs index a21e475..778c198 100644 --- a/test/elastix/http_test.exs +++ b/test/elastix/http_test.exs @@ -1,5 +1,6 @@ defmodule Elastix.HTTPTest do use ExUnit.Case + alias Elastix.HTTP @test_url Elastix.config(:test_url) @@ -35,7 +36,7 @@ defmodule Elastix.HTTPTest do end test "process_response_body should parse the json body into a map" do - body = "{\"some\":\"json\"}" + body = ~s({"some":"json"}) assert HTTP.process_response_body(body) == %{"some" => "json"} end diff --git a/test/elastix/index_test.exs b/test/elastix/index_test.exs index 5c9687b..8cadfbe 100644 --- a/test/elastix/index_test.exs +++ b/test/elastix/index_test.exs @@ -1,5 +1,6 @@ defmodule Elastix.IndexTest do use ExUnit.Case + alias Elastix.Index @test_url Elastix.config(:test_url) diff --git a/test/elastix/mapping_test.exs b/test/elastix/mapping_test.exs index 82785d2..5f8d8e0 100644 --- a/test/elastix/mapping_test.exs +++ b/test/elastix/mapping_test.exs @@ -1,8 +1,9 @@ defmodule Elastix.MappingTest do use ExUnit.Case + + alias Elastix.Document alias Elastix.Index alias Elastix.Mapping - alias Elastix.Document @test_url Elastix.config(:test_url) @test_index Elastix.config(:test_index) diff --git a/test/elastix/search_test.exs b/test/elastix/search_test.exs index 84bb2e4..b2c74e7 100644 --- a/test/elastix/search_test.exs +++ b/test/elastix/search_test.exs @@ -1,8 +1,9 @@ defmodule Elastix.SearchTest do use ExUnit.Case - alias Elastix.Search - alias Elastix.Index + alias Elastix.Document + alias Elastix.Index + alias Elastix.Search alias HTTPoison.Response @test_url Elastix.config(:test_url) @@ -110,7 +111,7 @@ defmodule Elastix.SearchTest do {:ok, %Response{body: body}} = Search.scroll(@test_url, %{scroll: "1m", scroll_id: body["_scroll_id"]}) - assert length(body["hits"]["hits"]) === 0 + assert body["hits"]["hits"] == [] end test "count should return with status 200" do diff --git a/test/elastix/snapshot/repository_test.exs b/test/elastix/snapshot/repository_test.exs index 7c2f40c..efb615e 100644 --- a/test/elastix/snapshot/repository_test.exs +++ b/test/elastix/snapshot/repository_test.exs @@ -9,6 +9,7 @@ defmodule Elastix.Snapshot.RepositoryTest do """ use ExUnit.Case + alias Elastix.Snapshot.Repository @test_url Elastix.config(:test_url) diff --git a/test/elastix/snapshot/snapshot_test.exs b/test/elastix/snapshot/snapshot_test.exs index 9ef0473..43d8e03 100644 --- a/test/elastix/snapshot/snapshot_test.exs +++ b/test/elastix/snapshot/snapshot_test.exs @@ -12,8 +12,10 @@ defmodule Elastix.Snapshot.SnapshotTest do use ExUnit.Case use Retry + alias Elastix.Index - alias Elastix.Snapshot.{Repository, Snapshot} + alias Elastix.Snapshot.Repository + alias Elastix.Snapshot.Snapshot @test_url Elastix.config(:test_url) @test_repository "elastix_test_repository" @@ -81,7 +83,7 @@ defmodule Elastix.Snapshot.SnapshotTest do wait_for_completion: true ) - wait linear_backoff(500, 1) |> expiry(5_000) do + wait 500 |> linear_backoff(1) |> expiry(5_000) do {:ok, %{body: %{"snapshots" => snapshots}}} = Snapshot.status(@test_url, @test_repository, "elastix_test_snapshot_2") @@ -107,7 +109,7 @@ defmodule Elastix.Snapshot.SnapshotTest do wait_for_completion: true ) - wait linear_backoff(500, 1) |> expiry(5_000) do + wait 500 |> linear_backoff(1) |> expiry(5_000) do {:ok, %{body: %{"snapshots" => snapshots}}} = Snapshot.status(@test_url, @test_repository, "elastix_test_snapshot_1") @@ -135,7 +137,7 @@ defmodule Elastix.Snapshot.SnapshotTest do wait_for_completion: true ) - wait linear_backoff(500, 1) |> expiry(5_000) do + wait 500 |> linear_backoff(1) |> expiry(5_000) do {:ok, %{body: %{"snapshots" => snapshots}}} = Snapshot.status(@test_url, @test_repository, "elastix_test_snapshot_4") @@ -149,7 +151,7 @@ defmodule Elastix.Snapshot.SnapshotTest do Index.delete(@test_url, "elastix_test_index_2") end - wait linear_backoff(500, 1) |> expiry(5_000) do + wait 500 |> linear_backoff(1) |> expiry(5_000) do {:ok, %{status_code: 404}} = Index.get(@test_url, "elastix_test_index_1") {:ok, %{status_code: 404}} = Index.get(@test_url, "elastix_test_index_2") after @@ -159,7 +161,7 @@ defmodule Elastix.Snapshot.SnapshotTest do }) end - wait linear_backoff(500, 1) |> expiry(5_000) do + wait 500 |> linear_backoff(1) |> expiry(5_000) do {:ok, %{status_code: 200}} = Index.get(@test_url, "elastix_test_index_1") {:ok, %{status_code: 200}} = Index.get(@test_url, "elastix_test_index_2") end @@ -174,7 +176,7 @@ defmodule Elastix.Snapshot.SnapshotTest do wait_for_completion: true ) - wait linear_backoff(500, 1) |> expiry(5_000) do + wait 500 |> linear_backoff(1) |> expiry(5_000) do {:ok, %{status_code: 200, body: %{"snapshots" => snapshots}}} = Snapshot.status(@test_url, @test_repository, "elastix_test_snapshot_3") @@ -188,7 +190,7 @@ defmodule Elastix.Snapshot.SnapshotTest do Index.delete(@test_url, "elastix_test_index_4") end - wait linear_backoff(500, 1) |> expiry(5_000) do + wait 500 |> linear_backoff(1) |> expiry(5_000) do {:ok, %{status_code: 404}} = Index.get(@test_url, "elastix_test_index_3") {:ok, %{status_code: 404}} = Index.get(@test_url, "elastix_test_index_4") after @@ -198,7 +200,7 @@ defmodule Elastix.Snapshot.SnapshotTest do }) end - wait linear_backoff(500, 1) |> expiry(5_000) do + wait 500 |> linear_backoff(1) |> expiry(5_000) do {:ok, %{status_code: 200}} = Index.get(@test_url, "elastix_test_index_3") {:ok, %{status_code: 404}} = Index.get(@test_url, "elastix_test_index_4") end @@ -224,7 +226,7 @@ defmodule Elastix.Snapshot.SnapshotTest do indices: "elastix_test_index_5" }) - wait linear_backoff(500, 1) |> expiry(5_000) do + wait 500 |> linear_backoff(1) |> expiry(5_000) do {:ok, %{status_code: 200}} = Snapshot.status(@test_url, @test_repository, "elastix_test_snapshot_5") end