Use LastFm.Config where needed
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
defmodule LastFm.APIBehaviour do
|
||||
alias LastFm.{Artist, Track}
|
||||
alias LastFm.{Artist, Config, Track}
|
||||
|
||||
@type musicbrainz_id :: String.t()
|
||||
@type user :: String.t()
|
||||
@type api_key :: String.t()
|
||||
@callback get_recent_tracks(user, api_key) :: {:ok, [Track.t()]} | {:error, String.t()}
|
||||
@callback get_artist_info(musicbrainz_id, api_key) :: {:ok, Artist.t()} | {:error, String.t()}
|
||||
@type config :: Config.t()
|
||||
@callback get_recent_tracks(config) :: {:ok, [Track.t()]} | {:error, String.t()}
|
||||
@callback get_artist_info(musicbrainz_id, config) :: {:ok, Artist.t()} | {:error, String.t()}
|
||||
end
|
||||
|
||||
+13
-9
@@ -17,18 +17,18 @@ defmodule LastFm.APIImpl do
|
||||
]
|
||||
|
||||
@impl true
|
||||
def get_recent_tracks(user, api_key) do
|
||||
def get_recent_tracks(config) do
|
||||
options = [
|
||||
method: "user.getrecenttracks",
|
||||
user: user,
|
||||
api_key: api_key,
|
||||
user: config.user,
|
||||
api_key: config.api_key,
|
||||
format: "json",
|
||||
limit: 50
|
||||
]
|
||||
|
||||
url = @base_url <> "?" <> URI.encode_query(options)
|
||||
|
||||
Logger.debug("Fetching data from #{sanitize_url(url, api_key)}")
|
||||
Logger.debug("Fetching data from #{sanitize_url(url, config.api_key)}")
|
||||
|
||||
case json_get(url) do
|
||||
{:ok, response} ->
|
||||
@@ -38,17 +38,19 @@ defmodule LastFm.APIImpl do
|
||||
|> Track.from_api_response()}
|
||||
|
||||
other ->
|
||||
msg = "Failed to fetch data from #{sanitize_url(url, api_key)}, reason: #{inspect(other)}"
|
||||
msg =
|
||||
"Failed to fetch data from #{sanitize_url(url, config.api_key)}, reason: #{inspect(other)}"
|
||||
|
||||
Logger.error(msg)
|
||||
{:error, msg}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def get_artist_info(artist_mbid, api_key) do
|
||||
def get_artist_info(artist_mbid, config) do
|
||||
options = [
|
||||
method: "artist.getInfo",
|
||||
api_key: api_key,
|
||||
api_key: config.api_key,
|
||||
mbid: artist_mbid,
|
||||
format: "json",
|
||||
limit: 50
|
||||
@@ -56,7 +58,7 @@ defmodule LastFm.APIImpl do
|
||||
|
||||
url = @base_url <> "?" <> URI.encode_query(options)
|
||||
|
||||
Logger.debug("Fetching data from #{sanitize_url(url, api_key)}")
|
||||
Logger.debug("Fetching data from #{sanitize_url(url, config.api_key)}")
|
||||
|
||||
case json_get(url) do
|
||||
{:ok, response} ->
|
||||
@@ -66,7 +68,9 @@ defmodule LastFm.APIImpl do
|
||||
|> Artist.from_api_response()}
|
||||
|
||||
other ->
|
||||
msg = "Failed to fetch data from #{sanitize_url(url, api_key)}, reason: #{inspect(other)}"
|
||||
msg =
|
||||
"Failed to fetch data from #{sanitize_url(url, config.api_key)}, reason: #{inspect(other)}"
|
||||
|
||||
Logger.error(msg)
|
||||
{:error, msg}
|
||||
end
|
||||
|
||||
@@ -15,4 +15,15 @@ defmodule LastFm.Config do
|
||||
def new(opts) do
|
||||
struct(__MODULE__, opts)
|
||||
end
|
||||
|
||||
@spec enabled?(t) :: boolean()
|
||||
def enabled?(config) do
|
||||
config.api && config.user !== "" && config.api_key !== ""
|
||||
end
|
||||
|
||||
def resolve(otp_app) do
|
||||
otp_app
|
||||
|> Application.get_env(LastFm)
|
||||
|> new()
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,9 +3,9 @@ defmodule LastFm.Refresh do
|
||||
|
||||
require Logger
|
||||
|
||||
alias LastFm.Feed
|
||||
alias LastFm.{Config, Feed}
|
||||
|
||||
@type config :: LastFm.Config.t()
|
||||
@type config :: Config.t()
|
||||
|
||||
@spec start_link(config) :: GenServer.on_start()
|
||||
def start_link(config) do
|
||||
@@ -20,7 +20,7 @@ defmodule LastFm.Refresh do
|
||||
@impl true
|
||||
@spec init(config) :: {:ok, config, {:continue, :refresh}} | :ignore
|
||||
def init(config) do
|
||||
if enabled?(config) do
|
||||
if Config.enabled?(config) do
|
||||
{:ok, config, {:continue, :refresh}}
|
||||
else
|
||||
:ignore
|
||||
@@ -31,7 +31,7 @@ defmodule LastFm.Refresh do
|
||||
@spec handle_call(:refresh, GenServer.from(), config) ::
|
||||
{:reply, :ok | {:error, term()}, config, pos_integer()}
|
||||
def handle_call(:refresh, _from, config) do
|
||||
case config.api.get_recent_tracks(config.user, config.api_key) do
|
||||
case config.api.get_recent_tracks(config) do
|
||||
{:ok, tracks} ->
|
||||
Feed.update(tracks)
|
||||
{:reply, :ok, config, config.refresh_interval}
|
||||
@@ -53,7 +53,7 @@ defmodule LastFm.Refresh do
|
||||
def handle_info(:timeout, config), do: refresh(config)
|
||||
|
||||
defp refresh(config) do
|
||||
case config.api.get_recent_tracks(config.user, config.api_key) do
|
||||
case config.api.get_recent_tracks(config) do
|
||||
{:ok, tracks} ->
|
||||
Feed.update(tracks)
|
||||
{:noreply, config, config.refresh_interval}
|
||||
@@ -63,8 +63,4 @@ defmodule LastFm.Refresh do
|
||||
{:noreply, config, config.refresh_interval}
|
||||
end
|
||||
end
|
||||
|
||||
defp enabled?(config) do
|
||||
config.api && config.api_key
|
||||
end
|
||||
end
|
||||
|
||||
@@ -15,7 +15,7 @@ defmodule MusicLibrary.Application do
|
||||
{DNSCluster, query: Application.get_env(:music_library, :dns_cluster_query) || :ignore},
|
||||
{Phoenix.PubSub, name: MusicLibrary.PubSub},
|
||||
MusicBrainz.Supervisor,
|
||||
{LastFm.Supervisor, last_fm_config()},
|
||||
{LastFm.Supervisor, LastFm.Config.resolve(:music_library)},
|
||||
# Start a worker by calling: MusicLibrary.Worker.start_link(arg)
|
||||
# {MusicLibrary.Worker, arg},
|
||||
# Start to serve requests, typically the last entry
|
||||
@@ -40,9 +40,4 @@ defmodule MusicLibrary.Application do
|
||||
# By default, sqlite migrations are run when using a release
|
||||
System.get_env("RELEASE_NAME") != nil
|
||||
end
|
||||
|
||||
defp last_fm_config do
|
||||
Application.get_env(:music_library, LastFm)
|
||||
|> LastFm.Config.new()
|
||||
end
|
||||
end
|
||||
|
||||
@@ -111,7 +111,7 @@ defmodule MusicLibrary.Records do
|
||||
end
|
||||
|
||||
def get_artist_info(musicbrainz_id) do
|
||||
last_fm().get_artist_info(musicbrainz_id, last_fm_api_key())
|
||||
last_fm().get_artist_info(musicbrainz_id, last_fm_config())
|
||||
end
|
||||
|
||||
def get_cover(id) do
|
||||
@@ -224,8 +224,5 @@ defmodule MusicLibrary.Records do
|
||||
Application.get_env(:music_library, :last_fm, LastFm.APIImpl)
|
||||
end
|
||||
|
||||
defp last_fm_api_key do
|
||||
Application.get_env(:music_library, LastFm)
|
||||
|> Keyword.fetch!(:api_key)
|
||||
end
|
||||
defp last_fm_config, do: LastFm.Config.resolve(:music_library)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user