Skip to content

Commit

Permalink
Merge pull request #1026 from reisub/improve-req-http-client-example
Browse files Browse the repository at this point in the history
Fix Req example HttpClient implementation for Req 0.4+
  • Loading branch information
bernardd authored Jan 11, 2024
2 parents 4f0f8b6 + 3ec23e1 commit 84db3d0
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions lib/ex_aws/request/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ defmodule ExAws.Request.HttpClient do
@impl ExAws.Request.HttpClient
def request(method, url, body, headers, _http_opts) do
case Req.request(
method: method,
url: url,
body: body,
headers: headers,
decode_body: false
) do
{:ok, %Req.Response{status: status} = response} ->
{:ok,
response
|> Map.take([:body, :headers])
|> Map.put(:status_code, status)}
{:error, exception} ->
{:error, %{reason: exception}}
case Req.request(method: method, url: url, body: body, headers: headers, decode_body: false) do
{:ok, response} -> {:ok, adapt_response(response)}
{:error, reason} -> {:error, %{reason: reason}}
end
end
defp adapt_response(response) do
# adapt the response to fit the shape expected by ExAWS
flat_headers =
Enum.flat_map(response.headers, fn
{name, vals} when is_list(vals) -> Enum.map(vals, &{name, &1})
{name, val} -> {name, val}
end)
%{
body: response.body,
status_code: response.status,
headers: flat_headers
}
end
end
```
Expand Down

0 comments on commit 84db3d0

Please sign in to comment.