Skip to content

Commit

Permalink
Drop client as function #176
Browse files Browse the repository at this point in the history
  • Loading branch information
teamon committed Feb 16, 2018
1 parent 377798d commit 47974f9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
10 changes: 3 additions & 7 deletions lib/tesla.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Tesla.Error do
end

defmodule Tesla.Env do
@type client :: Tesla.Client.t() | (t, stack -> t)
@type client :: Tesla.Client.t()
@type method :: :head | :get | :delete | :trace | :options | :post | :put | :patch
@type url :: binary
@type param :: binary | [{binary | atom, param}]
Expand Down Expand Up @@ -41,7 +41,6 @@ end

defmodule Tesla.Client do
@type t :: %__MODULE__{
fun: (Tesla.Env.t(), Tesla.Env.stack() -> Tesla.Env.t()) | nil,
pre: Tesla.Env.stack(),
post: Tesla.Env.stack()
}
Expand Down Expand Up @@ -88,15 +87,12 @@ defmodule Tesla do
end

@doc false
def execute(module, %{fun: fun, pre: pre, post: post} = client, options) do
def execute(module, %{pre: pre, post: post} = client, options) do
env = struct(Env, options ++ [__module__: module, __client__: client])
stack = pre ++ wrapfun(fun) ++ module.__middleware__ ++ post ++ [effective_adapter(module)]
stack = pre ++ module.__middleware__ ++ post ++ [effective_adapter(module)]
run(env, stack)
end

defp wrapfun(nil), do: []
defp wrapfun(fun), do: [{:fn, fun}]

@doc false
def effective_adapter(module) do
with nil <- adapter_per_module_from_config(module),
Expand Down
8 changes: 4 additions & 4 deletions lib/tesla/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ defmodule Tesla.Builder do

# fallback to keep backward compatibility
def unquote(method)(fun, url, body, options) when is_function(fun) and is_list(options) do
unquote(method)(%Tesla.Client{fun: fun}, url, body, options)
Tesla.Migration.client_function!()
end

if unquote(docs) do
Expand All @@ -199,7 +199,7 @@ defmodule Tesla.Builder do

# fallback to keep backward compatibility
def unquote(method)(fun, url, body) when is_function(fun) do
unquote(method)(%Tesla.Client{fun: fun}, url, body)
Tesla.Migration.client_function!()
end

if unquote(docs) do
Expand Down Expand Up @@ -252,7 +252,7 @@ defmodule Tesla.Builder do

# fallback to keep backward compatibility
def unquote(method)(fun, url, options) when is_function(fun) and is_list(options) do
unquote(method)(%Tesla.Client{fun: fun}, url, options)
Tesla.Migration.client_function!()
end

if unquote(docs) do
Expand All @@ -275,7 +275,7 @@ defmodule Tesla.Builder do

# fallback to keep backward compatibility
def unquote(method)(fun, url) when is_function(fun) do
unquote(method)(%Tesla.Client{fun: fun}, url)
Tesla.Migration.client_function!()
end

if unquote(docs) do
Expand Down
14 changes: 14 additions & 0 deletions lib/tesla/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Tesla.Migration do

@breaking_alias "https://github.com/teamon/tesla/wiki/0.x-to-1.0-Migration-Guide#dropped-aliases-support-159"
@breaking_headers_map "https://github.com/teamon/tesla/wiki/0.x-to-1.0-Migration-Guide#headers-are-now-a-list-160"
@breaking_client_fun "https://github.com/teamon/tesla/wiki/0.x-to-1.0-Migration-Guide#dropped-client-as-function-176"

def breaking_alias!(_kind, _name, nil), do: nil

Expand Down Expand Up @@ -80,6 +81,19 @@ defmodule Tesla.Migration do

def breaking_headers_map!(_middleware, _opts, _caller), do: nil

## CLIENT FUNCTION

def client_function! do
raise RuntimeError,
message: """
Using anonymous function as client has been removed.
Use `Tesla.build_client` instead
See #{@breaking_client_fun}
"""
end

## UTILS

defp elixir_module?(atom) do
Expand Down
9 changes: 9 additions & 0 deletions test/tesla/0.x_to_1.0_migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,13 @@ defmodule MigrationTest do
)
end
end

describe "Drop client as function #176" do
test "error when passing a function as client" do
client = fn env, next -> Tesla.run(env, next) end
assert_raise RuntimeError, fn ->
Tesla.get(client, "/")
end
end
end
end
16 changes: 0 additions & 16 deletions test/tesla_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,6 @@ defmodule TeslaTest do
assert response.body == "some-data"
end

test "request with client" do
client = fn env, next ->
env
|> Map.put(:url, "/prefix" <> env.url)
|> Tesla.run(next)
end

assert {:ok, response} = SimpleClient.get("/")
assert response.url == "/"
assert response.__client__ == %Tesla.Client{}

assert {:ok, response} = client |> SimpleClient.get("/")
assert response.url == "/prefix/"
assert response.__client__ == %Tesla.Client{fun: client}
end

test "better errors when given nil opts" do
assert_raise FunctionClauseError, fn ->
Tesla.get("/", nil)
Expand Down

0 comments on commit 47974f9

Please sign in to comment.