Remove LastFm.APIBehaviour

This commit is contained in:
Claudio Ortolina
2025-02-27 13:43:24 +00:00
parent 3d11cd585d
commit 7ac2f7a815
7 changed files with 12 additions and 35 deletions
-1
View File
@@ -28,7 +28,6 @@ user_agent = "MusicLibrary/0.1.0 ( cloud8421@gmail.com )"
config :music_library, LastFm, config :music_library, LastFm,
user: "username", user: "username",
api: LastFm.APIImpl,
auto_refresh: true, auto_refresh: true,
refresh_interval: System.convert_time_unit(60, :second, :millisecond), refresh_interval: System.convert_time_unit(60, :second, :millisecond),
api_key: "change me", api_key: "change me",
+6 -4
View File
@@ -1,8 +1,10 @@
defmodule LastFm do defmodule LastFm do
alias LastFm.API
def get_artist_info(musicbrainz_id, name) do def get_artist_info(musicbrainz_id, name) do
last_fm_config = last_fm_config() last_fm_config = last_fm_config()
case LastFm.APIImpl.get_artist_info( case API.get_artist_info(
{:musicbrainz_id, musicbrainz_id}, {:musicbrainz_id, musicbrainz_id},
last_fm_config last_fm_config
) do ) do
@@ -12,7 +14,7 @@ defmodule LastFm do
{:error, :invalid_parameters} -> {:error, :invalid_parameters} ->
# Sometimes the artist info cannot be identified with the MusicBrainz ID, # Sometimes the artist info cannot be identified with the MusicBrainz ID,
# because Last.fm doesn't have that information. In that case, we try again with the artist name. # because Last.fm doesn't have that information. In that case, we try again with the artist name.
LastFm.APIImpl.get_artist_info({:name, name}, last_fm_config) API.get_artist_info({:name, name}, last_fm_config)
error -> error ->
error error
@@ -22,7 +24,7 @@ defmodule LastFm do
def get_similar_artists(musicbrainz_id, name) do def get_similar_artists(musicbrainz_id, name) do
last_fm_config = last_fm_config() last_fm_config = last_fm_config()
case LastFm.APIImpl.get_similar_artists( case API.get_similar_artists(
{:musicbrainz_id, musicbrainz_id}, {:musicbrainz_id, musicbrainz_id},
last_fm_config last_fm_config
) do ) do
@@ -32,7 +34,7 @@ defmodule LastFm do
{:error, :invalid_parameters} -> {:error, :invalid_parameters} ->
# Sometimes the artist info cannot be identified with the MusicBrainz ID, # Sometimes the artist info cannot be identified with the MusicBrainz ID,
# because Last.fm doesn't have that information. In that case, we try again with the artist name. # because Last.fm doesn't have that information. In that case, we try again with the artist name.
LastFm.APIImpl.get_similar_artists({:name, name}, last_fm_config) API.get_similar_artists({:name, name}, last_fm_config)
error -> error ->
error error
@@ -1,6 +1,4 @@
defmodule LastFm.APIImpl do defmodule LastFm.API do
@behaviour LastFm.APIBehaviour
require Logger require Logger
alias LastFm.{Artist, Track} alias LastFm.{Artist, Track}
@@ -28,7 +26,6 @@ defmodule LastFm.APIImpl do
defp map_error(29), do: :rate_limit_exceeded defp map_error(29), do: :rate_limit_exceeded
end end
@impl true
def get_recent_tracks(config) do def get_recent_tracks(config) do
params = params =
config config
@@ -42,7 +39,6 @@ defmodule LastFm.APIImpl do
|> get_request() |> get_request()
end end
@impl true
def get_artist_info(id_or_name_option, config) do def get_artist_info(id_or_name_option, config) do
params = params =
config config
@@ -57,7 +53,6 @@ defmodule LastFm.APIImpl do
|> get_request() |> get_request()
end end
@impl true
def get_similar_artists(id_or_name_option, config) do def get_similar_artists(id_or_name_option, config) do
params = params =
config config
-12
View File
@@ -1,12 +0,0 @@
defmodule LastFm.APIBehaviour do
alias LastFm.{Artist, Config, Track}
@type musicbrainz_id :: String.t()
@type name :: String.t()
@type config :: Config.t()
@callback get_recent_tracks(config) :: {:ok, [Track.t()]} | {:error, String.t()}
@callback get_artist_info({:musicbrainz_id, musicbrainz_id} | {:name, name}, config) ::
{:ok, Artist.t()} | {:error, String.t()}
@callback get_similar_artists({:musicbrainz_id, musicbrainz_id} | {:name, name}, config) ::
{:ok, [Artist.t()]} | {:error, String.t()}
end
+2 -8
View File
@@ -1,6 +1,5 @@
defmodule LastFm.Config do defmodule LastFm.Config do
@type t :: %__MODULE__{ @type t :: %__MODULE__{
api: module(),
api_key: String.t(), api_key: String.t(),
user: String.t(), user: String.t(),
auto_refresh: boolean(), auto_refresh: boolean(),
@@ -9,9 +8,8 @@ defmodule LastFm.Config do
req_options: Keyword.t() req_options: Keyword.t()
} }
@enforce_keys [:api, :api_key, :user] @enforce_keys [:api_key, :user]
defstruct api: LastFm.Api, defstruct api_key: "",
api_key: "",
user: "", user: "",
auto_refresh: true, auto_refresh: true,
refresh_interval: 60_000, refresh_interval: 60_000,
@@ -19,10 +17,6 @@ defmodule LastFm.Config do
req_options: [] req_options: []
@schema NimbleOptions.new!( @schema NimbleOptions.new!(
api: [
type: :atom,
required: true
],
api_key: [ api_key: [
type: :string, type: :string,
required: true required: true
+3 -3
View File
@@ -3,7 +3,7 @@ defmodule LastFm.Refresh do
require Logger require Logger
alias LastFm.{Config, Feed} alias LastFm.{API, Config, Feed}
@type config :: Config.t() @type config :: Config.t()
@@ -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) do case 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) do case 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}
-1
View File
@@ -4,7 +4,6 @@ defmodule LastFm.ConfigTest do
describe "resolve/1" do describe "resolve/1" do
test "reads data from application configuration" do test "reads data from application configuration" do
assert %LastFm.Config{ assert %LastFm.Config{
api: LastFm.APIImpl,
api_key: api_key, api_key: api_key,
user: user, user: user,
auto_refresh: false, auto_refresh: false,