From 17299ba3ec18854658174bbba6f6704cf47b5f27 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Sat, 19 Sep 2020 21:47:04 -0400 Subject: store client opts as a map and drop duplicate data --- lib/polyjuice/client.ex | 66 +++++++++++++++++++++----------------------- lib/polyjuice/client/sync.ex | 30 +++++++++----------- 2 files changed, 44 insertions(+), 52 deletions(-) diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex index 152735f..e0f25d7 100644 --- a/lib/polyjuice/client.ex +++ b/lib/polyjuice/client.ex @@ -64,11 +64,7 @@ defmodule Polyjuice.Client do base_url: String.t(), id: integer, pid: pid, - storage: Polyjuice.Client.Storage.t(), - handler: Polyjuice.Client.Handler.t(), - opts: list, - auto_retry_rate_limited: boolean, - test: boolean + opts: map } @enforce_keys [:base_url, :id] @@ -76,12 +72,7 @@ defmodule Polyjuice.Client do :base_url, :id, :pid, - :storage, - :handler, - sync: true, - opts: [], - auto_retry_rate_limited: true, - test: false + opts: %{} ] @doc """ @@ -122,16 +113,23 @@ defmodule Polyjuice.Client do client_id = Agent.get_and_update(Polyjuice.Client.ID, &{&1, &1 + 1}) - sync = Keyword.get(opts, :sync, true) - storage = Keyword.get(opts, :storage) - handler = Keyword.get(opts, :handler) + opts = + Map.new(opts) + |> Map.put_new(:sync, true) + |> Map.put_new(:test, false) + |> Map.put_new(:storage, nil) + |> Map.put_new(:handler, nil) + + sync = opts.sync + storage = opts.storage + handler = opts.handler # get/store access token, user ID, and device ID from/to storage # - if they're specified, use the specified values, and store them # - otherwise, see if we have stored values, and use them if so # - otherwise, use nil access_token = - case Keyword.get(opts, :access_token) do + case Map.get(opts, :access_token) do nil -> if storage do Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "access_token") @@ -146,7 +144,7 @@ defmodule Polyjuice.Client do end user_id = - case Keyword.get(opts, :user_id) do + case Map.get(opts, :user_id) do nil -> if storage do Polyjuice.Client.Storage.kv_get(storage, "ca.uhoreg.polyjuice", "user_id") @@ -161,7 +159,7 @@ defmodule Polyjuice.Client do end device_id = - case Keyword.get(opts, :device_id) do + case Map.get(opts, :device_id) do nil -> if storage && user_id do # it doesn't make sense to try to fetch a device ID if there's no user ID @@ -200,11 +198,7 @@ defmodule Polyjuice.Client do base_url: base_url, id: client_id, pid: self(), - storage: storage, - handler: handler, - sync: sync, - opts: opts, - test: Keyword.get(opts, :test, false) + opts: opts } }} end @@ -310,7 +304,7 @@ defmodule Polyjuice.Client do defimpl Polyjuice.Client.API do def call( - %{base_url: base_url, id: id, pid: pid, test: test} = client, + %{base_url: base_url, id: id, pid: pid, opts: %{test: test}} = client, endpoint ) do %Polyjuice.Client.Endpoint.HttpSpec{ @@ -376,7 +370,7 @@ defmodule Polyjuice.Client do 401 -> # If the server says that our access token is invalid, kill the # sync, notify the handler, and forget the access token - if (client.storage || client.handler || client.sync) && + if (client.opts.storage || client.opts.handler || client.opts.sync) && Polyjuice.Client.Endpoint.get_header(headers, "content-type") == "application/json" do str_body = if stream_response, do: Enum.join(body), else: body @@ -387,8 +381,8 @@ defmodule Polyjuice.Client do Polyjuice.Client.set_logged_out( pid, - client.storage, - client.handler, + client.opts.storage, + client.opts.handler, Map.get(json, "soft_logout", false) ) end @@ -400,7 +394,7 @@ defmodule Polyjuice.Client do 429 -> # retry request if it was rate limited - if client.auto_retry_rate_limited && + if Map.get(client.opts, :auto_retry_rate_limited) && Polyjuice.Client.Endpoint.get_header(headers, "content-type") == "application/json" do str_body = if stream_response, do: Enum.join(body), else: body @@ -559,38 +553,38 @@ defmodule Polyjuice.Client do {:set, %{access_token: access_token, user_id: user_id, device_id: device_id}} ) - if client.storage do + if client.opts.storage do Polyjuice.Client.Storage.kv_put( - client.storage, + client.opts.storage, "ca.uhoreg.polyjuice", "access_token", access_token ) Polyjuice.Client.Storage.kv_put( - client.storage, + client.opts.storage, "ca.uhoreg.polyjuice", "user_id", user_id ) Polyjuice.Client.Storage.kv_put( - client.storage, + client.opts.storage, "ca.uhoreg.polyjuice", "device_id", device_id ) end - if client.handler do + if client.opts.handler do Polyjuice.Client.Handler.handle( - client.handler, + client.opts.handler, :logged_in, {user_id, device_id, Map.drop(http_ret, ["user_id", "device_id"])} ) end - if client.sync && client.handler do + if client.opts.sync && client.opts.handler do # make sure we don't already have a sync process running kill_sync(client.id) @@ -613,7 +607,9 @@ defmodule Polyjuice.Client do Log out an existing session. """ @spec log_out(client :: Polyjuice.Client.t()) :: {:ok} | any - def log_out(%Polyjuice.Client{id: id, pid: pid, storage: storage, handler: handler} = client) do + def log_out( + %Polyjuice.Client{id: id, pid: pid, opts: %{storage: storage, handler: handler}} = client + ) do kill_sync(id) case Polyjuice.Client.API.call( diff --git a/lib/polyjuice/client/sync.ex b/lib/polyjuice/client/sync.ex index 752cada..7a9ebf9 100644 --- a/lib/polyjuice/client/sync.ex +++ b/lib/polyjuice/client/sync.ex @@ -47,17 +47,13 @@ defmodule Polyjuice.Client.Sync do homeserver_url, id, pid, - opts + %{storage: storage, handler: handler, test: test} = opts ) do - storage = Keyword.fetch!(opts, :storage) - handler = Keyword.fetch!(opts, :handler) - test = Keyword.get(opts, :test, false) - # Figure out how to handle the filter (if any): can we pass it in straight # to the query, or do we need to get its ID. And if we get its ID, do we # already have it, or do we need to send it to the server? {filter, set_filter} = - case Keyword.get(opts, :sync_filter) do + case Map.get(opts, :sync_filter) do nil -> {nil, nil} @@ -72,19 +68,19 @@ defmodule Polyjuice.Client.Sync do end query_params = - Enum.reduce( - opts, - if filter do - [{"timeout", @sync_timeout}, {"filter", filter}] - else - [{"timeout", @sync_timeout}] + [ + [timeout: @sync_timeout], + if(filter, do: [filter: filter], else: []), + case Map.get(opts, :sync_full_state) do + nil -> [] + full_state -> [full_state: full_state] end, - fn - {:full_state, full_state}, acc -> [{"full_state", full_state} | acc] - {:set_presence, set_presence}, acc -> [{"set_presence", set_presence} | acc] - _, acc -> acc + case Map.get(opts, :set_presence) do + nil -> [] + set_presence -> [set_presence: set_presence] end - ) + ] + |> Enum.concat() |> URI.encode_query() uri = URI.merge(homeserver_url, @sync_path) -- cgit v1.2.3