Skip to content

Instantly share code, notes, and snippets.

@myobie
Last active June 15, 2024 18:35
Show Gist options
  • Save myobie/1858944b1be86e43f5d37c007e878d48 to your computer and use it in GitHub Desktop.
Save myobie/1858944b1be86e43f5d37c007e878d48 to your computer and use it in GitHub Desktop.
Using Finch with ExAws
defmodule Example.Application do
@moduledoc false
use Application
def start(_type, _args) do
children = [
Example.Repo,
ExampleWeb.Telemetry,
{Phoenix.PubSub, name: Example.PubSub},
# Finch ↓
{Finch, name: ExAws.Request.Finch},
ExampleWeb.Endpoint
]
opts = [strategy: :one_for_one, name: Example.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
ExampleWeb.Endpoint.config_change(changed, removed)
:ok
end
end
config :ex_aws,
region: "us-east-1",
json_codec: Jason,
http_client: Example.ExAwsHttpClient
defmodule Mix.Tasks.CustomExAwxTask do
use Mix.Task
def run([]) do
{:ok, _} = Application.ensure_all_started(:finch)
{:ok, pid} = Finch.start_link(name: ExAws.Request.Finch)
Process.unlink(pid)
# ...
end
end
defmodule Example.ExAwsHttpClient do
@behaviour ExAws.Request.HttpClient
require Logger
def request(method, url, body, headers, http_opts) do
case http_opts do
[] -> :noop
opts -> Logger.debug(inspect({:http_opts, opts}))
end
with {:ok, resp} <-
Finch.build(method, url, headers, body)
|> Finch.request(ExAws.Request.Finch) do
{:ok, %{status_code: resp.status, body: resp.body, headers: resp.headers}}
else
{:error, reason} ->
{:error, %{reason: reason}}
end
end
end
@derpycoder
Copy link

Thanks @myobie,

I got ExAws working with Finch, thanks to you.

Glad you explored this before me, you are a life saver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment