iangreenleaf

iangreenleaf

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?

Showing Posts 1 to 1

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews