Skip to content
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
42 changes: 22 additions & 20 deletions lib/ex_aws/request/hackney.ex
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
defmodule ExAws.Request.Hackney do
@behaviour ExAws.Request.HttpClient
if Code.ensure_loaded?(:hackney) do
defmodule ExAws.Request.Hackney do
@behaviour ExAws.Request.HttpClient

@moduledoc """
Configuration for `:hackney`.
@moduledoc """
Configuration for `:hackney`.

Options can be set for `:hackney` with the following config:
Options can be set for `:hackney` with the following config:

config :ex_aws, :hackney_opts,
recv_timeout: 30_000
config :ex_aws, :hackney_opts,
recv_timeout: 30_000

The default config handles setting the above.
"""
The default config handles setting the above.
"""

@default_opts [recv_timeout: 30_000]
@default_opts [recv_timeout: 30_000]

def request(method, url, body \\ "", headers \\ [], http_opts \\ []) do
opts = Application.get_env(:ex_aws, :hackney_opts, @default_opts)
opts = http_opts ++ [:with_body | opts]
def request(method, url, body \\ "", headers \\ [], http_opts \\ []) do
opts = Application.get_env(:ex_aws, :hackney_opts, @default_opts)
opts = http_opts ++ [:with_body | opts]

case :hackney.request(method, url, headers, body, opts) do
{:ok, status, headers} ->
{:ok, %{status_code: status, headers: headers}}
case :hackney.request(method, url, headers, body, opts) do
{:ok, status, headers} ->
{:ok, %{status_code: status, headers: headers}}

{:ok, status, headers, body} ->
{:ok, %{status_code: status, headers: headers, body: body}}
{:ok, status, headers, body} ->
{:ok, %{status_code: status, headers: headers, body: body}}

{:error, reason} ->
{:error, %{reason: reason}}
{:error, reason} ->
{:error, %{reason: reason}}
end
end
end
end
58 changes: 30 additions & 28 deletions lib/ex_aws/request/req.ex
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
defmodule ExAws.Request.Req do
@behaviour ExAws.Request.HttpClient
if Code.ensure_loaded?(Req) do
defmodule ExAws.Request.Req do
@behaviour ExAws.Request.HttpClient

@moduledoc """
Configuration for `m:Req`.
@moduledoc """
Configuration for `m:Req`.

Options can be set for `m:Req` with the following config:
Options can be set for `m:Req` with the following config:

config :ex_aws, :req_opts,
receive_timeout: 30_000
config :ex_aws, :req_opts,
receive_timeout: 30_000

The default config handles setting the above.
"""
The default config handles setting the above.
"""

@default_opts [receive_timeout: 30_000]
@default_opts [receive_timeout: 30_000]

@impl true
def request(method, url, body \\ "", headers \\ [], http_opts \\ []) do
http_opts = rename_follow_redirect(http_opts)
@impl true
def request(method, url, body \\ "", headers \\ [], http_opts \\ []) do
http_opts = rename_follow_redirect(http_opts)

[method: method, url: url, body: body, headers: headers, decode_body: false, retry: false]
|> Keyword.merge(Application.get_env(:ex_aws, :req_opts, @default_opts))
|> Keyword.merge(http_opts)
|> Req.request()
|> case do
{:ok, resp} ->
{:ok, %{status_code: resp.status, headers: Req.get_headers_list(resp), body: resp.body}}
[method: method, url: url, body: body, headers: headers, decode_body: false, retry: false]
|> Keyword.merge(Application.get_env(:ex_aws, :req_opts, @default_opts))
|> Keyword.merge(http_opts)
|> Req.request()
|> case do
{:ok, resp} ->
{:ok, %{status_code: resp.status, headers: Req.get_headers_list(resp), body: resp.body}}

{:error, reason} ->
{:error, %{reason: reason}}
{:error, reason} ->
{:error, %{reason: reason}}
end
end
end

# Req >= 0.4.0 uses :redirect, but some clients pass the :hackney option
# :follow_redirect. Rename the option for Req to use.
defp rename_follow_redirect(opts) do
{follow, opts} = Keyword.pop(opts, :follow_redirect, false)
# Req >= 0.4.0 uses :redirect, but some clients pass the :hackney option
# :follow_redirect. Rename the option for Req to use.
defp rename_follow_redirect(opts) do
{follow, opts} = Keyword.pop(opts, :follow_redirect, false)

Keyword.put(opts, :redirect, follow)
Keyword.put(opts, :redirect, follow)
end
end
end