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
|
||||
|
||||
Reference in New Issue
Block a user