Packages
goth
0.3.1
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.1
1.3.0
1.3.0-rc.5
1.3.0-rc.4
1.3.0-rc.3
1.3.0-rc.2
1.3.0-rc.1
1.3.0-rc.0
1.2.0
1.1.0
1.0.1
1.0.1-beta
1.0.0
0.11.1
0.11.0
0.10.0
0.9.0
0.8.2
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.0
0.5.1
0.5.0
0.4.0
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
0.0.3
0.0.2
0.0.1
A simple library to generate and retrieve Oauth2 tokens for use with Google Cloud Service accounts.
Current section
Files
Jump to
Current section
Files
lib/goth/config.ex
defmodule Goth.Config do
@moduledoc """
`Goth.Config` is a `GenServer` that holds the current configuration.
This configuration is loaded from one of three places:
1. a JSON string passed in via your application's config
2. a ENV variable passed in via your application's config
3. Google's metadata service (note: this only works if running in GCP)
The `Goth.Config` server exists mostly for other parts of your application
(or other libraries) to pull the current configuration state,
via `Goth.Config.get/1`. If necessary, you can also set config values via
`Goth.Config.set/2`
"""
use GenServer
alias Goth.Client
def start_link do
GenServer.start_link(__MODULE__, :ok, [name: __MODULE__])
end
def init(:ok) do
case Application.get_env(:goth, :json) do
nil -> {:ok, Application.get_env(:goth, :config,
%{"token_source" => :metadata,
"project_id" => Client.retrieve_metadata_project()})}
{:system, var} -> {:ok, decode_json(System.get_env(var)) }
json -> {:ok, decode_json(json)}
end
end
# Decodes JSON (if configured) and sets oauth token source
defp decode_json(json) do
json
|> Poison.decode!
|> Map.put("token_source", :oauth)
end
def set(key, value) when is_atom(key), do: key |> to_string |> set(value)
def set(key, value) do
GenServer.call(__MODULE__, {:set, key, value})
end
def get(key) when is_atom(key), do: key |> to_string |> get
def get(key) do
GenServer.call(__MODULE__, {:get, key})
end
def handle_call({:set, key, value}, _from, keys) do
{:reply, :ok, Map.put(keys, key, value)}
end
def handle_call({:get, key}, _from, keys) do
{:reply, Map.fetch(keys, key), keys}
end
end