-
Notifications
You must be signed in to change notification settings - Fork 91
Add pluggable HTTP client behaviour with Hackney and Finch implementations #148
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
Closed
klacointe
wants to merge
5
commits into
elixir-waffle:master
from
klacointe:feat/pluggable-http-client
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1f57d35
feat: introduce pluggable HTTP client behaviour
klacointe 1fca1a0
feat: address review feedback on pluggable HTTP client
klacointe 8fb8043
feat: address round 2 review feedback on Finch client
klacointe a9efa4d
feat: add follow_redirect support to Waffle.HTTPClient.Finch
klacointe cbedd7c
docs: fix missing and unsupported option docs in HTTP clients
klacointe 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 |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| defmodule Mix.Tasks.Waffle do | ||
| @moduledoc false | ||
|
|
||
| defmodule G do | ||
| use Mix.Task | ||
| import Mix.Generator | ||
|
|
||
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,87 @@ | ||
| 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. | ||
| - `Waffle.HTTPClient.Finch` — uses `Finch`. Add `{:finch, "~> 0.18"}` to your deps, | ||
| start a `Finch` pool in your application supervision tree, and configure | ||
| `config :waffle, Waffle.HTTPClient.Finch, pool_name: MyApp.Finch`. | ||
|
|
||
| At least one HTTP client dependency is required when downloading remote files. | ||
|
|
||
| ## 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 | | ||
| | `:max_retries` | `non_neg_integer()` | `3` | Number of retries on timeout or 503 | | ||
| | `:backoff_factor` | `non_neg_integer()` | `1_000` | Base backoff delay between retries (ms) | | ||
| | `:backoff_max` | `non_neg_integer()` | `30_000` | Maximum backoff delay between retries (ms) | | ||
|
|
||
| ### Return values | ||
|
|
||
| | Pattern | Meaning | | ||
| |----------------------------------------|--------------------------------------------------| | ||
| | `{:ok, body}` | Successful response, no filename in headers | | ||
| | `{:ok, body, filename}` | Successful response with `content-disposition` filename | | ||
| | `{:error, :timeout}` | Request timed out — Waffle will retry | | ||
| | `{:error, :service_unavailable}` | Server returned 503 — Waffle will retry | | ||
| | `{:error, {:http_error, reason}}` | Non-retryable error | | ||
| """ | ||
|
|
||
| @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 | :service_unavailable | {:http_error, any()}} | ||
|
|
||
| @doc """ | ||
| Parses the `filename` parameter from a `content-disposition` header value. | ||
|
|
||
| Handles quoted filenames (`filename="foo.png"`), unquoted filenames | ||
| (`filename=foo.png`), and multi-parameter values. | ||
|
|
||
| Returns `nil` when no filename is present. | ||
| """ | ||
| @spec parse_content_disposition(String.t()) :: String.t() | nil | ||
| def parse_content_disposition(value) do | ||
| value | ||
| |> String.split(";") | ||
| |> Enum.find_value(&extract_filename_param/1) | ||
| end | ||
|
|
||
| defp extract_filename_param(part) do | ||
| with [key, raw] <- String.split(String.trim(part), "=", parts: 2), | ||
| true <- String.downcase(String.trim(key)) == "filename", | ||
| name when name != "" <- raw |> String.trim() |> String.trim("\"") do | ||
| name | ||
| else | ||
| _ -> nil | ||
| 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.