-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) -> | ||
option | ||
|
||
[{:except, excludes}] -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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." | ||
|
@@ -51,6 +67,9 @@ defmodule CurlReq do | |
* `-L`/`--location` | ||
* `-I`/`--head` | ||
* `-d`/`--data` | ||
* `-u`/`--user` | ||
* `-n`/`--netrc` | ||
* `--netrc-file` | ||
|
||
Options: | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = | ||
|
@@ -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"] | ||
|
@@ -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 | ||
|
||
|
@@ -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. | ||
|
||
|
There was a problem hiding this comment.
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