Last active
June 15, 2024 18:35
-
-
Save myobie/1858944b1be86e43f5d37c007e878d48 to your computer and use it in GitHub Desktop.
Using Finch with ExAws
This file contains 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
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 |
This file contains 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
config :ex_aws, | |
region: "us-east-1", | |
json_codec: Jason, | |
http_client: Example.ExAwsHttpClient |
This file contains 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
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 |
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @myobie,
I got ExAws working with Finch, thanks to you.
Glad you explored this before me, you are a life saver.