danielberkompas / elasticsearch-elixir

No-nonsense Elasticsearch library for Elixir
MIT License
422 stars 73 forks source link

Different HTTP Client? #112

Open Stratus3D opened 1 year ago

Stratus3D commented 1 year ago

I'm interested in trying elasticsearch-elixir with Req or Finch for performance reasons. I see there is an ElasticSearch.API behavior for the HTTP client modules, but it looks like the types referenced by the behavior are specific to HTTPoison. Is elasticsearch-elixir designed to be used with other HTTP clients? And if so, can the HTTPoison specific types in the behavior be removed?

Thanks!

Ivor commented 1 year ago

Also running into this when trying to use a MockHTTP module with hammox. The contract is enforced to ensure the Mock returns the same types as what is specified in the behaviour. This means that it is not just about implementing with a different client, but also when using a mock for testing.

Stratus3D commented 1 year ago

I ended up solving this by doing something like this:

defmodule ClientImplementation do
  @behaviour Elasticsearch.API

  ...

  @impl true
  def request(config, method, url, data, opts) do
    # perform actual request here with whatever client you want...

    # elasticsearch-library assumes HTTPoison response, so we mimic an HTTPoison
    # struct here to keep Dialyzer happy
    {:ok, %{status_code: status, body: body}}
  end
end

Kind of ugly returning a map with certain values but it kept dialyzer happy in our project so that is what we did.