Skip to content

Instantly share code, notes, and snippets.

@myobie

myobie/application.ex

Last active Apr 1, 2021
Embed
What would you like to do?
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment