Skip to content

Commit

Permalink
improve on reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 11, 2023
1 parent a8f9ba4 commit 4a8672e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
3 changes: 0 additions & 3 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
# General application configuration
import Config

config :tesla, DockerEngineAPI.Connection,
adapter: Tesla.Adapter.Hackney

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
#
Expand Down
4 changes: 0 additions & 4 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@ import Config
# and secrets from environment variables or elsewhere. Do not define
# any compile-time configuration in here, as it won't be applied.
# The block below contains prod specific runtime configuration.

if base_url = System.get_env("DOCKER_ENGINE_API_BASE_URI") do
config :docker_engine_api, base_url: base_url
end
13 changes: 8 additions & 5 deletions lib/docker_engine_api/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule DockerEngineAPI.Connection do
"""
@spec new() :: Tesla.Env.client()
def new do
Tesla.client(middleware(), adapter())
Tesla.client(middleware(), adapter(adapter: Tesla.Adapter.Hackney))
end

@doc """
Expand All @@ -67,7 +67,7 @@ defmodule DockerEngineAPI.Connection do
def new(options) when is_list(options) do
options
|> middleware()
|> Tesla.client(adapter())
|> Tesla.client(adapter(options))
end


Expand Down Expand Up @@ -116,8 +116,11 @@ defmodule DockerEngineAPI.Connection do
@doc """
Returns the default adapter for this API.
"""
def adapter do
get_tesla_options()
|> Keyword.get(:adapter, nil)
def adapter(options \\ []) do
Keyword.get(
options,
:adapter,
get_tesla_options() |> Keyword.get(:adapter, nil)
)
end
end
12 changes: 10 additions & 2 deletions test/connection_test.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
defmodule DockerEngineAPI.ConnectionTest do
use ExUnit.Case

test "can list images" do
test "can list images with hackney adapter" do
{:ok, list} = DockerEngineAPI.Connection.new(
base_url: "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43"
base_url: "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43",
adapter: Tesla.Adapter.Hackney
)
|> 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()
end
end

0 comments on commit 4a8672e

Please sign in to comment.