diff --git a/lib/tesla.ex b/lib/tesla.ex index 556bf0d9..02e13745 100644 --- a/lib/tesla.ex +++ b/lib/tesla.ex @@ -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}] @@ -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() } @@ -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), diff --git a/lib/tesla/builder.ex b/lib/tesla/builder.ex index 22dba265..b8331fc8 100644 --- a/lib/tesla/builder.ex +++ b/lib/tesla/builder.ex @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/tesla/migration.ex b/lib/tesla/migration.ex index 0575e1b4..213efe29 100644 --- a/lib/tesla/migration.ex +++ b/lib/tesla/migration.ex @@ -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 @@ -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 diff --git a/test/tesla/0.x_to_1.0_migration_test.exs b/test/tesla/0.x_to_1.0_migration_test.exs index 527361d7..ce432402 100644 --- a/test/tesla/0.x_to_1.0_migration_test.exs +++ b/test/tesla/0.x_to_1.0_migration_test.exs @@ -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 diff --git a/test/tesla_test.exs b/test/tesla_test.exs index e278d124..3f1285ab 100644 --- a/test/tesla_test.exs +++ b/test/tesla_test.exs @@ -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)