Make LastFm user agent configurable
This commit is contained in:
+4
-1
@@ -24,12 +24,15 @@ config :music_library, MusicLibraryWeb.Endpoint,
|
|||||||
pubsub_server: MusicLibrary.PubSub,
|
pubsub_server: MusicLibrary.PubSub,
|
||||||
live_view: [signing_salt: "g/qw4SNo"]
|
live_view: [signing_salt: "g/qw4SNo"]
|
||||||
|
|
||||||
|
user_agent = "MusicLibrary/0.1.0 ( cloud8421@gmail.com )"
|
||||||
|
|
||||||
config :music_library, LastFm,
|
config :music_library, LastFm,
|
||||||
user: "username",
|
user: "username",
|
||||||
api: LastFm.APIImpl,
|
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",
|
||||||
|
user_agent: user_agent
|
||||||
|
|
||||||
# Configure esbuild (the version is required)
|
# Configure esbuild (the version is required)
|
||||||
config :esbuild,
|
config :esbuild,
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ defmodule LastFm.APIImpl do
|
|||||||
|
|
||||||
Logger.debug("Fetching data from #{sanitize_url(url, config.api_key)}")
|
Logger.debug("Fetching data from #{sanitize_url(url, config.api_key)}")
|
||||||
|
|
||||||
case json_get(url) do
|
case json_get(url, config.user_agent) do
|
||||||
{:ok, response} ->
|
{:ok, response} ->
|
||||||
{:ok,
|
{:ok,
|
||||||
response
|
response
|
||||||
@@ -61,7 +61,7 @@ defmodule LastFm.APIImpl do
|
|||||||
|
|
||||||
Logger.debug("Fetching data from #{sanitize_url(url, config.api_key)}")
|
Logger.debug("Fetching data from #{sanitize_url(url, config.api_key)}")
|
||||||
|
|
||||||
case json_get(url) do
|
case json_get(url, config.user_agent) do
|
||||||
{:ok, response} ->
|
{:ok, response} ->
|
||||||
{:ok,
|
{:ok,
|
||||||
response
|
response
|
||||||
@@ -77,11 +77,10 @@ defmodule LastFm.APIImpl do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp json_get(url) do
|
defp json_get(url, user_agent) do
|
||||||
req =
|
req =
|
||||||
Finch.build(:get, url, [
|
Finch.build(:get, url, [
|
||||||
# TODO: move user agent to config
|
{"User-Agent", user_agent}
|
||||||
{"User-Agent", "MusicLibrary/0.1.0 ( cloud8421@gmail.com )"}
|
|
||||||
])
|
])
|
||||||
|
|
||||||
case Finch.request(req, LastFm.Finch, @request_opts) do
|
case Finch.request(req, LastFm.Finch, @request_opts) do
|
||||||
|
|||||||
@@ -4,14 +4,16 @@ defmodule LastFm.Config do
|
|||||||
api_key: String.t(),
|
api_key: String.t(),
|
||||||
user: String.t(),
|
user: String.t(),
|
||||||
auto_refresh: boolean(),
|
auto_refresh: boolean(),
|
||||||
refresh_interval: pos_integer()
|
refresh_interval: pos_integer(),
|
||||||
|
user_agent: String.t()
|
||||||
}
|
}
|
||||||
|
|
||||||
defstruct api: LastFm.Api,
|
defstruct api: LastFm.Api,
|
||||||
api_key: "",
|
api_key: "",
|
||||||
user: "",
|
user: "",
|
||||||
auto_refresh: true,
|
auto_refresh: true,
|
||||||
refresh_interval: 60_000
|
refresh_interval: 60_000,
|
||||||
|
user_agent: "change me"
|
||||||
|
|
||||||
@spec resolve(atom) :: t
|
@spec resolve(atom) :: t
|
||||||
def resolve(otp_app) do
|
def resolve(otp_app) do
|
||||||
|
|||||||
@@ -8,12 +8,14 @@ defmodule LastFm.ConfigTest do
|
|||||||
api_key: api_key,
|
api_key: api_key,
|
||||||
user: user,
|
user: user,
|
||||||
auto_refresh: false,
|
auto_refresh: false,
|
||||||
refresh_interval: refresh_interval
|
refresh_interval: refresh_interval,
|
||||||
|
user_agent: user_agent
|
||||||
} =
|
} =
|
||||||
LastFm.Config.resolve(:music_library)
|
LastFm.Config.resolve(:music_library)
|
||||||
|
|
||||||
assert is_binary(api_key)
|
assert is_binary(api_key)
|
||||||
assert is_binary(user)
|
assert is_binary(user)
|
||||||
|
assert is_binary(user_agent)
|
||||||
assert is_integer(refresh_interval)
|
assert is_integer(refresh_interval)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user