Repository: keathley/finch (elixir)

Issue:
accept %URI{} as well as binary for url()
https://hexdocs.pm/finch/Finch.html#t:url/0 defines a url as a binary(). I am accustomed to passing around %URI{} structs instead.

Is there a reason you wouldn't also accept (or perhaps only expect) a %URI{} struct?

BTW - love the library, just thought I'd ask.

Pull request description:
Accepts a URI struct in request/3/4/5/6
Closes #46 


Patch:
diff --git a/lib/finch.ex b/lib/finch.ex
index 21b759c..5a47775 100644
--- a/lib/finch.ex
+++ b/lib/finch.ex
@@ -66,7 +66,7 @@ defmodule Finch do
   @typedoc """
   A Uniform Resource Locator, the address of a resource on the Web.
   """
-  @type url() :: String.t()
+  @type url() :: String.t() | URI.t()
 
   @typedoc """
   A body associated with a request.
@@ -79,7 +79,7 @@ defmodule Finch do
   ## Options:
     * `:name` - The name of your Finch instance. This field is required.
 
-    * `:pools` - A map specifying the configuration for your pools. The keys should be URLs 
+    * `:pools` - A map specifying the configuration for your pools. The keys should be URLs
     provided as binaries, or the atom `:default` to provide a catch-all configuration to be used
     for any unspecified URLs. See "Pool Configuration Options" below for details on the possible
     map values. Default value is `%{default: [size: #{@default_pool_size}, count: #{
@@ -150,7 +150,16 @@ defmodule Finch do
   end
 
   defp parse_and_normalize_url(url) when is_binary(url) do
-    parsed_uri = URI.parse(url)
+    url
+    |> URI.parse()
+    |> parse_and_normalize_url()
+  end
+
+  defp parse_and_normalize_url(%URI{} = parsed_uri) do
+    normalize_uri(parsed_uri)
+  end
+
+  defp normalize_uri(parsed_uri) do
     normalized_path = parsed_uri.path || "/"
 
     with {:ok, scheme} <- normalize_scheme(parsed_uri.scheme) do


Test patch:
diff --git a/test/finch_test.exs b/test/finch_test.exs
index f153690..6546ecb 100644
--- a/test/finch_test.exs
+++ b/test/finch_test.exs
@@ -169,6 +169,20 @@ defmodule FinchTest do
                end)
     end
 
+    test "successful get request, with query string, when given a %URI{}", %{bypass: bypass} do
+      start_supervised({Finch, name: MyFinch})
+      query_string = "query=value"
+      uri = URI.parse(endpoint(bypass, "?" <> query_string))
+
+      Bypass.expect_once(bypass, "GET", "/", fn conn ->
+        assert conn.query_string == query_string
+        Plug.Conn.send_resp(conn, 200, "OK")
+      end)
+
+      assert {:ok, %{status: 200}} =
+               Finch.request(MyFinch, :get, uri)
+    end
+
     test "raises if unsupported atom request method provided", %{bypass: bypass} do
       assert_raise ArgumentError, ~r/got unsupported atom method :gimme/, fn ->
         Finch.request(MyFinch, :gimme, endpoint(bypass))