Skip to content

Commit

Permalink
implement proper default options, fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 12, 2023
1 parent 4a8672e commit cb3b96f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 68 deletions.
13 changes: 0 additions & 13 deletions config/config.exs

This file was deleted.

8 changes: 0 additions & 8 deletions config/runtime.exs

This file was deleted.

44 changes: 8 additions & 36 deletions lib/docker_engine_api/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@
defmodule DockerEngineAPI.Connection do
@moduledoc """
Handle Tesla connections for DockerEngineAPI.
Additional middleware can be set in the compile-time or runtime configuration:
config :tesla, DockerEngineAPI.Connection,
base_url: "http://localhost/v1.43",
adapter: Tesla.Adapter.Hackney
The default base URL can also be set as:
config :docker_engine_api,
:base_url, "http://localhost/v1.43"
"""

@default_base_url Application.compile_env(
:docker_engine_api,
:base_url,
"http://localhost/v1.43"
)

@default_options [
base_url: "http://localhost/v1.43",
adapter: Tesla.Adapter.Hackney
]

@typedoc """
The list of options that can be passed to new/1.
Expand All @@ -39,18 +26,6 @@ defmodule DockerEngineAPI.Connection do
@spec request(Tesla.Client.t(), [Tesla.option()]) :: Tesla.Env.result()
defdelegate request(client, options), to: Tesla

@doc """
Configure a client with no authentication.
### Returns
Tesla.Env.client
"""
@spec new() :: Tesla.Env.client()
def new do
Tesla.client(middleware(), adapter(adapter: Tesla.Adapter.Hackney))
end

@doc """
Configure a client that may have authentication.
Expand All @@ -64,8 +39,10 @@ defmodule DockerEngineAPI.Connection do
"""
@spec new(options) :: Tesla.Env.client()

def new(options) when is_list(options) do
def new(options \\ []) when is_list(options) do
options = @default_options |> Keyword.merge(options)
options
|> Keyword.merge(options)
|> middleware()
|> Tesla.client(adapter(options))
end
Expand All @@ -78,12 +55,7 @@ defmodule DockerEngineAPI.Connection do
"""
@spec middleware(options) :: [Tesla.Client.middleware()]
def middleware(options \\ []) do
base_url =
Keyword.get(
options,
:base_url,
Application.get_env(:docker_engine_api, :base_url, @default_base_url)
)
base_url = Keyword.get(options, :base_url)

tesla_options = get_tesla_options()
middleware = Keyword.get(tesla_options, :middleware, [])
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.30.6", "5f8b54854b240a2b55c9734c4b1d0dd7bdd41f71a095d42a70445c03cf05a281", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bd48f2ddacf4e482c727f9293d9498e0881597eae6ddc3d9562bd7923375109f"},
"hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"},
"httpoison": {:hex, :httpoison, "2.1.0", "655fd9a7b0b95ee3e9a3b535cf7ac8e08ef5229bab187fa86ac4208b122d934b", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "fc455cb4306b43827def4f57299b2d5ac8ac331cb23f517e734a4b78210a160c"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
Expand Down
20 changes: 9 additions & 11 deletions test/connection_test.exs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
defmodule DockerEngineAPI.ConnectionTest do
use ExUnit.Case

test "can list images with hackney adapter" do
{:ok, list} = DockerEngineAPI.Connection.new(
base_url: "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43",
adapter: Tesla.Adapter.Hackney
)
|> DockerEngineAPI.Api.Image.image_list()
test "can list images without specifying adapter (uses Hackney by default)" do
{:ok, list} =
DockerEngineAPI.Connection.new(base_url: "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43")
|> DockerEngineAPI.Api.Image.image_list()

assert is_list(list)
end

test "doesnt work without adapter" do
{:error, {:bad_scheme, ~c"http+unix"}} = DockerEngineAPI.Connection.new(
base_url: "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43"
)
|> DockerEngineAPI.Api.Image.image_list()
test "cannot use connection without proper base_url (default baseurl is localhost url)" do
{:error, :econnrefused} =
DockerEngineAPI.Connection.new()
|> DockerEngineAPI.Api.Image.image_list()
end
end

0 comments on commit cb3b96f

Please sign in to comment.