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,
user: "username",
api: LastFm.APIImpl,
auto_refresh: true,
refresh_interval: System.convert_time_unit(60, :second, :millisecond),
api_key: "change me",
+6 -4
View File
@@ -1,8 +1,10 @@
defmodule LastFm do
alias LastFm.API
def get_artist_info(musicbrainz_id, name) do
last_fm_config = last_fm_config()
case LastFm.APIImpl.get_artist_info(
case API.get_artist_info(
{:musicbrainz_id, musicbrainz_id},
last_fm_config
) do
@@ -12,7 +14,7 @@ defmodule LastFm do
{:error, :invalid_parameters} ->
# 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.
LastFm.APIImpl.get_artist_info({:name, name}, last_fm_config)
API.get_artist_info({:name, name}, last_fm_config)
error ->
error
@@ -22,7 +24,7 @@ defmodule LastFm do
def get_similar_artists(musicbrainz_id, name) do
last_fm_config = last_fm_config()
case LastFm.APIImpl.get_similar_artists(
case API.get_similar_artists(
{:musicbrainz_id, musicbrainz_id},
last_fm_config
) do
@@ -32,7 +34,7 @@ defmodule LastFm do
{:error, :invalid_parameters} ->
# 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.
LastFm.APIImpl.get_similar_artists({:name, name}, last_fm_config)
API.get_similar_artists({:name, name}, last_fm_config)
error ->
error
@@ -1,6 +1,4 @@
defmodule LastFm.APIImpl do
@behaviour LastFm.APIBehaviour
defmodule LastFm.API do
require Logger
alias LastFm.{Artist, Track}
@@ -28,7 +26,6 @@ defmodule LastFm.APIImpl do
defp map_error(29), do: :rate_limit_exceeded
end
@impl true
def get_recent_tracks(config) do
params =
config
@@ -42,7 +39,6 @@ defmodule LastFm.APIImpl do
|> get_request()
end
@impl true
def get_artist_info(id_or_name_option, config) do
params =
config
@@ -57,7 +53,6 @@ defmodule LastFm.APIImpl do
|> get_request()
end
@impl true
def get_similar_artists(id_or_name_option, config) do
params =
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
@type t :: %__MODULE__{
api: module(),
api_key: String.t(),
user: String.t(),
auto_refresh: boolean(),
@@ -9,9 +8,8 @@ defmodule LastFm.Config do
req_options: Keyword.t()
}
@enforce_keys [:api, :api_key, :user]
defstruct api: LastFm.Api,
api_key: "",
@enforce_keys [:api_key, :user]
defstruct api_key: "",
user: "",
auto_refresh: true,
refresh_interval: 60_000,
@@ -19,10 +17,6 @@ defmodule LastFm.Config do
req_options: []
@schema NimbleOptions.new!(
api: [
type: :atom,
required: true
],
api_key: [
type: :string,
required: true
+3 -3
View File
@@ -3,7 +3,7 @@ defmodule LastFm.Refresh do
require Logger
alias LastFm.{Config, Feed}
alias LastFm.{API, Config, Feed}
@type config :: Config.t()
@@ -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) do
case 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) do
case API.get_recent_tracks(config) do
{:ok, tracks} ->
Feed.update(tracks)
{:noreply, config, config.refresh_interval}
-1
View File
@@ -4,7 +4,6 @@ defmodule LastFm.ConfigTest do
describe "resolve/1" do
test "reads data from application configuration" do
assert %LastFm.Config{
api: LastFm.APIImpl,
api_key: api_key,
user: user,
auto_refresh: false,