Skip to content

Commit

Permalink
Add :finch_private option to put into Finch.Request (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbernheisel authored Oct 5, 2023
1 parent 3698e6f commit 1583363
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/req.ex
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ defmodule Req do
* `:unix_socket` - if set, connect through the given UNIX domain socket.
* `:finch_private` - a map or keyword list of private metadata to add to the Finch request. May be useful
for adding custom data when handling telemetry with `Finch.Telemetry`.
* `:finch_request` - a function that executes the Finch request, defaults to using `Finch.request/3`.
## Examples
Expand Down Expand Up @@ -350,6 +353,7 @@ defmodule Req do
:plug,
:finch,
:finch_request,
:finch_private,
:connect_options,
:inet6,
:receive_timeout,
Expand Down
13 changes: 13 additions & 0 deletions lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ defmodule Req.Steps do
* `:unix_socket` - if set, connect through the given UNIX domain socket.
* `:finch_private` - a map or keyword list of private metadata to add to the Finch request. May be useful
for adding custom data when handling telemetry with `Finch.Telemetry`.
* `:finch_request` - a function that executes the Finch request, defaults to using `Finch.request/3`.
The function should accept 4 arguments:
Expand Down Expand Up @@ -675,6 +678,7 @@ defmodule Req.Steps do
finch_request =
Finch.build(request.method, request.url, request_headers, body)
|> Map.replace!(:unix_socket, request.options[:unix_socket])
|> add_private_options(request.options[:finch_private])

finch_options =
request.options |> Map.take([:receive_timeout, :pool_timeout]) |> Enum.to_list()
Expand Down Expand Up @@ -838,6 +842,15 @@ defmodule Req.Steps do
end
end

defp add_private_options(finch_request, nil), do: finch_request

defp add_private_options(finch_request, private_options)
when is_list(private_options) or is_map(private_options) do
Enum.reduce(private_options, finch_request, fn {k, v}, acc_finch_req ->
Finch.Request.put_private(acc_finch_req, k, v)
end)
end

defp finch_fields_to_map(fields) do
Enum.reduce(fields, %{}, fn {name, value}, acc ->
Map.update(acc, name, [value], &(&1 ++ [value]))
Expand Down
24 changes: 24 additions & 0 deletions test/req/steps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,30 @@ defmodule Req.StepsTest do
end
end

def send_telemetry_metadata_pid(_name, _measurements, metadata, _) do
send(metadata.request.private.pid, :telemetry_private)
:ok
end

test ":finch_private", c do
on_exit(fn -> :telemetry.detach("#{c.test}") end)

:ok =
:telemetry.attach(
"#{c.test}",
[:finch, :request, :stop],
&__MODULE__.send_telemetry_metadata_pid/4,
nil
)

Bypass.expect(c.bypass, "GET", "/ok", fn conn ->
Plug.Conn.send_resp(conn, 200, "finch_private")
end)

assert Req.get!(c.url <> "/ok", finch_private: %{pid: self()}).body == "finch_private"
assert_received :telemetry_private
end

test "into: fun" do
%{url: url} =
TestSocket.serve(fn socket ->
Expand Down

0 comments on commit 1583363

Please sign in to comment.