From b367a68357f98471453f0ae7859a8296f59b5742 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 28 Feb 2021 13:23:12 -0700 Subject: [PATCH 1/7] include prior command/input in entity protocol --- lib/entity.ex | 8 ++++++-- lib/httpact.ex | 6 +++--- test/httpact_test.exs | 9 ++------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/entity.ex b/lib/entity.ex index f199583..bed74f1 100755 --- a/lib/entity.ex +++ b/lib/entity.ex @@ -3,9 +3,13 @@ defprotocol HTTPact.Entity do Provides a contract for an API Wrapper to convert responses into a return type. """ @fallback_to_any true - def from_response(response) + def from_response(response, source_command) end defimpl HTTPact.Entity, for: Any do - def from_response(response), do: response + def from_response(response, _), do: response +end + +defimpl HTTPact.Entity, for: HTTPact.Request do + def from_response(request, _), do: request end diff --git a/lib/httpact.ex b/lib/httpact.ex index ea21de8..4bdad39 100755 --- a/lib/httpact.ex +++ b/lib/httpact.ex @@ -24,17 +24,17 @@ defmodule HTTPact do @spec execute(Request.t() | command(), http_client()) :: any def execute(%Request{} = request, client) when is_function(client) do client.(request) - |> Entity.from_response() + |> Entity.from_response(request) end def execute(%Request{} = request, client) when is_atom(client) do client.execute(request) - |> Entity.from_response() + |> Entity.from_response(request) end def execute(command, client) do Command.to_request(command) |> execute(client) - |> Entity.from_response() + |> Entity.from_response(command) end end diff --git a/test/httpact_test.exs b/test/httpact_test.exs index c426a39..84364cf 100755 --- a/test/httpact_test.exs +++ b/test/httpact_test.exs @@ -1,7 +1,7 @@ defmodule HTTPactTest do use ExUnit.Case doctest HTTPact - HTTPact.{Request, Response} + alias HTTPact.{Request, Response} test "We can mock an HTTP Client with anonymous functions for testing API Wrappers" do # Build a function that knows how to return responses from matched requests @@ -23,17 +23,12 @@ defmodule HTTPactTest do body: "test body", path: "https://testsite.test/tests", headers: [{"test", "header"}], - http_client: test_client, } - assert HTTPact.execute(request) == {:ok, %Response{ + assert HTTPact.execute(request, test_client) == {:ok, %Response{ status: 200, body: "it's kind of alive!", headers: [{"test", "header"}], }} end - - test "We can test an HTTP Client implementation by setting up a simple test server" do - assert false - end end From bd3961f17f30b9e1160ba35f7fbcc68ed9b4f784 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 28 Feb 2021 13:34:45 -0700 Subject: [PATCH 2/7] match on request directly for default protocol behaviour --- lib/entity.ex | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/entity.ex b/lib/entity.ex index bed74f1..eec25af 100755 --- a/lib/entity.ex +++ b/lib/entity.ex @@ -6,10 +6,6 @@ defprotocol HTTPact.Entity do def from_response(response, source_command) end -defimpl HTTPact.Entity, for: Any do - def from_response(response, _), do: response -end - defimpl HTTPact.Entity, for: HTTPact.Request do - def from_response(request, _), do: request + def from_response(response, %HTTPact.Request{}), do: response end From 90a4d69d32c5d55f361af1c531082a32192c22b8 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 28 Feb 2021 13:44:07 -0700 Subject: [PATCH 3/7] remove fallback to any --- lib/entity.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/entity.ex b/lib/entity.ex index eec25af..f307f5c 100755 --- a/lib/entity.ex +++ b/lib/entity.ex @@ -2,7 +2,6 @@ defprotocol HTTPact.Entity do @moduledoc """ Provides a contract for an API Wrapper to convert responses into a return type. """ - @fallback_to_any true def from_response(response, source_command) end From d7d21b8470dc2d85d707d2b3a43ed8d8e6955411 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 28 Feb 2021 13:52:24 -0700 Subject: [PATCH 4/7] impl for response instead - match on request --- lib/entity.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/entity.ex b/lib/entity.ex index f307f5c..b974086 100755 --- a/lib/entity.ex +++ b/lib/entity.ex @@ -5,6 +5,6 @@ defprotocol HTTPact.Entity do def from_response(response, source_command) end -defimpl HTTPact.Entity, for: HTTPact.Request do +defimpl HTTPact.Entity, for: HTTPact.Response do def from_response(response, %HTTPact.Request{}), do: response end From 26b6a9fd51ecce0f642907e194c2eca19aa3e260 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 28 Feb 2021 14:02:14 -0700 Subject: [PATCH 5/7] reverse entity protocol params for match on command in impl fors --- lib/entity.ex | 6 +++--- lib/httpact.ex | 16 +++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/entity.ex b/lib/entity.ex index b974086..c0dcd1c 100755 --- a/lib/entity.ex +++ b/lib/entity.ex @@ -2,9 +2,9 @@ defprotocol HTTPact.Entity do @moduledoc """ Provides a contract for an API Wrapper to convert responses into a return type. """ - def from_response(response, source_command) + def from_response(source_command, response) end -defimpl HTTPact.Entity, for: HTTPact.Response do - def from_response(response, %HTTPact.Request{}), do: response +defimpl HTTPact.Entity, for: HTTPact.Request do + def from_response(%HTTPact.Request{}, response), do: response end diff --git a/lib/httpact.ex b/lib/httpact.ex index 4bdad39..df8871b 100755 --- a/lib/httpact.ex +++ b/lib/httpact.ex @@ -23,18 +23,20 @@ defmodule HTTPact do @spec execute(Request.t() | command(), http_client()) :: any def execute(%Request{} = request, client) when is_function(client) do - client.(request) - |> Entity.from_response(request) + response = client.(request) + Entity.from_response(request, response) end def execute(%Request{} = request, client) when is_atom(client) do - client.execute(request) - |> Entity.from_response(request) + response = client.execute(request) + Entity.from_response(request, response) end def execute(command, client) do - Command.to_request(command) - |> execute(client) - |> Entity.from_response(command) + response = + Command.to_request(command) + |> execute(client) + + Entity.from_response(command, response) end end From 8ab4dab29c45ff69af32dd3dab622793fbf71fe9 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 28 Feb 2021 14:21:09 -0700 Subject: [PATCH 6/7] properly expect http clients to behave --- lib/httpact.ex | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/httpact.ex b/lib/httpact.ex index df8871b..14e272d 100755 --- a/lib/httpact.ex +++ b/lib/httpact.ex @@ -23,13 +23,21 @@ defmodule HTTPact do @spec execute(Request.t() | command(), http_client()) :: any def execute(%Request{} = request, client) when is_function(client) do - response = client.(request) - Entity.from_response(request, response) + with {:ok, response} <- client.(request) do + Entity.from_response(request, response) + else + {:error, _msg} = error -> error + :error -> {:error, "An unknown error occurred with the given HTTPact client"} + end end def execute(%Request{} = request, client) when is_atom(client) do - response = client.execute(request) - Entity.from_response(request, response) + with {:ok, response} <- client.execute(request) do + Entity.from_response(request, response) + else + {:error, _msg} = error -> error + :error -> {:error, "An unknown error occurred with the client: #{Atom.to_string(client)}"} + end end def execute(command, client) do From 69b5803d82370b2b5e3d23bc7fdbaf1634d1bc87 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 28 Feb 2021 14:27:45 -0700 Subject: [PATCH 7/7] adjust for passing test --- test/httpact_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/httpact_test.exs b/test/httpact_test.exs index 84364cf..1833ee1 100755 --- a/test/httpact_test.exs +++ b/test/httpact_test.exs @@ -25,10 +25,10 @@ defmodule HTTPactTest do headers: [{"test", "header"}], } - assert HTTPact.execute(request, test_client) == {:ok, %Response{ + assert HTTPact.execute(request, test_client) == %Response{ status: 200, body: "it's kind of alive!", headers: [{"test", "header"}], - }} + } end end