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