Skip to content
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
39 changes: 29 additions & 10 deletions lib/open_api_typesense/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,52 @@ defmodule OpenApiTypesense.Connection do
Setting new connection or using the default config.

> #### On using this function {: .info}
> Functions e.g. `OpenApiTypesense.Health.health` don't need to explicitly pass this
> Functions e.g. `OpenApiTypesense.Health.health/0` don't need to explicitly pass this
> unless you want to use another connection. Also, `api_key` is hidden when invoking
> this function.

## Examples

iex> alias OpenApiTypesense.Connection

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

iex> Connection.new(%{})
** (ArgumentError) Missing required fields: [:port, :scheme, :host, :api_key]
(open_api_typesense 0.2.0) lib/open_api_typesense/connection.ex:56: OpenApiTypesense.Connection.new/1
iex:2: (file)

"""
@doc since: "0.2.0"
@spec new(connection :: t() | map()) :: %__MODULE__{}
def new(connection \\ defaults()) when is_map(connection) do
%__MODULE__{
host: Map.get(connection, :host),
api_key: Map.get(connection, :api_key),
port: Map.get(connection, :port),
scheme: Map.get(connection, :scheme)
}

def new(connection \\ defaults())

def new(connection) when is_map(connection) do
missing_fields = required_fields() -- Map.keys(connection)

if missing_fields == [] do
struct(__MODULE__, connection)
else
raise ArgumentError, "Missing required fields: #{inspect(missing_fields)}"
end
end

def new(_) do
raise ArgumentError, "Expected a map for connection options"
end

@spec required_fields :: map()
defp required_fields do
struct(__MODULE__, %{}) |> Map.drop([:__struct__]) |> Map.keys()
end

@doc since: "0.2.0"
@spec defaults :: map()
defp defaults do
%{
Expand Down
42 changes: 42 additions & 0 deletions test/open_api_typesense_connection_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule OpenApiTypesense.ConnectionTest do
use ExUnit.Case, async: true

alias OpenApiTypesense.Connection

test "new/0 using the default config to creates a connection struct" do
conn = Connection.new()

assert conn == %Connection{
api_key: "xyz",
host: "localhost",
port: 8108,
scheme: "http"
}
end

test "new/1 with custom fields creates a connection struct" do
conn =
Connection.new(%{
host: "otherhost",
port: 9200,
scheme: "https",
api_key: "myapikey"
})

assert conn == %Connection{
api_key: "myapikey",
host: "otherhost",
port: 9200,
scheme: "https"
}
end

test "new/1 with empty map raises ArgumentError" do

Check failure on line 34 in test/open_api_typesense_connection_test.exs

View workflow job for this annotation

GitHub Actions / test (27.1, 25, 1.14)

test new/1 with empty map raises ArgumentError (OpenApiTypesense.ConnectionTest)
msg = "Missing required fields: [:port, :scheme, :host, :api_key]"
assert_raise ArgumentError, msg, fn -> Connection.new(%{}) end
end

test "new/1 with invalid data type raises ArgumentError" do
assert_raise ArgumentError, fn -> Connection.new("invalid") end
end
end
Loading