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

Set default config for dev/test environments #37

Merged
merged 1 commit into from
Jul 14, 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
9 changes: 9 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Config

if Mix.env() in [:dev, :test] do
config :ex_typesense,
api_key: "xyz",
host: "localhost",
port: 8108,
scheme: "http"
end
27 changes: 18 additions & 9 deletions lib/ex_typesense/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,36 @@ defmodule ExTypesense.Connection do

alias ExTypesense.HttpClient

# @derive {Inspect, except: [:api_key]}
# defstruct host: HttpClient.get_host(),
# api_key: HttpClient.api_key(),
# port: HttpClient.get_port(),
# scheme: HttpClient.get_scheme()
defstruct [:host, :api_key, :port, :scheme]

@typedoc since: "0.4.0"
@type t() :: %{
host: String.t() | nil,
api_key: String.t() | nil,
host: binary() | nil,
api_key: binary() | nil,
port: non_neg_integer() | nil,
scheme: String.t() | nil
scheme: binary() | nil
}

@doc """
Fetches credentials either from application env or map.

## Examples

Using the default credential from local development Typesense instance:

iex> conn = ExTypesense.Connection.new()
%ExTypesense.Connection{
host: "localhost",
api_key: "xyz",
port: 8108,
scheme: "http"
}

"""
@doc since: "0.4.0"
@spec new(connection :: t() | map()) :: ExTypesense.Connection.t()
def new(connection \\ defaults()) when is_map(connection) do
%{
%ExTypesense.Connection{
host: Map.get(connection, :host),
api_key: Map.get(connection, :api_key),
port: Map.get(connection, :port),
Expand Down
2 changes: 2 additions & 0 deletions lib/ex_typesense/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ defmodule ExTypesense.HttpClient do
- `:content_type` `"Content-Type"` request header. Defaults to `"application/json"`.

## Examples

iex> connection = %ExTypesense.Connection{
...> host: "localhost",
...> api_key: "some_api_key",
...> port: "8108",
...> scheme: "http"
...> }

iex> HttpClient.request(connection, %{method: :post, path: "/collections", body: ExTypesense.TestSchema.Person})
{:ok,
[%{
Expand Down
14 changes: 0 additions & 14 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
Application.put_all_env(
ex_typesense: [
api_key: "xyz",
host: "localhost",
port: 8108,
scheme: "http"
]
)

ExUnit.start()

ExUnit.after_suite(fn %{failures: 0, skipped: 0, excluded: 0} ->
[:api_key, :host, :port, :scheme]
|> Enum.each(&Application.delete_env(:ex_typesense, &1))
end)