diff --git a/config/test.exs b/config/test.exs index 8d8cf0f..1acc318 100644 --- a/config/test.exs +++ b/config/test.exs @@ -28,7 +28,7 @@ config :pigeon, PigeonTest.APNS.JWT, config :pigeon, PigeonTest.FCM, adapter: Pigeon.FCM, - project_id: System.get_env("FCM_PROJECT"), - token_fetcher: PigeonTest.Goth + auth: PigeonTest.Goth, + project_id: System.get_env("FCM_PROJECT") config :pigeon, PigeonTest.Sandbox, adapter: Pigeon.Sandbox diff --git a/lib/pigeon/dispatcher.ex b/lib/pigeon/dispatcher.ex index d75dbff..fe381c6 100644 --- a/lib/pigeon/dispatcher.ex +++ b/lib/pigeon/dispatcher.ex @@ -12,8 +12,8 @@ defmodule Pigeon.Dispatcher do # FCM as an example, but use the relevant options for your push type. opts = [ adapter: Pigeon.FCM, - project_id: "example-project-123", - token_fetcher: YourApp.Goth + auth: YourApp.Goth, + project_id: "example-project-123" ] {:ok, pid} = Pigeon.Dispatcher.start_link(opts) @@ -61,9 +61,9 @@ defmodule Pigeon.Dispatcher do defp push_spec(%{type: "fcm"} = config) do {Pigeon.Dispatcher, [ adapter: Pigeon.FCM, + auth: String.to_existing_atom(config.auth), name: {:via, Registry, {Registry.YourApp, config.name}}, - project_id: config.project_id, - token_fetcher: String.to_existing_atom(config.token_fetcher) + project_id: config.project_id ]} end end diff --git a/lib/pigeon/fcm.ex b/lib/pigeon/fcm.ex index 190dcd9..3946ae8 100644 --- a/lib/pigeon/fcm.ex +++ b/lib/pigeon/fcm.ex @@ -21,8 +21,8 @@ defmodule Pigeon.FCM do config :your_app, YourApp.FCM, adapter: Pigeon.FCM, - project_id: "example-project-123", - token_fetcher: YourApp.Goth + auth: YourApp.Goth, + project_id: "example-project-123" ``` 3. Start your dispatcher on application boot. @@ -66,8 +66,8 @@ defmodule Pigeon.FCM do defp fcm_opts do [ adapter: Pigeon.FCM, - project_id: "example-project-123", - token_fetcher: YourApp.Goth + auth: YourApp.Goth, + project_id: "example-project-123" ] end end @@ -90,8 +90,9 @@ defmodule Pigeon.FCM do YourApp.FCM.push(n) ``` - ## Customizable Goth Token Fetcher - If you need a customizable `:token_fetcher` that handles fetching its own configuration, here's + ## Customizing Goth + + If you need a customizable `:auth` that handles fetching its own configuration, here's an example you can use to get started. For other `:source` configurations of `YourApp.Goth`, check out the `goth` documentation for [`Goth.start_link/1`](https://hexdocs.pm/goth/Goth.html#start_link/1) diff --git a/lib/pigeon/fcm/config.ex b/lib/pigeon/fcm/config.ex index ec388cb..c7f0441 100644 --- a/lib/pigeon/fcm/config.ex +++ b/lib/pigeon/fcm/config.ex @@ -1,7 +1,7 @@ defmodule Pigeon.FCM.Config do @moduledoc false - defstruct token_fetcher: nil, + defstruct auth: nil, project_id: nil, uri: ~c"fcm.googleapis.com", port: 443 @@ -11,10 +11,10 @@ defmodule Pigeon.FCM.Config do This is passed directly to `Goth.fetch!/1`. """ - @type token_fetcher :: module() | term() + @type auth :: module() | term() @type t :: %__MODULE__{ - token_fetcher: nil | token_fetcher(), + auth: nil | auth(), project_id: nil | String.t(), uri: String.t(), port: pos_integer() @@ -26,13 +26,13 @@ defmodule Pigeon.FCM.Config do ## Examples iex> Pigeon.FCM.Config.new( - ...> project_id: "example-project", - ...> token_fetcher: YourApp.Goth + ...> auth: YourApp.Goth, + ...> project_id: "example-project" ...> ) %Pigeon.FCM.Config{ + auth: YourApp.Goth, port: 443, project_id: "example-project", - token_fetcher: YourApp.Goth, uri: ~c"fcm.googleapis.com" } """ @@ -40,9 +40,9 @@ defmodule Pigeon.FCM.Config do opts = Map.new(opts) %__MODULE__{ + auth: opts[:auth], port: Map.get(opts, :port, 443), project_id: opts[:project_id], - token_fetcher: opts[:token_fetcher], uri: Map.get(opts, :uri, ~c"fcm.googleapis.com") } end @@ -90,7 +90,7 @@ defimpl Pigeon.Configurable, for: Pigeon.FCM.Config do _notification, _opts ) do - token = Goth.fetch!(config.token_fetcher) + token = Goth.fetch!(config.auth) [ {":method", "POST"}, @@ -134,10 +134,10 @@ defimpl Pigeon.Configurable, for: Pigeon.FCM.Config do |> Enum.each(&do_validate!(&1, config)) end - defp do_validate!({:token_fetcher, mod}, config) + defp do_validate!({:auth, mod}, config) when not is_atom(mod) or is_nil(mod) do raise Pigeon.ConfigError, - reason: "attempted to start without valid :token_fetcher module", + reason: "attempted to start without valid :auth module", config: redact(config) end diff --git a/test/pigeon/fcm_test.exs b/test/pigeon/fcm_test.exs index 5c1dff0..116d762 100644 --- a/test/pigeon/fcm_test.exs +++ b/test/pigeon/fcm_test.exs @@ -9,7 +9,7 @@ defmodule Pigeon.FCMTest do @data %{"message" => "Test push"} @invalid_project_msg ~r/^attempted to start without valid :project_id/ - @invalid_fetcher_msg ~r/^attempted to start without valid :token_fetcher module/ + @invalid_auth_msg ~r/^attempted to start without valid :auth module/ defp valid_fcm_reg_id do Application.get_env(:pigeon, :test)[:valid_fcm_reg_id] @@ -18,14 +18,14 @@ defmodule Pigeon.FCMTest do describe "init/1" do test "raises if configured with invalid project" do assert_raise(Pigeon.ConfigError, @invalid_project_msg, fn -> - [project_id: nil, token_fetcher: PigeonTest.Goth] + [project_id: nil, auth: PigeonTest.Goth] |> Pigeon.FCM.init() end) end - test "raises if configured with invalid token_fetcher module" do - assert_raise(Pigeon.ConfigError, @invalid_fetcher_msg, fn -> - [project_id: "example", token_fetcher: nil] + test "raises if configured with invalid auth module" do + assert_raise(Pigeon.ConfigError, @invalid_auth_msg, fn -> + [project_id: "example", auth: nil] |> Pigeon.FCM.init() end) end