diff --git a/config/config.exs b/config/config.exs index 082ead88..9fce067a 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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", diff --git a/lib/last_fm.ex b/lib/last_fm.ex index 2ee6736c..e3ee8b20 100644 --- a/lib/last_fm.ex +++ b/lib/last_fm.ex @@ -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 diff --git a/lib/last_fm/api_impl.ex b/lib/last_fm/api.ex similarity index 97% rename from lib/last_fm/api_impl.ex rename to lib/last_fm/api.ex index 4892f98a..b56eb1af 100644 --- a/lib/last_fm/api_impl.ex +++ b/lib/last_fm/api.ex @@ -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 diff --git a/lib/last_fm/api_behaviour.ex b/lib/last_fm/api_behaviour.ex deleted file mode 100644 index 91e6213c..00000000 --- a/lib/last_fm/api_behaviour.ex +++ /dev/null @@ -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 diff --git a/lib/last_fm/config.ex b/lib/last_fm/config.ex index 17051da3..975b6a6e 100644 --- a/lib/last_fm/config.ex +++ b/lib/last_fm/config.ex @@ -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 diff --git a/lib/last_fm/refresh.ex b/lib/last_fm/refresh.ex index dc797119..79003c4c 100644 --- a/lib/last_fm/refresh.ex +++ b/lib/last_fm/refresh.ex @@ -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} diff --git a/test/last_fm/config_test.exs b/test/last_fm/config_test.exs index 981093c7..1b5b6844 100644 --- a/test/last_fm/config_test.exs +++ b/test/last_fm/config_test.exs @@ -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,