From 0eb718d246c384581562c0ce17b929e214e7847e Mon Sep 17 00:00:00 2001 From: Po Chen Date: Fri, 26 Nov 2021 22:22:49 +1100 Subject: [PATCH 1/9] add initial type specs --- lib/req.ex | 23 +++++++++++++++++++++++ lib/req/response.ex | 2 ++ 2 files changed, 25 insertions(+) diff --git a/lib/req.ex b/lib/req.ex index c8396fe1..6d3bbdb9 100644 --- a/lib/req.ex +++ b/lib/req.ex @@ -8,12 +8,28 @@ defmodule Req do |> String.split("") |> Enum.fetch!(1) + @type option :: + {:base_url, String.t()} + | {:netrc, String.t()} + | {:auth, term()} + | {:params, Enum.t()} + | {:range, Range.t()} + | {:cache, boolean()} + | {:raw, boolean()} + | {:retry, keyword()} + | {:steps, tuple() | atom() | function()} + + @type options :: [option()] + + @type method :: :get | :post | :put | :delete + @doc """ Makes a GET request. See `request/3` for a list of supported options. """ @doc api: :high_level + @spec get!(String.t(), options()) :: Req.Response.t() def get!(url, options \\ []) do request!(:get, url, options) end @@ -24,6 +40,7 @@ defmodule Req do See `request/3` for a list of supported options. """ @doc api: :high_level + @spec post!(String.t(), term(), options()) :: Req.Response.t() def post!(url, body, options \\ []) do options = Keyword.put(options, :body, body) request!(:post, url, options) @@ -35,6 +52,7 @@ defmodule Req do See `request/3` for a list of supported options. """ @doc api: :high_level + @spec put!(String.t(), term(), options()) :: Req.Response.t() def put!(url, body, options \\ []) do options = Keyword.put(options, :body, body) request!(:put, url, options) @@ -46,6 +64,7 @@ defmodule Req do See `request/3` for a list of supported options. """ @doc api: :high_level + @spec delete!(String.t(), options()) :: Req.Response.t() def delete!(url, options \\ []) do request!(:delete, url, options) end @@ -71,6 +90,7 @@ defmodule Req do The `options` are merged with default options set with `default_options/1`. """ @doc api: :high_level + @spec request(method(), String.t(), options()) :: {:ok, Req.Response.t()} | {:error, term()} def request(method, url, options \\ []) do options = Keyword.merge(default_options(), options) @@ -86,6 +106,7 @@ defmodule Req do See `request/3` for more information. """ @doc api: :high_level + @spec request!(method(), String.t(), options()) :: Req.Response.t() | no_return() def request!(method, url, options \\ []) do options = Keyword.merge(default_options(), options) @@ -101,6 +122,7 @@ defmodule Req do See `default_options/1` for more information. """ @doc api: :high_level + @spec default_options() :: options() def default_options() do Application.get_env(:req, :default_options, []) end @@ -114,6 +136,7 @@ defmodule Req do Avoid setting default options in libraries as they are global. """ @doc api: :high_level + @spec default_options(options()) :: :ok def default_options(options) do Application.put_env(:req, :default_options, options) end diff --git a/lib/req/response.ex b/lib/req/response.ex index 28c57646..6cff05d4 100644 --- a/lib/req/response.ex +++ b/lib/req/response.ex @@ -15,6 +15,8 @@ defmodule Req.Response do conflicts. Only accepts `t:atom/0` keys. """ + @type t :: %__MODULE__{} + defstruct [ :status, headers: [], From 50691bc8de559e8702243c7ed12520189bff274f Mon Sep 17 00:00:00 2001 From: Po Chen Date: Wed, 1 Dec 2021 20:50:42 +1100 Subject: [PATCH 2/9] Update lib/req.ex Co-authored-by: Wojtek Mach --- lib/req.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/req.ex b/lib/req.ex index 6d3bbdb9..a97ae255 100644 --- a/lib/req.ex +++ b/lib/req.ex @@ -90,7 +90,7 @@ defmodule Req do The `options` are merged with default options set with `default_options/1`. """ @doc api: :high_level - @spec request(method(), String.t(), options()) :: {:ok, Req.Response.t()} | {:error, term()} + @spec request(method(), String.t(), options()) :: {:ok, Req.Response.t()} | {:error, Exception.t()} def request(method, url, options \\ []) do options = Keyword.merge(default_options(), options) From 42721825498b0e672f7c5156c4380df7dbbfa4ff Mon Sep 17 00:00:00 2001 From: Po Chen Date: Wed, 1 Dec 2021 20:51:01 +1100 Subject: [PATCH 3/9] Update lib/req.ex Co-authored-by: Wojtek Mach --- lib/req.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/req.ex b/lib/req.ex index a97ae255..07f73275 100644 --- a/lib/req.ex +++ b/lib/req.ex @@ -106,7 +106,7 @@ defmodule Req do See `request/3` for more information. """ @doc api: :high_level - @spec request!(method(), String.t(), options()) :: Req.Response.t() | no_return() + @spec request!(method(), String.t(), options()) :: Req.Response.t() def request!(method, url, options \\ []) do options = Keyword.merge(default_options(), options) From 9a6208b308f068aa73aa65071d781ba53e28a7ea Mon Sep 17 00:00:00 2001 From: Po Chen Date: Wed, 1 Dec 2021 20:51:11 +1100 Subject: [PATCH 4/9] Update lib/req/response.ex Co-authored-by: Wojtek Mach --- lib/req/response.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/req/response.ex b/lib/req/response.ex index 6cff05d4..b828fe58 100644 --- a/lib/req/response.ex +++ b/lib/req/response.ex @@ -15,7 +15,11 @@ defmodule Req.Response do conflicts. Only accepts `t:atom/0` keys. """ - @type t :: %__MODULE__{} + @type t() :: %__MODULE__{ + status: non_neg_integer(), + headers: [{binary(), binary()}], + body: binary() + } defstruct [ :status, From a5682665988a8af2b15adb2467349d79ec7f1463 Mon Sep 17 00:00:00 2001 From: Po Chen Date: Wed, 1 Dec 2021 21:10:54 +1100 Subject: [PATCH 5/9] Update lib/req.ex Co-authored-by: Wojtek Mach --- lib/req.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/req.ex b/lib/req.ex index 07f73275..d40c977e 100644 --- a/lib/req.ex +++ b/lib/req.ex @@ -29,7 +29,7 @@ defmodule Req do See `request/3` for a list of supported options. """ @doc api: :high_level - @spec get!(String.t(), options()) :: Req.Response.t() + @spec get!(String.t(), keyword()) :: Req.Response.t() def get!(url, options \\ []) do request!(:get, url, options) end From 4e3b11ad12128a8bca5d29dea4b19d42e6dc22a2 Mon Sep 17 00:00:00 2001 From: Po Chen Date: Wed, 1 Dec 2021 21:13:03 +1100 Subject: [PATCH 6/9] Update req.ex --- lib/req.ex | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/lib/req.ex b/lib/req.ex index d40c977e..c06d59e2 100644 --- a/lib/req.ex +++ b/lib/req.ex @@ -8,19 +8,6 @@ defmodule Req do |> String.split("") |> Enum.fetch!(1) - @type option :: - {:base_url, String.t()} - | {:netrc, String.t()} - | {:auth, term()} - | {:params, Enum.t()} - | {:range, Range.t()} - | {:cache, boolean()} - | {:raw, boolean()} - | {:retry, keyword()} - | {:steps, tuple() | atom() | function()} - - @type options :: [option()] - @type method :: :get | :post | :put | :delete @doc """ @@ -40,7 +27,7 @@ defmodule Req do See `request/3` for a list of supported options. """ @doc api: :high_level - @spec post!(String.t(), term(), options()) :: Req.Response.t() + @spec post!(String.t(), term(), keyword()) :: Req.Response.t() def post!(url, body, options \\ []) do options = Keyword.put(options, :body, body) request!(:post, url, options) @@ -52,7 +39,7 @@ defmodule Req do See `request/3` for a list of supported options. """ @doc api: :high_level - @spec put!(String.t(), term(), options()) :: Req.Response.t() + @spec put!(String.t(), term(), keyword()) :: Req.Response.t() def put!(url, body, options \\ []) do options = Keyword.put(options, :body, body) request!(:put, url, options) @@ -64,7 +51,7 @@ defmodule Req do See `request/3` for a list of supported options. """ @doc api: :high_level - @spec delete!(String.t(), options()) :: Req.Response.t() + @spec delete!(String.t(), keyword()) :: Req.Response.t() def delete!(url, options \\ []) do request!(:delete, url, options) end @@ -90,7 +77,7 @@ defmodule Req do The `options` are merged with default options set with `default_options/1`. """ @doc api: :high_level - @spec request(method(), String.t(), options()) :: {:ok, Req.Response.t()} | {:error, Exception.t()} + @spec request(method(), String.t(), keyword()) :: {:ok, Req.Response.t()} | {:error, Exception.t()} def request(method, url, options \\ []) do options = Keyword.merge(default_options(), options) @@ -106,7 +93,7 @@ defmodule Req do See `request/3` for more information. """ @doc api: :high_level - @spec request!(method(), String.t(), options()) :: Req.Response.t() + @spec request!(method(), String.t(), keyword()) :: Req.Response.t() def request!(method, url, options \\ []) do options = Keyword.merge(default_options(), options) @@ -122,7 +109,7 @@ defmodule Req do See `default_options/1` for more information. """ @doc api: :high_level - @spec default_options() :: options() + @spec default_options() :: keyword() def default_options() do Application.get_env(:req, :default_options, []) end @@ -136,7 +123,7 @@ defmodule Req do Avoid setting default options in libraries as they are global. """ @doc api: :high_level - @spec default_options(options()) :: :ok + @spec default_options(keyword()) :: :ok def default_options(options) do Application.put_env(:req, :default_options, options) end From 5fb49a94de4b602d5337fc571c2ef4de07031fb6 Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Wed, 1 Dec 2021 11:15:59 +0100 Subject: [PATCH 7/9] Update lib/req/response.ex --- lib/req/response.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/req/response.ex b/lib/req/response.ex index b828fe58..4a57f072 100644 --- a/lib/req/response.ex +++ b/lib/req/response.ex @@ -18,7 +18,8 @@ defmodule Req.Response do @type t() :: %__MODULE__{ status: non_neg_integer(), headers: [{binary(), binary()}], - body: binary() + body: binary(), + private: map() } defstruct [ From dd6adddd4287f6abc338fd6fceb0484a3bf02c59 Mon Sep 17 00:00:00 2001 From: Po Chen Date: Wed, 1 Dec 2021 21:17:53 +1100 Subject: [PATCH 8/9] run formatter --- lib/req.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/req.ex b/lib/req.ex index c06d59e2..2989c3c7 100644 --- a/lib/req.ex +++ b/lib/req.ex @@ -77,7 +77,8 @@ defmodule Req do The `options` are merged with default options set with `default_options/1`. """ @doc api: :high_level - @spec request(method(), String.t(), keyword()) :: {:ok, Req.Response.t()} | {:error, Exception.t()} + @spec request(method(), String.t(), keyword()) :: + {:ok, Req.Response.t()} | {:error, Exception.t()} def request(method, url, options \\ []) do options = Keyword.merge(default_options(), options) From 94dc49d3574668a56321f05f98ea251d4f4dbca0 Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Wed, 1 Dec 2021 11:20:14 +0100 Subject: [PATCH 9/9] Apply suggestions from code review --- lib/req.ex | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/req.ex b/lib/req.ex index 2989c3c7..93c8ed7a 100644 --- a/lib/req.ex +++ b/lib/req.ex @@ -8,7 +8,9 @@ defmodule Req do |> String.split("") |> Enum.fetch!(1) - @type method :: :get | :post | :put | :delete + @type url() :: URI.t() | String.t() + + @type method() :: :get | :post | :put | :delete @doc """ Makes a GET request. @@ -16,7 +18,7 @@ defmodule Req do See `request/3` for a list of supported options. """ @doc api: :high_level - @spec get!(String.t(), keyword()) :: Req.Response.t() + @spec get!(url(), keyword()) :: Req.Response.t() def get!(url, options \\ []) do request!(:get, url, options) end @@ -27,7 +29,7 @@ defmodule Req do See `request/3` for a list of supported options. """ @doc api: :high_level - @spec post!(String.t(), term(), keyword()) :: Req.Response.t() + @spec post!(url(), body :: term(), keyword()) :: Req.Response.t() def post!(url, body, options \\ []) do options = Keyword.put(options, :body, body) request!(:post, url, options) @@ -39,7 +41,7 @@ defmodule Req do See `request/3` for a list of supported options. """ @doc api: :high_level - @spec put!(String.t(), term(), keyword()) :: Req.Response.t() + @spec put!(url(), body :: term(), keyword()) :: Req.Response.t() def put!(url, body, options \\ []) do options = Keyword.put(options, :body, body) request!(:put, url, options) @@ -51,7 +53,7 @@ defmodule Req do See `request/3` for a list of supported options. """ @doc api: :high_level - @spec delete!(String.t(), keyword()) :: Req.Response.t() + @spec delete!(url(), keyword()) :: Req.Response.t() def delete!(url, options \\ []) do request!(:delete, url, options) end @@ -77,7 +79,7 @@ defmodule Req do The `options` are merged with default options set with `default_options/1`. """ @doc api: :high_level - @spec request(method(), String.t(), keyword()) :: + @spec request(method(), url(), keyword()) :: {:ok, Req.Response.t()} | {:error, Exception.t()} def request(method, url, options \\ []) do options = Keyword.merge(default_options(), options) @@ -94,7 +96,7 @@ defmodule Req do See `request/3` for more information. """ @doc api: :high_level - @spec request!(method(), String.t(), keyword()) :: Req.Response.t() + @spec request!(method(), url(), keyword()) :: Req.Response.t() def request!(method, url, options \\ []) do options = Keyword.merge(default_options(), options)