diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..1f438af --- /dev/null +++ b/config/config.exs @@ -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 diff --git a/lib/ex_typesense/connection.ex b/lib/ex_typesense/connection.ex index ebb7a55..9708245 100644 --- a/lib/ex_typesense/connection.ex +++ b/lib/ex_typesense/connection.ex @@ -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), diff --git a/lib/ex_typesense/http_client.ex b/lib/ex_typesense/http_client.ex index 684f598..009cdb0 100644 --- a/lib/ex_typesense/http_client.ex +++ b/lib/ex_typesense/http_client.ex @@ -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, [%{ diff --git a/test/test_helper.exs b/test/test_helper.exs index cfc20f1..869559e 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -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)