@@ -20,33 +20,52 @@ defmodule OpenApiTypesense.Connection do
2020 Setting new connection or using the default config.
2121
2222 > #### On using this function {: .info}
23- > Functions e.g. `OpenApiTypesense.Health.health` don't need to explicitly pass this
23+ > Functions e.g. `OpenApiTypesense.Health.health/0 ` don't need to explicitly pass this
2424 > unless you want to use another connection. Also, `api_key` is hidden when invoking
2525 > this function.
2626
2727 ## Examples
28+
2829 iex> alias OpenApiTypesense.Connection
29-
30+
3031 iex> conn = Connection.new()
3132 %OpenApiTypesense.Connection{
3233 host: "localhost",
3334 port: 8108,
3435 scheme: "http",
3536 ...
3637 }
38+
39+ iex> Connection.new(%{})
40+ ** (ArgumentError) Missing required fields: [:port, :scheme, :host, :api_key]
41+ (open_api_typesense 0.2.0) lib/open_api_typesense/connection.ex:56: OpenApiTypesense.Connection.new/1
42+ iex:2: (file)
43+
3744 """
3845 @ doc since: "0.2.0"
3946 @ spec new ( connection :: t ( ) | map ( ) ) :: % __MODULE__ { }
40- def new ( connection \\ defaults ( ) ) when is_map ( connection ) do
41- % __MODULE__ {
42- host: Map . get ( connection , :host ) ,
43- api_key: Map . get ( connection , :api_key ) ,
44- port: Map . get ( connection , :port ) ,
45- scheme: Map . get ( connection , :scheme )
46- }
47+
48+ def new ( connection \\ defaults ( ) )
49+
50+ def new ( connection ) when is_map ( connection ) do
51+ missing_fields = required_fields ( ) -- Map . keys ( connection )
52+
53+ if missing_fields == [ ] do
54+ struct ( __MODULE__ , connection )
55+ else
56+ raise ArgumentError , "Missing required fields: #{ inspect ( missing_fields ) } "
57+ end
58+ end
59+
60+ def new ( _ ) do
61+ raise ArgumentError , "Expected a map for connection options"
62+ end
63+
64+ @ spec required_fields :: map ( )
65+ defp required_fields do
66+ struct ( __MODULE__ , % { } ) |> Map . drop ( [ :__struct__ ] ) |> Map . keys ( )
4767 end
4868
49- @ doc since: "0.2.0"
5069 @ spec defaults :: map ( )
5170 defp defaults do
5271 % {
0 commit comments