Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial type specs #59

Merged
merged 9 commits into from Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/req.ex
Expand Up @@ -8,12 +8,17 @@ defmodule Req do
|> String.split("<!-- MDOC !-->")
|> Enum.fetch!(1)

@type url() :: URI.t() | String.t()

@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!(url(), keyword()) :: Req.Response.t()
def get!(url, options \\ []) do
request!(:get, url, options)
end
Expand All @@ -24,6 +29,7 @@ defmodule Req do
See `request/3` for a list of supported options.
"""
@doc api: :high_level
@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)
Expand All @@ -35,6 +41,7 @@ defmodule Req do
See `request/3` for a list of supported options.
"""
@doc api: :high_level
@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)
Expand All @@ -46,6 +53,7 @@ defmodule Req do
See `request/3` for a list of supported options.
"""
@doc api: :high_level
@spec delete!(url(), keyword()) :: Req.Response.t()
def delete!(url, options \\ []) do
request!(:delete, url, options)
end
Expand All @@ -71,6 +79,8 @@ defmodule Req do
The `options` are merged with default options set with `default_options/1`.
"""
@doc api: :high_level
@spec request(method(), url(), keyword()) ::
{:ok, Req.Response.t()} | {:error, Exception.t()}
def request(method, url, options \\ []) do
options = Keyword.merge(default_options(), options)

Expand All @@ -86,6 +96,7 @@ defmodule Req do
See `request/3` for more information.
"""
@doc api: :high_level
@spec request!(method(), url(), keyword()) :: Req.Response.t()
def request!(method, url, options \\ []) do
options = Keyword.merge(default_options(), options)

Expand All @@ -101,6 +112,7 @@ defmodule Req do
See `default_options/1` for more information.
"""
@doc api: :high_level
@spec default_options() :: keyword()
def default_options() do
Application.get_env(:req, :default_options, [])
end
Expand All @@ -114,6 +126,7 @@ defmodule Req do
Avoid setting default options in libraries as they are global.
"""
@doc api: :high_level
@spec default_options(keyword()) :: :ok
def default_options(options) do
Application.put_env(:req, :default_options, options)
end
Expand Down
7 changes: 7 additions & 0 deletions lib/req/response.ex
Expand Up @@ -15,6 +15,13 @@ defmodule Req.Response do
conflicts. Only accepts `t:atom/0` keys.
"""

@type t() :: %__MODULE__{
status: non_neg_integer(),
headers: [{binary(), binary()}],
body: binary(),
private: map()
}

defstruct [
:status,
headers: [],
Expand Down