Skip to content

Commit be9e8ce

Browse files
authored
Polish up Req.Request docs (wojtekmach#366)
1 parent 357f27a commit be9e8ce

File tree

1 file changed

+69
-22
lines changed

1 file changed

+69
-22
lines changed

lib/req/request.ex

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -122,45 +122,51 @@ defmodule Req.Request do
122122
123123
Nothing is actually executed until we run the pipeline with `Req.Request.run_request/1`.
124124
125-
### Request steps
125+
### Request Steps
126126
127-
A request step is a function that accepts a `request` and returns one of the following:
127+
A **request step** is a function that accepts a `request` and returns one of the following:
128128
129-
* A `request`
129+
* A `request`.
130130
131-
* A `{request, response_or_error}` tuple. In that case no further request steps are executed
132-
and the return value goes through response or error steps
131+
* A `{request, response_or_error}` tuple. In this case no further request steps are executed
132+
and the return value goes through response or error steps.
133133
134-
Examples:
134+
#### Examples
135+
136+
A request step that adds a `user-agent` header if it's not there already:
135137
136138
def put_default_headers(request) do
137-
update_in(request.headers, &[{"user-agent", "req"} | &1])
139+
Req.Request.put_new_header(request, "user-agent", "req")
138140
end
139141
142+
The next is a request step that reads the response from cache if available. Note how, if the
143+
cached response is available, this step returns a `{request, response}` tuple so that the
144+
request doesn't actually go through:
145+
140146
def read_from_cache(request) do
141147
case ResponseCache.fetch(request) do
142148
{:ok, response} -> {request, response}
143149
:error -> request
144150
end
145151
end
146152
147-
### Response and error steps
153+
### Response and Error Steps
148154
149155
A response step is a function that accepts a `{request, response}` tuple and returns one of the
150156
following:
151157
152-
* A `{request, response}` tuple
158+
* A `{request, response}` tuple.
153159
154160
* A `{request, exception}` tuple. In that case, no further response steps are executed but the
155-
exception goes through error steps
161+
exception goes through error steps.
156162
157163
Similarly, an error step is a function that accepts a `{request, exception}` tuple and returns one
158164
of the following:
159165
160166
* A `{request, exception}` tuple
161167
162168
* A `{request, response}` tuple. In that case, no further error steps are executed but the
163-
response goes through response steps
169+
response goes through response steps.
164170
165171
Examples:
166172
@@ -181,7 +187,7 @@ defmodule Req.Request do
181187
182188
### Halting
183189
184-
Any step can call `halt/2` to halt the pipeline. This will prevent any further steps
190+
Any step can call `halt/2` to halt the pipeline. This prevents any further steps
185191
from being invoked.
186192
187193
Examples:
@@ -207,6 +213,7 @@ defmodule Req.Request do
207213
## Request Options
208214
209215
* `:print_headers` - if `true`, prints the headers. Defaults to `false`.
216+
210217
\"""
211218
def attach(%Req.Request{} = request, options \\ []) do
212219
request
@@ -283,7 +290,7 @@ defmodule Req.Request do
283290
284291
## Adapter
285292
286-
As noted in the ["Request steps"](#module-request-steps) section, a request step besides returning the request,
293+
As noted in the ["Request Steps"](#module-request-steps) section, a request step besides returning the request,
287294
might also return `{request, response}` or `{request, exception}`, thus invoking either response or error steps next.
288295
This is exactly how Req makes the underlying HTTP call, by invoking a request step that follows this contract.
289296
@@ -335,8 +342,12 @@ defmodule Req.Request do
335342
336343
Req.get!("https://api.github.com/repos/wojtekmach/req", adapter: hackney).body["description"]
337344
#=> "Req is a batteries-included HTTP client for Elixir."
345+
338346
"""
339347

348+
@typedoc """
349+
The request struct.
350+
"""
340351
@type t() :: %Req.Request{
341352
method: atom(),
342353
url: URI.t(),
@@ -402,8 +413,11 @@ defmodule Req.Request do
402413
iex> resp.status
403414
200
404415
"""
416+
@spec new(keyword()) :: t()
417+
def new(options \\ [])
418+
405419
if Req.MixProject.legacy_headers_as_lists?() do
406-
def new(options \\ []) do
420+
def new(options) do
407421
options =
408422
options
409423
|> Keyword.validate!([:method, :url, :headers, :body, :adapter, :options])
@@ -413,7 +427,7 @@ defmodule Req.Request do
413427
struct!(__MODULE__, options)
414428
end
415429
else
416-
def new(options \\ []) do
430+
def new(options) do
417431
options =
418432
options
419433
|> Keyword.validate!([:method, :url, :headers, :body, :adapter, :options])
@@ -556,6 +570,7 @@ defmodule Req.Request do
556570
@doc """
557571
Gets the value for a specific private `key`.
558572
"""
573+
@spec get_private(t(), atom(), default) :: term() | default when default: var
559574
def get_private(request, key, default \\ nil) when is_atom(key) do
560575
Map.get(request.private, key, default)
561576
end
@@ -575,14 +590,15 @@ defmodule Req.Request do
575590
iex> Req.Request.update_private(req, :b, 11, & &1 + 1).private
576591
%{a: 1, b: 11}
577592
"""
578-
@spec update_private(t(), key :: atom(), default :: term(), (atom() -> term())) :: t()
593+
@spec update_private(t(), key :: atom(), default :: term(), (term() -> term())) :: t()
579594
def update_private(request, key, default, fun) when is_atom(key) and is_function(fun, 1) do
580595
update_in(request.private, &Map.update(&1, key, default, fun))
581596
end
582597

583598
@doc """
584599
Assigns a private `key` to `value`.
585600
"""
601+
@spec put_private(t(), atom(), term()) :: t()
586602
def put_private(request, key, value) when is_atom(key) do
587603
put_in(request.private[key], value)
588604
end
@@ -599,6 +615,8 @@ defmodule Req.Request do
599615
This function returns an updated request and the response or exception that caused the halt.
600616
It's perfect when used in a request step to stop the pipeline.
601617
618+
See the ["Halting"](#module-halting) section in the module documentation for more information.
619+
602620
## Examples
603621
604622
Req.Request.prepend_request_steps(request, circuit_breaker: fn request ->
@@ -623,7 +641,10 @@ defmodule Req.Request do
623641
end
624642

625643
@doc """
626-
Appends request steps.
644+
Appends **request steps** to the existing request steps.
645+
646+
See the ["Request Steps"](#module-request-steps) section in the module documentation
647+
for more information.
627648
628649
## Examples
629650
@@ -632,6 +653,7 @@ defmodule Req.Request do
632653
inspect: &IO.inspect/1
633654
)
634655
"""
656+
@spec append_request_steps(t(), keyword(fun())) :: t()
635657
def append_request_steps(request, steps) do
636658
%{
637659
request
@@ -641,7 +663,10 @@ defmodule Req.Request do
641663
end
642664

643665
@doc """
644-
Prepends request steps.
666+
Prepends **request steps** to the existing request steps.
667+
668+
See the ["Request Steps"](#module-request-steps) section in the module documentation
669+
for more information.
645670
646671
## Examples
647672
@@ -650,6 +675,7 @@ defmodule Req.Request do
650675
inspect: &IO.inspect/1
651676
)
652677
"""
678+
@spec prepend_request_steps(t(), keyword(fun())) :: t()
653679
def prepend_request_steps(request, steps) do
654680
%{
655681
request
@@ -659,7 +685,10 @@ defmodule Req.Request do
659685
end
660686

661687
@doc """
662-
Appends response steps.
688+
Appends **response steps** to the existing response steps.
689+
690+
See the ["Response and Error Steps"](#module-response-and-error-steps) section in the
691+
module documentation for more information.
663692
664693
## Examples
665694
@@ -668,6 +697,7 @@ defmodule Req.Request do
668697
inspect: &IO.inspect/1
669698
)
670699
"""
700+
@spec append_response_steps(t(), keyword(fun())) :: t()
671701
def append_response_steps(request, steps) do
672702
%{
673703
request
@@ -676,7 +706,10 @@ defmodule Req.Request do
676706
end
677707

678708
@doc """
679-
Prepends response steps.
709+
Prepends **response steps** to the existing response steps.
710+
711+
See the ["Response and Error Steps"](#module-response-and-error-steps) section in the
712+
module documentation for more information.
680713
681714
## Examples
682715
@@ -685,6 +718,7 @@ defmodule Req.Request do
685718
inspect: &IO.inspect/1
686719
)
687720
"""
721+
@spec prepend_response_steps(t(), keyword(fun())) :: t()
688722
def prepend_response_steps(request, steps) do
689723
%{
690724
request
@@ -693,7 +727,10 @@ defmodule Req.Request do
693727
end
694728

695729
@doc """
696-
Appends error steps.
730+
Appends **error steps** to the existing error steps.
731+
732+
See the ["Response and Error Steps"](#module-response-and-error-steps) section in the
733+
module documentation for more information.
697734
698735
## Examples
699736
@@ -702,6 +739,7 @@ defmodule Req.Request do
702739
inspect: &IO.inspect/1
703740
)
704741
"""
742+
@spec append_error_steps(t(), keyword(fun())) :: t()
705743
def append_error_steps(request, steps) do
706744
%{
707745
request
@@ -710,7 +748,10 @@ defmodule Req.Request do
710748
end
711749

712750
@doc """
713-
Prepends error steps.
751+
Prepends **error steps** to the existing error steps.
752+
753+
See the ["Response and Error Steps"](#module-response-and-error-steps) section in the
754+
module documentation for more information.
714755
715756
## Examples
716757
@@ -719,6 +760,7 @@ defmodule Req.Request do
719760
inspect: &IO.inspect/1
720761
)
721762
"""
763+
@spec prepend_error_steps(t(), keyword(fun())) :: t()
722764
def prepend_error_steps(request, steps) do
723765
%{
724766
request
@@ -915,6 +957,9 @@ defmodule Req.Request do
915957
[]
916958
917959
"""
960+
@spec delete_header(t(), binary()) :: t()
961+
def delete_header(request, name)
962+
918963
if Req.MixProject.legacy_headers_as_lists?() do
919964
def delete_header(%Req.Request{} = request, name) when is_binary(name) do
920965
name = Req.__ensure_header_downcase__(name)
@@ -949,6 +994,7 @@ defmodule Req.Request do
949994
Req.get!(req, url: "/status/201", foo: :bar).status
950995
#=> 201
951996
"""
997+
@spec register_options(t(), keyword()) :: t()
952998
def register_options(%Req.Request{} = request, options) when is_list(options) do
953999
update_in(request.registered_options, &MapSet.union(&1, MapSet.new(options)))
9541000
end
@@ -989,6 +1035,7 @@ defmodule Req.Request do
9891035
iex> response.status
9901036
200
9911037
"""
1038+
@spec run_request(t()) :: {t(), Req.Response.t() | Exception.t()}
9921039
def run_request(request)
9931040

9941041
def run_request(%{current_request_steps: [step | rest]} = request) do

0 commit comments

Comments
 (0)