Skip to content

Commit f0db99b

Browse files
committed
Support PID as connection handle
1 parent 6b79eea commit f0db99b

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

lib/tesla/adapter/hackney.ex

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ if Code.ensure_loaded?(:hackney) do
3232
@behaviour Tesla.Adapter
3333
alias Tesla.Multipart
3434

35+
# hackney 1.x uses references while hackney 2.x uses pids
36+
# https://github.com/benoitc/hackney/blob/master/guides/MIGRATION.md#connection-handle
37+
# further usage in code is the same
38+
defguard is_hackney_connection_handle(handle) when is_reference(handle) or is_pid(handle)
39+
3540
@impl Tesla.Adapter
3641
def call(env, opts) do
3742
with {:ok, status, headers, body} <- request(env, opts) do
@@ -46,7 +51,7 @@ if Code.ensure_loaded?(:hackney) do
4651
end
4752

4853
defp format_body(data) when is_list(data), do: IO.iodata_to_binary(data)
49-
defp format_body(data) when is_binary(data) or is_reference(data), do: data
54+
defp format_body(data) when is_binary(data) or is_hackney_connection_handle(data), do: data
5055

5156
defp request(env, opts) do
5257
request(
@@ -88,6 +93,8 @@ if Code.ensure_loaded?(:hackney) do
8893

8994
defp send_stream(ref, body) do
9095
Enum.reduce_while(body, :ok, fn data, _ ->
96+
IO.inspect(data)
97+
9198
case :hackney.send_body(ref, data) do
9299
:ok -> {:cont, :ok}
93100
error -> {:halt, error}
@@ -99,12 +106,12 @@ if Code.ensure_loaded?(:hackney) do
99106
defp handle({:error, _} = error, _opts), do: error
100107
defp handle({:ok, status, headers}, _opts), do: {:ok, status, headers, []}
101108

102-
defp handle({:ok, ref}, _opts) when is_reference(ref) do
103-
handle_async_response({ref, %{status: nil, headers: nil}})
109+
defp handle({:ok, handle}, _opts) when is_hackney_connection_handle(handle) do
110+
handle_async_response({handle, %{status: nil, headers: nil}})
104111
end
105112

106-
defp handle({:ok, status, headers, ref}, opts) when is_reference(ref) do
107-
with {:ok, body} <- :hackney.body(ref, Keyword.get(opts, :max_body, :infinity)) do
113+
defp handle({:ok, status, headers, handle}, opts) when is_hackney_connection_handle(handle) do
114+
with {:ok, body} <- :hackney.body(handle, Keyword.get(opts, :max_body, :infinity)) do
108115
{:ok, status, headers, body}
109116
end
110117
end

test/tesla/adapter/hackney_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule Tesla.Adapter.HackneyTest do
3333

3434
assert {:ok, %Env{} = response} = call(request, with_body: true, async: true)
3535
assert response.status == 200
36-
assert is_reference(response.body) == true
36+
assert is_reference(response.body) or is_pid(response.body) == true
3737
end
3838

3939
test "get with `:max_body` option" do

0 commit comments

Comments
 (0)