Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of Mix.env() #52

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Config

if Mix.env() in [:dev, :test] do
if config_env() in [:dev, :test] do
config :ex_typesense,
api_key: "xyz",
host: "localhost",
port: 8108,
scheme: "http",
retry: :false,
options: %{}
end
32 changes: 14 additions & 18 deletions lib/ex_typesense/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,6 @@ defmodule ExTypesense.HttpClient do
@doc since: "0.4.0"
@spec request(Connection.t(), map()) :: {:ok, any()} | {:error, String.t()}
def request(conn, opts \\ %{}) do
# Req.Request.append_error_steps and its retry option are used here.
# options like retry, max_retries, etc. can be found in:
# https://hexdocs.pm/req/Req.Steps.html#retry/1
# NOTE: look at source code in Github
retry = fn request ->
if Mix.env() === :test do
{req, resp_or_err} = request

# disabled in order to cut time in tests
req = %{req | options: %{retry: false}}

Req.Steps.retry({req, resp_or_err})
else
Req.Steps.retry(request)
end
end

url =
%URI{
scheme: conn.scheme,
Expand All @@ -117,7 +100,7 @@ defmodule ExTypesense.HttpClient do
}
|> Req.Request.put_header("x-typesense-api-key", conn.api_key)
|> Req.Request.put_header("content-type", opts[:content_type] || "application/json")
|> Req.Request.append_error_steps(retry: retry)
|> Req.Request.append_error_steps(retry: &maybe_retry/1)
|> Req.Request.run!()

case response.status in 200..299 do
Expand All @@ -133,4 +116,17 @@ defmodule ExTypesense.HttpClient do
{:error, Jason.decode!(response.body)["message"]}
end
end

# Req.Request.append_error_steps and its retry option are used here.
# options like retry, max_retries, etc. can be found in:
# https://hexdocs.pm/req/Req.Steps.html#retry/1
# NOTE: look at source code in Github
defp maybe_retry({req, resp}) do
if Application.get_env(:ex_typesense, :retry) === false do
req = %{req | options: %{retry: false}}
Req.Steps.retry({req, resp})
else
Req.Steps.retry({req, resp})
end
end
end