From 808e2adcdae52f70cc71f32ad8ad3f7dad2fce7c Mon Sep 17 00:00:00 2001 From: Milton Mazzarri Date: Tue, 19 Mar 2019 12:18:34 -0500 Subject: [PATCH] Allow runtime configuration changes --- lib/hunter/account.ex | 30 +++++++++++++++--------------- lib/hunter/application.ex | 6 +++--- lib/hunter/attachment.ex | 4 ++-- lib/hunter/card.ex | 4 ++-- lib/hunter/client.ex | 5 +++-- lib/hunter/config.ex | 11 +++++------ lib/hunter/context.ex | 4 ++-- lib/hunter/domain.ex | 8 ++++---- lib/hunter/instance.ex | 4 ++-- lib/hunter/notification.ex | 10 +++++----- lib/hunter/relationship.ex | 16 ++++++++-------- lib/hunter/report.ex | 6 +++--- lib/hunter/result.ex | 4 ++-- lib/hunter/status.ex | 28 ++++++++++++++-------------- test/test_helper.exs | 1 + 15 files changed, 71 insertions(+), 70 deletions(-) diff --git a/lib/hunter/account.ex b/lib/hunter/account.ex index 09f0eab..b7022d9 100644 --- a/lib/hunter/account.ex +++ b/lib/hunter/account.ex @@ -27,7 +27,7 @@ defmodule Hunter.Account do * `bot` - whether this account is a bot or not """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ id: non_neg_integer, @@ -100,7 +100,7 @@ defmodule Hunter.Account do """ @spec verify_credentials(Hunter.Client.t()) :: Hunter.Account.t() def verify_credentials(conn) do - @hunter_api.verify_credentials(conn) + Config.hunter_api().verify_credentials(conn) end @doc """ @@ -121,7 +121,7 @@ defmodule Hunter.Account do """ @spec update_credentials(Hunter.Client.t(), map) :: Hunter.Account.t() def update_credentials(conn, data) do - @hunter_api.update_credentials(conn, data) + Config.hunter_api().update_credentials(conn, data) end @doc """ @@ -135,7 +135,7 @@ defmodule Hunter.Account do """ @spec account(Hunter.Client.t(), non_neg_integer) :: Hunter.Account.t() def account(conn, id) do - @hunter_api.account(conn, id) + Config.hunter_api().account(conn, id) end @doc """ @@ -161,7 +161,7 @@ defmodule Hunter.Account do """ @spec followers(Hunter.Client.t(), non_neg_integer, Keyword.t()) :: [Hunter.Account.t()] def followers(conn, id, options \\ []) do - @hunter_api.followers(conn, id, options) + Config.hunter_api().followers(conn, id, options) end @doc """ @@ -187,7 +187,7 @@ defmodule Hunter.Account do """ @spec following(Hunter.Client.t(), non_neg_integer, Keyword.t()) :: [Hunter.Account.t()] def following(conn, id, options \\ []) do - @hunter_api.following(conn, id, options) + Config.hunter_api().following(conn, id, options) end @doc """ @@ -201,7 +201,7 @@ defmodule Hunter.Account do """ @spec follow_by_uri(Hunter.Client.t(), String.t()) :: Hunter.Account.t() def follow_by_uri(conn, uri) do - @hunter_api.follow_by_uri(conn, uri) + Config.hunter_api().follow_by_uri(conn, uri) end @doc """ @@ -225,7 +225,7 @@ defmodule Hunter.Account do limit: Keyword.get(options, :limit, 40) } - @hunter_api.search_account(conn, opts) + Config.hunter_api().search_account(conn, opts) end @doc """ @@ -244,7 +244,7 @@ defmodule Hunter.Account do """ @spec blocks(Hunter.Client.t(), Keyword.t()) :: [Hunter.Account.t()] def blocks(conn, options \\ []) do - @hunter_api.blocks(conn, options) + Config.hunter_api().blocks(conn, options) end @doc """ @@ -264,7 +264,7 @@ defmodule Hunter.Account do """ @spec follow_requests(Hunter.Client.t(), Keyword.t()) :: [Hunter.Account.t()] def follow_requests(conn, options \\ []) do - @hunter_api.follow_requests(conn, options) + Config.hunter_api().follow_requests(conn, options) end @doc """ @@ -284,7 +284,7 @@ defmodule Hunter.Account do """ @spec mutes(Hunter.Client.t(), Keyword.t()) :: [Hunter.Account.t()] def mutes(conn, options \\ []) do - @hunter_api.mutes(conn, options) + Config.hunter_api().mutes(conn, options) end @doc """ @@ -298,7 +298,7 @@ defmodule Hunter.Account do """ @spec accept_follow_request(Hunter.Client.t(), non_neg_integer) :: boolean def accept_follow_request(conn, id) do - @hunter_api.follow_request_action(conn, id, :authorize) + Config.hunter_api().follow_request_action(conn, id, :authorize) end @doc """ @@ -312,7 +312,7 @@ defmodule Hunter.Account do """ @spec reject_follow_request(Hunter.Client.t(), non_neg_integer) :: boolean def reject_follow_request(conn, id) do - @hunter_api.follow_request_action(conn, id, :reject) + Config.hunter_api().follow_request_action(conn, id, :reject) end @doc """ @@ -333,7 +333,7 @@ defmodule Hunter.Account do """ @spec reblogged_by(Hunter.Client.t(), non_neg_integer, Keyword.t()) :: [Hunter.Account.t()] def reblogged_by(conn, id, options \\ []) do - @hunter_api.reblogged_by(conn, id, options) + Config.hunter_api().reblogged_by(conn, id, options) end @doc """ @@ -355,6 +355,6 @@ defmodule Hunter.Account do @spec favourited_by(Hunter.Client.t(), non_neg_integer, Keyword.t()) :: [Hunter.Account.t()] def favourited_by(conn, id, options \\ []) do - @hunter_api.favourited_by(conn, id, options) + Config.hunter_api().favourited_by(conn, id, options) end end diff --git a/lib/hunter/application.ex b/lib/hunter/application.ex index 281ad11..79b79d9 100644 --- a/lib/hunter/application.ex +++ b/lib/hunter/application.ex @@ -12,7 +12,7 @@ defmodule Hunter.Application do * `client_secret` - client secret """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ id: non_neg_integer, @@ -70,9 +70,9 @@ defmodule Hunter.Application do options \\ [] ) do save? = Keyword.get(options, :save?, false) - base_url = Keyword.get(options, :api_base_url, Hunter.Config.api_base_url()) + base_url = Keyword.get(options, :api_base_url, Config.api_base_url()) - app = @hunter_api.create_app(client_name, redirect_uris, scopes, website, base_url) + app = Config.hunter_api().create_app(client_name, redirect_uris, scopes, website, base_url) if save?, do: save_credentials(client_name, app) diff --git a/lib/hunter/attachment.ex b/lib/hunter/attachment.ex index 9a46444..199436b 100644 --- a/lib/hunter/attachment.ex +++ b/lib/hunter/attachment.ex @@ -23,7 +23,7 @@ defmodule Hunter.Attachment do available and local `url` is missing """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ id: non_neg_integer, @@ -55,6 +55,6 @@ defmodule Hunter.Attachment do """ @spec upload_media(Hunter.Client.t(), Path.t(), Keyword.t()) :: Hunter.Attachment.t() def upload_media(conn, file, options \\ []) do - @hunter_api.upload_media(conn, file, options) + Config.hunter_api().upload_media(conn, file, options) end end diff --git a/lib/hunter/card.ex b/lib/hunter/card.ex index cd1261f..44881de 100644 --- a/lib/hunter/card.ex +++ b/lib/hunter/card.ex @@ -21,7 +21,7 @@ defmodule Hunter.Card do * `height` - height in pixels """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ url: String.t(), @@ -74,6 +74,6 @@ defmodule Hunter.Card do """ @spec card_by_status(Hunter.Client.t(), non_neg_integer) :: Hunter.Card.t() def card_by_status(conn, id) do - @hunter_api.card_by_status(conn, id) + Config.hunter_api().card_by_status(conn, id) end end diff --git a/lib/hunter/client.ex b/lib/hunter/client.ex index 10fc74d..ecccbaa 100644 --- a/lib/hunter/client.ex +++ b/lib/hunter/client.ex @@ -3,7 +3,7 @@ defmodule Hunter.Client do Defines a `Hunter` client """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ base_url: String.t(), @@ -48,6 +48,7 @@ defmodule Hunter.Client do """ @spec log_in(Hunter.Application.t(), String.t(), String.t(), String.t()) :: Hunter.Client.t() def log_in(app, username, password, base_url \\ nil) do - @hunter_api.log_in(app, username, password, base_url || Hunter.Config.api_base_url()) + base_url = base_url || Config.api_base_url() + Config.hunter_api().log_in(app, username, password, base_url) end end diff --git a/lib/hunter/config.ex b/lib/hunter/config.ex index 41096c3..a867a88 100644 --- a/lib/hunter/config.ex +++ b/lib/hunter/config.ex @@ -1,15 +1,14 @@ defmodule Hunter.Config do - @moduledoc false - - @hunter_api Application.get_env(:hunter, :hunter_api, Hunter.Api.HTTPClient) - @api_base_url "https://mastodon.social" + @moduledoc """ + Hunter configuration. + """ def hunter_api do - @hunter_api + Application.get_env(:hunter, :hunter_api, Hunter.Api.HTTPClient) end def api_base_url do - @api_base_url + Application.get_env(:hunter, :api_base_url, "https://mastodon.social") end def home do diff --git a/lib/hunter/context.ex b/lib/hunter/context.ex index 60c32fd..fb09157 100644 --- a/lib/hunter/context.ex +++ b/lib/hunter/context.ex @@ -8,7 +8,7 @@ defmodule Hunter.Context do * `descendants` - The descendants of the status in the conversation, as a list of Statuses """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ ancestors: [Hunter.Status.t()], @@ -29,6 +29,6 @@ defmodule Hunter.Context do """ @spec status_context(Hunter.Client.t(), non_neg_integer) :: Hunter.Context.t() def status_context(conn, id) do - @hunter_api.status_context(conn, id) + Config.hunter_api().status_context(conn, id) end end diff --git a/lib/hunter/domain.ex b/lib/hunter/domain.ex index d8c6425..032c310 100644 --- a/lib/hunter/domain.ex +++ b/lib/hunter/domain.ex @@ -3,7 +3,7 @@ defmodule Hunter.Domain do Domain blocks """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @doc """ Fetch user's blocked domains @@ -22,7 +22,7 @@ defmodule Hunter.Domain do """ @spec blocked_domains(Hunter.Client.t(), Keyword.t()) :: list def blocked_domains(conn, options \\ []) do - @hunter_api.blocked_domains(conn, options) + Config.hunter_api().blocked_domains(conn, options) end @doc """ @@ -36,7 +36,7 @@ defmodule Hunter.Domain do """ @spec block_domain(Hunter.Client.t(), String.t()) :: boolean def block_domain(conn, domain) do - @hunter_api.block_domain(conn, domain) + Config.hunter_api().block_domain(conn, domain) end @doc """ @@ -50,6 +50,6 @@ defmodule Hunter.Domain do """ @spec unblock_domain(Hunter.Client.t(), String.t()) :: boolean def unblock_domain(conn, domain) do - @hunter_api.unblock_domain(conn, domain) + Config.hunter_api().unblock_domain(conn, domain) end end diff --git a/lib/hunter/instance.ex b/lib/hunter/instance.ex index 7c684a9..369cbfb 100644 --- a/lib/hunter/instance.ex +++ b/lib/hunter/instance.ex @@ -15,7 +15,7 @@ defmodule Hunter.Instance do * `urls` - `streaming_api` """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ uri: String.t(), @@ -48,6 +48,6 @@ defmodule Hunter.Instance do """ @spec instance_info(Hunter.Client.t()) :: Hunter.Instance.t() def instance_info(conn) do - @hunter_api.instance_info(conn) + Config.hunter_api().instance_info(conn) end end diff --git a/lib/hunter/notification.ex b/lib/hunter/notification.ex index ce2bf47..bb586c0 100644 --- a/lib/hunter/notification.ex +++ b/lib/hunter/notification.ex @@ -14,7 +14,7 @@ defmodule Hunter.Notification do * `status` - The `Hunter.Status` associated with the notification, if applicable """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ id: String.t(), @@ -49,7 +49,7 @@ defmodule Hunter.Notification do """ @spec notifications(Hunter.Client.t(), Keyword.t()) :: [Hunter.Notification.t()] def notifications(conn, options \\ []) do - @hunter_api.notifications(conn, options) + Config.hunter_api().notifications(conn, options) end @doc """ @@ -68,7 +68,7 @@ defmodule Hunter.Notification do """ @spec notification(Hunter.Client.t(), non_neg_integer) :: Hunter.Notification.t() def notification(conn, id) do - @hunter_api.notification(conn, id) + Config.hunter_api().notification(conn, id) end @doc """ @@ -81,7 +81,7 @@ defmodule Hunter.Notification do """ @spec clear_notifications(Hunter.Client.t()) :: boolean def clear_notifications(conn) do - @hunter_api.clear_notifications(conn) + Config.hunter_api().clear_notifications(conn) end @doc """ @@ -95,6 +95,6 @@ defmodule Hunter.Notification do """ @spec clear_notification(Hunter.Client.t(), non_neg_integer) :: boolean def clear_notification(conn, id) do - @hunter_api.clear_notification(conn, id) + Config.hunter_api().clear_notification(conn, id) end end diff --git a/lib/hunter/relationship.ex b/lib/hunter/relationship.ex index ba241dc..39eff11 100644 --- a/lib/hunter/relationship.ex +++ b/lib/hunter/relationship.ex @@ -16,7 +16,7 @@ defmodule Hunter.Relationship do * `domain_blocking` - whether the user is currently blocking the user's domain """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ id: non_neg_integer, @@ -42,7 +42,7 @@ defmodule Hunter.Relationship do """ @spec relationships(Hunter.Client.t(), [non_neg_integer]) :: [Hunter.Relationship.t()] def relationships(conn, ids) do - @hunter_api.relationships(conn, ids) + Config.hunter_api().relationships(conn, ids) end @doc """ @@ -56,7 +56,7 @@ defmodule Hunter.Relationship do """ @spec follow(Hunter.Client.t(), non_neg_integer) :: Hunter.Relationship.t() def follow(conn, id) do - @hunter_api.follow(conn, id) + Config.hunter_api().follow(conn, id) end @doc """ @@ -70,7 +70,7 @@ defmodule Hunter.Relationship do """ @spec unfollow(Hunter.Client.t(), non_neg_integer) :: Hunter.Relationship.t() def unfollow(conn, id) do - @hunter_api.unfollow(conn, id) + Config.hunter_api().unfollow(conn, id) end @doc """ @@ -84,7 +84,7 @@ defmodule Hunter.Relationship do """ @spec block(Hunter.Client.t(), non_neg_integer) :: Hunter.Relationship.t() def block(conn, id) do - @hunter_api.block(conn, id) + Config.hunter_api().block(conn, id) end @doc """ @@ -96,7 +96,7 @@ defmodule Hunter.Relationship do """ @spec unblock(Hunter.Client.t(), non_neg_integer) :: Hunter.Relationship.t() def unblock(conn, id) do - @hunter_api.unblock(conn, id) + Config.hunter_api().unblock(conn, id) end @doc """ @@ -110,7 +110,7 @@ defmodule Hunter.Relationship do """ @spec mute(Hunter.Client.t(), non_neg_integer) :: Hunter.Relationship.t() def mute(conn, id) do - @hunter_api.mute(conn, id) + Config.hunter_api().mute(conn, id) end @doc """ @@ -124,6 +124,6 @@ defmodule Hunter.Relationship do """ @spec unmute(Hunter.Client.t(), non_neg_integer) :: Hunter.Relationship.t() def unmute(conn, id) do - @hunter_api.unmute(conn, id) + Config.hunter_api().unmute(conn, id) end end diff --git a/lib/hunter/report.ex b/lib/hunter/report.ex index d8d1b4c..1c126be 100644 --- a/lib/hunter/report.ex +++ b/lib/hunter/report.ex @@ -11,7 +11,7 @@ defmodule Hunter.Report do * `action_taken` - action taken in response to the report """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ id: non_neg_integer, @@ -31,7 +31,7 @@ defmodule Hunter.Report do """ @spec reports(Hunter.Client.t()) :: [Hunter.Report.t()] def reports(conn) do - @hunter_api.reports(conn) + Config.hunter_api().reports(conn) end @doc """ @@ -48,6 +48,6 @@ defmodule Hunter.Report do @spec report(Hunter.Client.t(), non_neg_integer, [non_neg_integer], String.t()) :: Hunter.Report.t() def report(conn, account_id, status_ids, comment) do - @hunter_api.report(conn, account_id, status_ids, comment) + Config.hunter_api().report(conn, account_id, status_ids, comment) end end diff --git a/lib/hunter/result.ex b/lib/hunter/result.ex index 790a322..49d6ed4 100644 --- a/lib/hunter/result.ex +++ b/lib/hunter/result.ex @@ -9,7 +9,7 @@ defmodule Hunter.Result do * `hashtags` - list of matched hashtags, as strings """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ accounts: [Hunter.Account.t()], @@ -40,6 +40,6 @@ defmodule Hunter.Result do """ @spec search(Hunter.Client.t(), String.t(), Keyword.t()) :: Hunter.Result.t() def search(conn, query, options \\ []) do - @hunter_api.search(conn, query, options) + Config.hunter_api().search(conn, query, options) end end diff --git a/lib/hunter/status.ex b/lib/hunter/status.ex index 686bc55..7fa1e83 100644 --- a/lib/hunter/status.ex +++ b/lib/hunter/status.ex @@ -27,10 +27,10 @@ defmodule Hunter.Status do * `application` - `Hunter.Application` from which the status was posted * `language` - detected language for the status, default: en - **NOTE**: When `spoiler_text` is present, `sensitive` is true + **NOTE**: When `spoiler_text` is present, `sensitive` is true """ - @hunter_api Hunter.Config.hunter_api() + alias Hunter.Config @type t :: %__MODULE__{ id: non_neg_integer, @@ -103,7 +103,7 @@ defmodule Hunter.Status do """ @spec create_status(Hunter.Client.t(), String.t(), Keyword.t()) :: Hunter.Status.t() | no_return def create_status(conn, status, options \\ []) do - @hunter_api.create_status(conn, status, Map.new(options)) + Config.hunter_api().create_status(conn, status, Map.new(options)) end @doc """ @@ -117,7 +117,7 @@ defmodule Hunter.Status do """ @spec status(Hunter.Client.t(), status_id) :: Hunter.Status.t() def status(conn, id) do - @hunter_api.status(conn, id) + Config.hunter_api().status(conn, id) end @doc """ @@ -131,7 +131,7 @@ defmodule Hunter.Status do """ @spec destroy_status(Hunter.Client.t(), status_id) :: boolean def destroy_status(conn, id) do - @hunter_api.destroy_status(conn, id) + Config.hunter_api().destroy_status(conn, id) end @doc """ @@ -145,7 +145,7 @@ defmodule Hunter.Status do """ @spec reblog(Hunter.Client.t(), status_id) :: Hunter.Status.t() def reblog(conn, id) do - @hunter_api.reblog(conn, id) + Config.hunter_api().reblog(conn, id) end @doc """ @@ -159,7 +159,7 @@ defmodule Hunter.Status do """ @spec unreblog(Hunter.Client.t(), status_id) :: Hunter.Status.t() def unreblog(conn, id) do - @hunter_api.unreblog(conn, id) + Config.hunter_api().unreblog(conn, id) end @doc """ @@ -173,7 +173,7 @@ defmodule Hunter.Status do """ @spec favourite(Hunter.Client.t(), status_id) :: Hunter.Status.t() def favourite(conn, id) do - @hunter_api.favourite(conn, id) + Config.hunter_api().favourite(conn, id) end @doc """ @@ -187,7 +187,7 @@ defmodule Hunter.Status do """ @spec unfavourite(Hunter.Client.t(), status_id) :: Hunter.Status.t() def unfavourite(conn, id) do - @hunter_api.unfavourite(conn, id) + Config.hunter_api().unfavourite(conn, id) end @doc """ @@ -207,7 +207,7 @@ defmodule Hunter.Status do """ @spec favourites(Hunter.Client.t(), Keyword.t()) :: [Hunter.Status.t()] def favourites(conn, options \\ []) do - @hunter_api.favourites(conn, options) + Config.hunter_api().favourites(conn, options) end @doc """ @@ -230,7 +230,7 @@ defmodule Hunter.Status do """ @spec statuses(Hunter.Client.t(), status_id, Keyword.t()) :: [Hunter.Status.t()] def statuses(conn, account_id, options \\ []) do - @hunter_api.statuses(conn, account_id, Map.new(options)) + Config.hunter_api().statuses(conn, account_id, Map.new(options)) end @doc """ @@ -250,7 +250,7 @@ defmodule Hunter.Status do """ @spec home_timeline(Hunter.Client.t(), Keyword.t()) :: [Hunter.Status.t()] def home_timeline(conn, options \\ []) do - @hunter_api.home_timeline(conn, Map.new(options)) + Config.hunter_api().home_timeline(conn, Map.new(options)) end @doc """ @@ -271,7 +271,7 @@ defmodule Hunter.Status do """ @spec public_timeline(Hunter.Client.t(), Keyword.t()) :: [Hunter.Status.t()] def public_timeline(conn, options \\ []) do - @hunter_api.public_timeline(conn, Map.new(options)) + Config.hunter_api().public_timeline(conn, Map.new(options)) end @doc """ @@ -293,6 +293,6 @@ defmodule Hunter.Status do """ @spec hashtag_timeline(Hunter.Client.t(), [String.t()], Keyword.t()) :: [Hunter.Status.t()] def hashtag_timeline(conn, hashtag, options \\ []) do - @hunter_api.hashtag_timeline(conn, hashtag, Map.new(options)) + Config.hunter_api().hashtag_timeline(conn, hashtag, Map.new(options)) end end diff --git a/test/test_helper.exs b/test/test_helper.exs index 869559e..23b94a4 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1 +1,2 @@ ExUnit.start() +Application.put_env(:hunter, :hunter_api, Hunter.Api.InMemory)