Skip to content

Commit

Permalink
Merge pull request #52 from kianmeng/remove-usage-of-mix-env
Browse files Browse the repository at this point in the history
Remove usage of Mix.env()
  • Loading branch information
jaeyson authored Sep 8, 2024
2 parents 23b9ab8 + 674b66a commit 40d15ba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
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

0 comments on commit 40d15ba

Please sign in to comment.