-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compile adapters only when available
- Loading branch information
Showing
2 changed files
with
53 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,44 @@ | ||
defmodule Tesla.Adapter.Hackney do | ||
def call(env, opts) do | ||
with {:ok, status, headers, body} <- request(env, opts || []) do | ||
%{env | status: status, | ||
headers: headers, | ||
body: body} | ||
if Code.ensure_loaded?(:hackney) do | ||
defmodule Tesla.Adapter.Hackney do | ||
def call(env, opts) do | ||
with {:ok, status, headers, body} <- request(env, opts || []) do | ||
%{env | status: status, | ||
headers: headers, | ||
body: body} | ||
end | ||
end | ||
end | ||
|
||
defp request(env, opts) do | ||
request( | ||
env.method, | ||
Tesla.build_url(env.url, env.query), | ||
Enum.into(env.headers, []), | ||
env.body, | ||
opts ++ env.opts | ||
) | ||
end | ||
defp request(method, url, headers, %Stream{} = body, opts), do: request_stream(method, url, headers, body, opts) | ||
defp request(method, url, headers, body, opts) when is_function(body), do: request_stream(method, url, headers, body, opts) | ||
defp request(method, url, headers, body, opts) do | ||
handle :hackney.request(method, url, headers, body || '', opts) | ||
end | ||
defp request(env, opts) do | ||
request( | ||
env.method, | ||
Tesla.build_url(env.url, env.query), | ||
Enum.into(env.headers, []), | ||
env.body, | ||
opts ++ env.opts | ||
) | ||
end | ||
defp request(method, url, headers, %Stream{} = body, opts), do: request_stream(method, url, headers, body, opts) | ||
defp request(method, url, headers, body, opts) when is_function(body), do: request_stream(method, url, headers, body, opts) | ||
defp request(method, url, headers, body, opts) do | ||
handle :hackney.request(method, url, headers, body || '', opts) | ||
end | ||
|
||
|
||
defp request_stream(method, url, headers, body, opts) do | ||
with {:ok, ref} <- :hackney.request(method, url, headers, :stream, opts) do | ||
for data <- body, do: :ok = :hackney.send_body(ref, data) | ||
handle :hackney.start_response(ref) | ||
else | ||
e -> handle(e) | ||
defp request_stream(method, url, headers, body, opts) do | ||
with {:ok, ref} <- :hackney.request(method, url, headers, :stream, opts) do | ||
for data <- body, do: :ok = :hackney.send_body(ref, data) | ||
handle :hackney.start_response(ref) | ||
else | ||
e -> handle(e) | ||
end | ||
end | ||
end | ||
|
||
defp handle({:error, _} = error), do: error | ||
defp handle({:ok, status, headers}), do: {:ok, status, headers, []} | ||
defp handle({:ok, status, headers, ref}) do | ||
with {:ok, body} <- :hackney.body(ref) do | ||
{:ok, status, headers, body} | ||
defp handle({:error, _} = error), do: error | ||
defp handle({:ok, status, headers}), do: {:ok, status, headers, []} | ||
defp handle({:ok, status, headers, ref}) do | ||
with {:ok, body} <- :hackney.body(ref) do | ||
{:ok, status, headers, body} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
defmodule Tesla.Adapter.Ibrowse do | ||
def call(env, opts) do | ||
with {:ok, status, headers, body} <- request(env, opts || []) do | ||
%{env | status: status, | ||
headers: headers, | ||
body: body} | ||
if Code.ensure_loaded?(:ibrowse) do | ||
defmodule Tesla.Adapter.Ibrowse do | ||
def call(env, opts) do | ||
with {:ok, status, headers, body} <- request(env, opts || []) do | ||
%{env | status: status, | ||
headers: headers, | ||
body: body} | ||
end | ||
end | ||
end | ||
|
||
defp request(env, opts) do | ||
body = env.body || [] | ||
:ibrowse.send_req( | ||
Tesla.build_url(env.url, env.query) |> to_char_list, | ||
Enum.into(env.headers, []), | ||
env.method, | ||
body, | ||
opts | ||
) | ||
defp request(env, opts) do | ||
body = env.body || [] | ||
:ibrowse.send_req( | ||
Tesla.build_url(env.url, env.query) |> to_char_list, | ||
Enum.into(env.headers, []), | ||
env.method, | ||
body, | ||
opts | ||
) | ||
end | ||
end | ||
end |