iangreenleaf

iangreenleaf

How to provide overrideable library behavior?

I’ve been using Elixir long enough to have a decent grasp on the mechanical bits of how the language fits together, but I feel like I’m still learning the pieces of how things are typically done (conventions and whatnot). Today I encountered a problem and came up with a solution that works, but I am curious if it’s a solution that smells good to the rest of you, or if there’s some other approach that I failed to think up. So, how would you solve this? Or, critique my solution—feedback big or small accepted :grinning_face_with_smiling_eyes:

The Problem

I’m using Arc with the GCS storage provider. If you’re lucky, you can just point it towards a bucket and it will use Goth to obtain an API token using the credentials you’ve already configured in Goth.

I’m not lucky, and I have two sets of credentials in my Goth configuration. This means the token function needs to be called with different parameters to specify which set of credentials to use. I need some way to hook into the arc_gcs library to override its default token fetching code.

My solution

Here’s my branch on GitHub.

I need a configuration option, but the token fetching itself is dynamic, so I can use a configuration option to provide a module that provides the token logic. And it seems like there should be a formally-defined interface for this, so it’s probably a time for behaviours…?

I change the arc_gcs library so it takes a configuration option to fetch the token:

-  defp for_scope(scopes) when is_list(scopes), do: for_scope(Enum.join(scopes, " "))
-
-  defp for_scope(scope) when is_binary(scope) do
-    {:ok, token} = Token.for_scope(scope)
-    token.token
-  end
+  defp for_scope(scopes) do
+    token_store = Application.get_env(:arc, :token_fetcher, DefaultGothToken)
+    token_store.get_token(scopes)
+  end

And define a behaviour and default implementation in the library:

  defmodule TokenFetcher do
    @callback get_token(binary | [binary]) :: binary
  end

  defmodule DefaultGothToken do
    @behaviour TokenFetcher

    @impl TokenFetcher
    def get_token(scopes) when is_list(scopes), do: get_token(Enum.join(scopes, " "))

    def get_token(scope) when is_binary(scope) do
      {:ok, token} = Token.for_scope(scope)
      token.token
    end
  end

Now in my own application, I can set my own module in configuration:

config :arc,
  storage: Arc.Storage.GCS,
  bucket: "my-bucket",
  token_fetcher: MyCredentials

And define the behavior I want:

defmodule MyCredentials do
  @behaviour Arc.Storage.GCS.TokenFetcher

  @impl Arc.Storage.GCS.TokenFetcher
  def get_token(scopes) when is_list(scopes), do: get_token(Enum.join(scopes, " "))

  def get_token(scope) when is_binary(scope) do
    {:ok, token} = Goth.Token.for_scope({"my-account@gcs", scope})
    token.token
  end
end

This works and now I can use the storage provider in my project. It should help other people with my problem, and be flexible enough to help people with other similar problems.

What do you think? Solid Elixir code, or crazy and dangerous?

Most Liked Switch mode

Marcus

Marcus

For me, it looks like a good example for a behaviour. Actually the only question is why ‘Arc.Storage’ is not a behaviour.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement