-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.ex
61 lines (49 loc) · 1.66 KB
/
token.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
defmodule Skaro.IGDB.Token do
@moduledoc """
Fetching and caching OAuth token for IGDB API
"""
alias Skaro.HttpClient
require OpenTelemetry.Tracer, as: Tracer
@cache_key "igdb_api_token"
def fetch do
case ConCache.get(:external_api_cache, @cache_key) do
nil ->
Tracer.with_span "igdb.token.fetch", kind: :client do
fetch_token_from_remote()
end
token ->
{:ok, token}
end
end
defp fetch_token_from_remote do
with body when is_binary(body) <-
HttpClient.idempotent_post(
oauth_url(),
{:form,
[
{:client_id, client_id()},
{:client_secret, client_secret()},
{:grant_type, "client_credentials"}
]},
[]
),
{:ok, %{"access_token" => token, "expires_in" => expires_in}} <- Jason.decode(body) do
ConCache.put(:external_api_cache, @cache_key, %ConCache.Item{
value: token,
ttl: expires_in - 60
})
Tracer.set_status(OpenTelemetry.status(:ok, ""))
{:ok, token}
else
{:error, reason} = error_tuple ->
Tracer.set_status(OpenTelemetry.status(:error, "Twitch API error: #{reason}"))
error_tuple
{:ok, json} ->
Tracer.set_status(OpenTelemetry.status(:error, "Twitch API error: unknown response"))
{:error, "Unknown oauth2 response: #{inspect(json)}"}
end
end
defp oauth_url, do: Application.fetch_env!(:skaro, :igdb)[:oauth_url]
defp client_id, do: Application.fetch_env!(:skaro, :igdb)[:client_id]
defp client_secret, do: Application.fetch_env!(:skaro, :igdb)[:client_secret]
end