Created
February 21, 2023 20:49
-
-
Save cblavier/d3f453aaacc19d59e9bd8911e09a1001 to your computer and use it in GitHub Desktop.
Revisions
-
cblavier created this gist
Feb 21, 2023 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ defmodule Readable.Transloadit do use Tesla plug Tesla.Middleware.BaseUrl, "https://api2.transloadit.com/" plug Tesla.Middleware.JSON @behaviour Readable.Transloadit.TransloaditAPI @impl true def run_assembly(assembly, file_path, source_container, target_container) do assembly = apply(Readable.Transloadit.TransloaditAssemblies, assembly, [ file_path, source_container, target_container ]) case post("/assemblies", %{params: Jason.encode!(assembly)}) do {:ok, %Tesla.Env{status: 200, body: body}} -> {:ok, Jason.decode!(body, keys: :atoms)} {:ok, %Tesla.Env{body: body}} -> {:error, body} {:error, error} -> {:error, error} end end @impl true def wait_until_completed(assembly_status_url, opts \\ [wait_time: 500]) do case get_assembly_status(assembly_status_url) do {:ok, "ASSEMBLY_COMPLETED"} -> :ok {:ok, _} -> Process.sleep(opts[:wait_time]) wait_until_completed(assembly_status_url, opts) {:error, error} -> {:error, error} end end defp get_assembly_status(assembly_status_url) do with {:ok, %Tesla.Env{status: 200, body: body}} <- Tesla.get(assembly_status_url), {:ok, body} <- Jason.decode(body), nil <- Map.get(body, "error") do {:ok, Map.get(body, "ok")} else {:ok, %Tesla.Env{body: body}} -> {:error, body} error -> {:error, error} 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ defmodule Readable.Transloadit.TransloaditAPI do @callback run_assembly(atom(), String.t(), String.t(), String.t()) :: {:ok, Tesla.Env.t()} | {:error, any()} @callback wait_until_completed(String.t()) :: :ok | {:error, any()} @callback wait_until_completed(String.t(), [{atom(), any()}]) :: :ok | {: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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ defmodule Readable.Transloadit.TransloaditAssemblies do @auth_key "key" def mono_mp3(input_path, source_container, target_container, notify_url \\ nil) do assembly_template( "96827bb365e64b7cae04d984c6", input_path, source_container, target_container, notify_url ) end def avatar(input_path, source_container, target_container, notify_url \\ nil) do assembly_template( "18dbc2f18120488abdd3cfbd18d", input_path, source_container, target_container, notify_url ) end def cover(input_path, source_container, target_container, notify_url \\ nil) do assembly_template( "18f2a04feb414097910e7367762c", input_path, source_container, target_container, notify_url ) end defp assembly_template(template_id, input_path, source_container, target_container, notify_url) do %{ auth: %{ key: @auth_key }, template_id: template_id, steps: %{ import: %{ container: source_container, path: input_path }, export: %{ container: target_container } }, notify_url: notify_url } end end