View Source Goth (Goth v1.3.0-rc.4)

A Goth token server.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Fetches the token, erroring if it is missing.

Fetches the token from the cache.

Starts the server.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

fetch!(name, timeout \\ 5000)

View Source (since 1.3.0)

Fetches the token, erroring if it is missing.

See fetch/2 for more information.

Link to this function

fetch(name, timeout \\ 5000)

View Source (since 1.3.0)

Fetches the token from the cache.

If the token is not in the cache, this function blocks for timeout milliseconds (defaults to 5000) while it is attempted to fetch it in the background.

To fetch the token bypassing the cache, see Goth.Token.fetch/1.

Link to this function

start_link(opts)

View Source (since 1.3.0)

Starts the server.

When the server is started, we attempt to fetch the token and store it in internal cache. If we fail, we'll retry with backoff.

options

Options

  • :name - a unique name to register the server under. It can be any term.

  • :source - the source to retrieve the token from.

    See documentation for the :source option in Goth.Token.fetch/1 for more information.

  • :refresh_after - Time in milliseconds after which the token will be automatically refreshed. Defaults to 3_300_000 (55 minutes; 5 minutes before the token, which is valid for 1h, expires)

  • :http_client - a function that makes the HTTP request. Defaults to using built-in integration with Hackney

    See documentation for the :http_client option in Goth.Token.fetch/1 for more information.

  • :prefetch - how to prefetch the token when the server starts. The possible options are :async to do it asynchronously or :sync to do it synchronously (that is, the server doesn't start until an attempt to fetch the token was made). Defaults to :async.

  • :max_retries - the maximum number of retries (default: 20)

  • :backoff_min - the minimum backoff interval (default: 1_000)

  • :backoff_max - the maximum backoff interval (default: 30_000)

  • :backoff_type - the backoff strategy, :exp for exponential, :rand for random and :rand_exp for random exponential (default: :rand_exp)

examples

Examples

Generate a token using a service account credentials file:

iex> credentials = "credentials.json" |> File.read!() |> Jason.decode!()
iex> {:ok, _} = Goth.start_link(name: MyApp.Goth, source: {:service_account, credentials, []})
iex> Goth.fetch!(MyApp.Goth)
%Goth.Token{...}

Retrieve the token using a refresh token:

iex> credentials = "credentials.json" |> File.read!() |> Jason.decode!()
iex> {:ok, _} = Goth.start_link(name: MyApp.Goth, source: {:refresh_token, credentials, []})
iex> Goth.fetch!(MyApp.Goth)
%Goth.Token{...}

Retrieve the token using the Google metadata server:

iex> {:ok, _} = Goth.start_link(name: MyApp.Goth, source: {:metadata, []})
iex> Goth.fetch!(MyApp.Goth)
%Goth.Token{...}