Skip to content

Commit

Permalink
FIXUP
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Jun 19, 2024
1 parent 910c447 commit cef3cef
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/req.ex
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ defmodule Req do
[`put_path_params`](`Req.Steps.put_path_params/1`) step.)
* `:path_params_style` - configures how `:path_params` are expressed (via
[`put_path_params_style`](`Req.Steps.put_path_params_style/1`) step). Can be one of:
[`put_path_params`](`Req.Steps.put_path_params/1`) step). Can be one of:
* `:colon` - default, params are expressed as `:name` in the path.
Expand Down
29 changes: 13 additions & 16 deletions lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ defmodule Req.Steps do
put_base_url: &Req.Steps.put_base_url/1,
auth: &Req.Steps.auth/1,
put_params: &Req.Steps.put_params/1,
put_path_params_style: &Req.Steps.put_path_params_style/1,
put_path_params: &Req.Steps.put_path_params/1,
put_range: &Req.Steps.put_range/1,
cache: &Req.Steps.cache/1,
Expand Down Expand Up @@ -402,7 +401,8 @@ defmodule Req.Steps do
By default, params in the URL path are expressed as strings prefixed with `:`. For example,
`:code` in `https://httpbin.org/status/:code`. If you want to use the `{code}` syntax,
set `path_params_style: :curly` (see `put_path_params_style/1`).
set `path_params_style: :curly`. Param names must start with a letter and can contain letters,
digits, and underscores; this is true both for `:colon_params` as well as `{curly_params}`.
Path params are replaced in the request URL path. The path params are specified as a keyword
list of parameter names and values, as in the examples below. The values of the parameters are
Expand All @@ -411,12 +411,22 @@ defmodule Req.Steps do
## Request Options
* `:path_params` - params to add to the templated path. Defaults to `[]`.
* `:path_params_style` (*available since v0.5.1*) - how path params are expressed.
Can be one of:
* `:colon` (default) for Plug-style parameters, such as
`https://httpbin.org/status/:code`.
* `:curly` for OpenAPI-style parameters, such as `https://httpbin.org/status/{code}`.
## Examples
iex> Req.get!("https://httpbin.org/status/:code", path_params: [code: 200]).status
200
iex> Req.get!("https://httpbin.org/status/{code}", path_params: [code: 201], path_params_style: :curly).status
200
"""
@doc step: :request
def put_path_params(request) do
Expand All @@ -435,7 +445,7 @@ defmodule Req.Steps do

defp apply_path_params(request, params) do
regex =
case Req.Request.get_private(request, :path_params_style) || raise("missing") do
case Req.Request.get_option(request, :path_params_style, :colon) do
:colon -> ~r/:([a-zA-Z]{1}[\w_]*)/
:curly -> ~r/\{([a-zA-Z]{1}[\w_]*)\}/
end
Expand All @@ -454,19 +464,6 @@ defmodule Req.Steps do
end)
end

@doc """
TODO
"""
@doc since: "0.5.1"
@doc step: :request
def put_path_params_style(request) do
Req.Request.put_private(
request,
:path_params_style,
Req.Request.get_option(request, :path_params_style, :colon)
)
end

@doc """
Adds params to request query string.
Expand Down
4 changes: 2 additions & 2 deletions test/req/steps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ defmodule Req.StepsTest do
Req.new(url: "http://foo/:id{ola}", path_params: [id: "abc|def"]) |> Req.Request.prepare()

assert URI.to_string(req.url) == "http://foo/abc%7Cdef{ola}"
end

test "put_path_params_style" do
# With :curly style.

req =
Req.new(url: "http://foo/{id}:bar", path_params: [id: "abc|def"], path_params_style: :curly)
|> Req.Request.prepare()
Expand Down

0 comments on commit cef3cef

Please sign in to comment.