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

feat(config): ✅ Add options configuration for HTTP client #51

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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ if config_env() == :prod do # if you'll use this in prod environment
api_key: "xyz",
host: "localhost",
port: 8108,
scheme: "http"
scheme: "http",
options: %{}
...
```

> **Note**: The `options` key can be used to pass additional configuration options such as custom Finch instance or receive timeout settings. You can add any options supported by Req here. For more details check [Req documentation](https://hexdocs.pm/req/Req.Steps.html#run_finch/1-request-options).

> **Note**: If you use this for adding test in your app, you might want to add this in `config/test.exs`:

For Cloud hosted, you can generate and obtain the credentials from cluster instance admin interface:
Expand All @@ -72,7 +75,8 @@ config :ex_typesense,
api_key: "credential", # Admin API key
host: "111222333aaabbbcc-9.x9.typesense.net" # Nodes
port: 443,
scheme: "https"
scheme: "https",
options: %{}
```

#### Option 2: Set credentials from a map
Expand Down
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ if Mix.env() in [:dev, :test] do
api_key: "xyz",
host: "localhost",
port: 8108,
scheme: "http"
scheme: "http",
options: %{}
end
7 changes: 6 additions & 1 deletion lib/ex_typesense/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ defmodule ExTypesense.HttpClient do
@spec get_host :: String.t() | nil
def get_host, do: Application.get_env(:ex_typesense, :host)

@doc since: "0.6.1"
@spec get_options :: Keyword.t()
def get_options, do: Application.get_env(:ex_typesense, :options, %{})

@doc since: "0.1.0"
@spec get_scheme :: String.t() | nil
def get_scheme, do: Application.get_env(:ex_typesense, :scheme)
Expand Down Expand Up @@ -108,7 +112,8 @@ defmodule ExTypesense.HttpClient do
%Req.Request{
body: opts[:body],
method: opts[:method] || :get,
url: url
url: url,
options: get_options()
}
|> Req.Request.put_header("x-typesense-api-key", conn.api_key)
|> Req.Request.put_header("content-type", opts[:content_type] || "application/json")
Expand Down
32 changes: 32 additions & 0 deletions test/http_client_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule ExTypesense.HttpClientTest do
use ExUnit.Case, async: true

alias ExTypesense.HttpClient

setup do
# Backup the original configuration
original_config = Application.get_env(:ex_typesense, :options, %{})

on_exit(fn ->
# Restore the original configuration after each test
Application.put_env(:ex_typesense, :options, original_config)
end)
end

describe "get_options/0" do
test "returns the configured options" do
Application.put_env(:ex_typesense, :options, %{
finch: MyApp.CustomFinch,
receive_timeout: 5000
})

assert HttpClient.get_options() == %{finch: MyApp.CustomFinch, receive_timeout: 5000}
end

test "returns an empty map if options is not configured" do
Application.delete_env(:ex_typesense, :options)

assert HttpClient.get_options() == %{}
end
end
end