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 more auths and fine tuned run_steps #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 60 additions & 12 deletions lib/curl_req.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,24 @@ defmodule CurlReq do
req
end

defp run_steps(req) do
Enum.reduce(req.request_steps, req, fn {step_name, step}, req ->
defp run_steps(req, option) do
req.request_steps
|> Enum.filter(fn {step_name, _} ->
case option do
_ when is_boolean(option) ->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is a little convoluted in my opinion. I'd like to at least see a pattern match on false to remove that branch from this path.

def run_steps(req, false), do: req

option

[{:except, excludes}] ->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for this, so that it becomes clearer what the purpose is of this addition? (same applies for :only).

step_name not in excludes

[{:only, includes}] ->
step_name in includes

_ ->
{:error, "Invalid option #{Kernel.inspect(option)} in `run_step`"}
end
end)
|> Enum.reduce(req, fn {step_name, step}, req ->
case step.(req) do
{_req, _response_or_error} ->
raise "The request was stopped by #{step_name} request_step."
Expand All @@ -51,6 +67,9 @@ defmodule CurlReq do
* `-L`/`--location`
* `-I`/`--head`
* `-d`/`--data`
* `-u`/`--user`
* `-n`/`--netrc`
* `--netrc-file`

Options:

Expand All @@ -70,14 +89,11 @@ defmodule CurlReq do
"""
@spec to_curl(Req.Request.t(), Keyword.t()) :: String.t()
def to_curl(req, options \\ []) do
flag_style = Keyword.get(options, :flags, :short)
opts = Keyword.validate!(options, flags: :short, run_steps: true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

flag_style = opts[:flags]
run_steps = opts[:run_steps]

req =
if Keyword.get(options, :run_steps, true) do
run_steps(req)
else
req
end
req = run_steps(req, run_steps)

cookies =
case Map.get(req.headers, "cookie") do
Expand All @@ -88,8 +104,9 @@ defmodule CurlReq do
headers =
req.headers
|> Enum.reject(fn {key, _val} -> key == "cookie" end)
|> Enum.flat_map(fn {key, value} ->
[header_flag(flag_style), "#{key}: #{value}"]
|> Enum.flat_map(fn
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you undo the formatting change here?

{key, value} ->
[header_flag(flag_style), "#{key}: #{value}"]
end)

body =
Expand All @@ -104,6 +121,29 @@ defmodule CurlReq do
_ -> []
end

auth =
with {%{auth: scheme}} <- req.options do
case scheme do
{:bearer, value} ->
[
header_flag(flag_style),
"authorization: Bearer #{value}"
]

{:basic, value} ->
[user_flag(flag_style), value]

:netrc ->
[netrc_flag(flag_style)]

{:netrc, filepath} ->
[netrc_file_flag(flag_style), filepath]
end
else
_ ->
[]
end

method =
case req.method do
nil -> [request_flag(flag_style), "GET"]
Expand All @@ -115,7 +155,7 @@ defmodule CurlReq do

CurlReq.Shell.cmd_to_string(
"curl",
headers ++ cookies ++ body ++ method ++ redirect ++ url
auth ++ headers ++ cookies ++ body ++ method ++ redirect ++ url
)
end

Expand All @@ -137,6 +177,14 @@ defmodule CurlReq do
defp location_flag(:short), do: "-L"
defp location_flag(:long), do: "--location"

defp user_flag(:short), do: "-u"
defp user_flag(:long), do: "--user"

defp netrc_flag(:short), do: "-n"
defp netrc_flag(:long), do: "--netrc"

defp netrc_file_flag(_), do: "--netrc-file"

@doc """
Transforms a curl command into a Req request.

Expand Down
12 changes: 10 additions & 2 deletions lib/curl_req/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ defmodule CurlReq.Macro do
head: :boolean,
form: :keep,
location: :boolean,
user: :string
user: :string,
netrc: :boolean
],
aliases: [
H: :header,
Expand All @@ -30,7 +31,8 @@ defmodule CurlReq.Macro do
I: :head,
F: :form,
L: :location,
u: :user
u: :user,
n: :netrc
]
)

Expand Down Expand Up @@ -73,6 +75,12 @@ defmodule CurlReq.Macro do
|> Req.Request.prepend_request_steps(auth: &Req.Steps.auth/1)
|> Req.merge(auth: {:bearer, token})

{"authorization", "Basic " <> token} ->
req
|> Req.Request.register_options([:auth])
|> Req.Request.prepend_request_steps(auth: &Req.Steps.auth/1)
|> Req.merge(auth: {:basic, token})

_ ->
Req.Request.put_header(req, key, value)
end
Expand Down