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 :smile:

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

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?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement