From 67d07c0074aac617f07c1f5764670adad687a58c Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Sat, 19 Sep 2020 22:42:26 -0400 Subject: add function to return client PID and client struct --- lib/polyjuice/client.ex | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex index e0f25d7..618a217 100644 --- a/lib/polyjuice/client.ex +++ b/lib/polyjuice/client.ex @@ -105,6 +105,25 @@ defmodule Polyjuice.Client do GenServer.start(__MODULE__, [base_url, opts]) end + @doc """ + Start the client process and return both the PID and the client struct. + + The return value of this function is compatible with + `DynamicSupervisor.start_child/1`. + """ + @spec start_link_and_get_client(base_url :: String.t(), opts :: Keyword.t()) :: + {:ok, pid, __MODULE__.t()} + def start_link_and_get_client(base_url, opts) do + case start_link(base_url, opts) do + {:ok, pid} -> + client = get_client(pid) + {:ok, pid, client} + + error -> + error + end + end + @impl GenServer def init([base_url, opts]) do base_url = @@ -221,6 +240,7 @@ defmodule Polyjuice.Client do Get a struct that implements `Polyjuice.Client.API` from the pid given by `start/2` or `start_link/2`. """ + @spec get_client(pid :: pid) :: __MODULE__.t() def get_client(pid) do GenServer.call(pid, :get_client) end -- cgit v1.2.3