-
Notifications
You must be signed in to change notification settings - Fork 91
feat: introduce pluggable HTTP client behaviour #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
achempion
merged 2 commits into
elixir-waffle:master
from
klacointe:feat/pluggable-http-client-adapter
Jul 2, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| defmodule Waffle.HTTPClient do | ||
| @moduledoc """ | ||
| Behaviour for pluggable HTTP clients used when downloading remote files. | ||
|
|
||
| ## Built-in implementations | ||
|
|
||
| - `Waffle.HTTPClient.Hackney` — default, uses `:hackney`. Add `{:hackney, "~> 1.9"}` to | ||
| your deps. | ||
|
|
||
| ## Configuration | ||
|
|
||
| config :waffle, :http_client, Waffle.HTTPClient.Hackney | ||
|
|
||
| ## Writing a custom client | ||
|
|
||
| Implement the `c:get/3` callback and configure Waffle to use your module: | ||
|
|
||
| config :waffle, :http_client, MyApp.HTTPClient | ||
|
|
||
| ### Options | ||
|
|
||
| Waffle passes the following options (all values come from application config): | ||
|
|
||
| | Option | Type | Default | Description | | ||
| |--------------------|-------------------------|------------|------------------------------------------| | ||
| | `:recv_timeout` | `non_neg_integer()` | `5_000` | Timeout for receiving a response (ms) | | ||
| | `:connect_timeout` | `non_neg_integer()` | `10_000` | Timeout for establishing a connection (ms) | | ||
| | `:max_body_length` | `non_neg_integer() \| :infinity` | `:infinity` | Maximum allowed response body size (bytes) | | ||
| | `:follow_redirect` | `boolean()` | `true` | Whether to follow HTTP redirects | | ||
|
|
||
| ### Return values | ||
|
|
||
| | Pattern | Meaning | | ||
| |----------------------------------------|--------------------------------------------------| | ||
| | `{:ok, body}` | Successful response, no filename in headers | | ||
| | `{:ok, body, filename}` | Successful response with `content-disposition` filename | | ||
| | `{:error, :timeout}` | Connect timed out — Waffle will retry | | ||
| | `{:error, :recv_timeout}` | Receive timed out — Waffle will retry | | ||
| | `{:error, :service_unavailable}` | Server returned 503 — Waffle will retry | | ||
| | `{:error, {:http_error, reason}}` | Non-retryable error; `reason` is the HTTP status integer for unexpected status codes, or an error term for connection/protocol errors | | ||
| """ | ||
|
|
||
| @type body :: binary() | ||
| @type filename :: String.t() | ||
| @type option :: | ||
| {:recv_timeout, non_neg_integer()} | ||
| | {:connect_timeout, non_neg_integer()} | ||
| | {:max_body_length, non_neg_integer() | :infinity} | ||
| | {:follow_redirect, boolean()} | ||
|
|
||
| @callback get(url :: String.t(), headers :: list(), options :: [option()]) :: | ||
| {:ok, body()} | ||
| | {:ok, body(), filename()} | ||
| | {:error, :timeout | :recv_timeout | :service_unavailable | {:http_error, any()}} | ||
| end |
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| defmodule Waffle.HTTPClient.Hackney do | ||
| @moduledoc """ | ||
| Default HTTP client implementation using `:hackney`. | ||
|
|
||
| Add `:hackney` to your dependencies: | ||
|
|
||
| {:hackney, "~> 1.9"} | ||
|
|
||
| ## Configuration | ||
|
|
||
| config :waffle, :http_client, Waffle.HTTPClient.Hackney | ||
|
|
||
| ## Options | ||
|
|
||
| | Option | Default | Description | | ||
| |--------------------|--------------|--------------------------------------------------------| | ||
| | `:recv_timeout` | `5_000` | Timeout for receiving a response, in milliseconds | | ||
| | `:connect_timeout` | `10_000` | Timeout for establishing a connection, in milliseconds | | ||
| | `:max_body_length` | `:infinity` | Maximum response body size, in bytes | | ||
| | `:follow_redirect` | `true` | Whether to follow HTTP redirects automatically | | ||
| """ | ||
|
|
||
| @behaviour Waffle.HTTPClient | ||
|
|
||
| @impl Waffle.HTTPClient | ||
| def get(url, headers, options) do | ||
| hackney_options = [ | ||
| follow_redirect: Keyword.get(options, :follow_redirect, true), | ||
| recv_timeout: Keyword.get(options, :recv_timeout, 5_000), | ||
| connect_timeout: Keyword.get(options, :connect_timeout, 10_000) | ||
| ] | ||
|
|
||
| max_body_length = Keyword.get(options, :max_body_length, :infinity) | ||
|
|
||
| case :hackney.get(url, headers, "", hackney_options) do | ||
| {:ok, 200, response_headers, client_ref} -> | ||
| read_body(client_ref, response_headers, max_body_length) | ||
|
|
||
| {:ok, 503, _headers, client_ref} -> | ||
| :hackney.close(client_ref) | ||
| {:error, :service_unavailable} | ||
|
|
||
| {:ok, status, _headers, client_ref} -> | ||
| :hackney.close(client_ref) | ||
| {:error, {:http_error, status}} | ||
|
|
||
| {:error, reason} -> | ||
| normalize_error(reason) | ||
| end | ||
| end | ||
|
|
||
| defp read_body(client_ref, response_headers, max_body_length) do | ||
| case :hackney.body(client_ref, max_body_length) do | ||
| {:ok, body} -> | ||
| filename = | ||
| :hackney_headers.new(response_headers) | ||
| |> get_content_disposition_filename() | ||
|
|
||
| if filename, do: {:ok, body, filename}, else: {:ok, body} | ||
|
|
||
| {:error, reason} -> | ||
|
klacointe marked this conversation as resolved.
|
||
| :hackney.close(client_ref) | ||
| normalize_error(reason) | ||
| end | ||
| end | ||
|
|
||
| # connect timeout: hackney returns %{reason: :timeout}, not a bare atom | ||
| defp normalize_error(%{reason: :timeout}), do: {:error, :timeout} | ||
| # recv timeout: hackney returns a bare :timeout atom | ||
| defp normalize_error(:timeout), do: {:error, :recv_timeout} | ||
| defp normalize_error(reason), do: {:error, {:http_error, reason}} | ||
|
|
||
| defp get_content_disposition_filename(headers) do | ||
| case :hackney_headers.get_value("content-disposition", headers) do | ||
| :undefined -> | ||
| nil | ||
|
|
||
| value -> | ||
| case :hackney_headers.content_disposition(value) do | ||
| {_, [{"filename", filename} | _]} -> filename | ||
| _ -> nil | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.