diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2020-12-26 21:32:44 -0500 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2020-12-26 21:32:44 -0500 |
commit | d11baa1ab6628781506ad17936dfcd5a9afd0821 (patch) | |
tree | 9c659069e71f84fd4ac9b9761115eb6e0190f3b7 /lib/polyjuice | |
parent | fix warnings in test (diff) |
add function for getting user/device ID
and use it when the user ID is unspecified when setting/getting account data
Diffstat (limited to 'lib/polyjuice')
-rw-r--r-- | lib/polyjuice/client.ex | 16 | ||||
-rw-r--r-- | lib/polyjuice/client/account.ex | 12 | ||||
-rw-r--r-- | lib/polyjuice/client/low_level.ex | 4 |
3 files changed, 26 insertions, 6 deletions
diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex index d7ad60a..361914d 100644 --- a/lib/polyjuice/client.ex +++ b/lib/polyjuice/client.ex @@ -309,6 +309,11 @@ defmodule Polyjuice.Client do end @impl GenServer + def handle_call(:get_user_and_device, _from, state) do + {:reply, {Map.get(state, :user_id), Map.get(state, :device_id)}, state} + end + + @impl GenServer def handle_cast({:set, new_state}, state) do Map.take(new_state, [:access_token, :user_id, :device_id]) |> (&Map.merge(state, &1)).() @@ -366,6 +371,13 @@ defmodule Polyjuice.Client do def transaction_id(client_api) @doc """ + Get the client's user and device IDs. + """ + @spec get_user_and_device(client_api :: Polyjuice.Client.API.t()) :: + {String.t() | nil, String.t() | nil} + def get_user_and_device(client_api) + + @doc """ Stop the client. """ @spec stop(Polyjuice.Client.t(), reason :: term, timeout()) :: :ok @@ -502,6 +514,10 @@ defmodule Polyjuice.Client do "#{Node.self()}_#{:erlang.system_time(:millisecond)}_#{:erlang.unique_integer()}" end + def get_user_and_device(%{pid: pid}) do + GenServer.call(pid, :get_user_and_device) + end + def stop(%{pid: pid}, reason \\ :normal, timeout \\ :infinity) do GenServer.stop(pid, reason, timeout) end diff --git a/lib/polyjuice/client/account.ex b/lib/polyjuice/client/account.ex index 0ad18c0..d7b8d0d 100644 --- a/lib/polyjuice/client/account.ex +++ b/lib/polyjuice/client/account.ex @@ -24,14 +24,14 @@ defmodule Polyjuice.Client.Account do """ @spec get_data( client_api :: Polyjuice.Client.API.t(), - user_id :: String.t(), + user_id :: String.t() | nil, event_type :: String.t() ) :: {:ok, map()} | any - def get_data(client_api, user_id, event_type) do + def get_data(client_api, user_id \\ nil, event_type) do Polyjuice.Client.API.call( client_api, %Polyjuice.Client.Endpoint.GetAccountData{ - user_id: user_id, + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0), type: event_type } ) @@ -45,15 +45,15 @@ defmodule Polyjuice.Client.Account do """ @spec put_data( client_api :: Polyjuice.Client.API.t(), - user_id :: String.t(), + user_id :: String.t() | nil, event_type :: String.t(), changes :: Map.t() ) :: {:ok} | any - def put_data(client_api, user_id, event_type, changes) do + def put_data(client_api, user_id \\ nil, event_type, changes) do Polyjuice.Client.API.call( client_api, %Polyjuice.Client.Endpoint.PutAccountData{ - user_id: user_id, + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0), type: event_type, account_data: changes } diff --git a/lib/polyjuice/client/low_level.ex b/lib/polyjuice/client/low_level.ex index 54acf83..91b39ae 100644 --- a/lib/polyjuice/client/low_level.ex +++ b/lib/polyjuice/client/low_level.ex @@ -118,6 +118,10 @@ defmodule Polyjuice.Client.LowLevel do "#{Node.self()}_#{:erlang.system_time(:millisecond)}_#{:erlang.unique_integer()}" end + def get_user_and_device(%{user_id: user_id, device_id: device_id}) do + {user_id, device_id} + end + def stop(_, _, _) do # don't need to do anything to stop it :ok |