Skip to content

Commit

Permalink
Set default config for dev/test environments (#37)
Browse files Browse the repository at this point in the history
Refactor config usage and add missing returning Connection type.
  • Loading branch information
kianmeng authored Jul 14, 2024
1 parent 36eef5d commit 7581ebe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
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)

0 comments on commit 7581ebe

Please sign in to comment.