From 23e4f3dab35e162d8f9be0b16de46bd1f5728fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wala?= Date: Wed, 12 Apr 2023 17:19:41 +0200 Subject: [PATCH 1/7] Add token configuration via config, update docs --- README.md | 7 ++++--- lib/jellyfish/client.ex | 25 ++++++++++++++++++++++--- lib/jellyfish/room.ex | 7 ++++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8daa959..87989f0 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,11 @@ end ## Usage -Make API calls to Jellyfish: +Make API calls to Jellyfish (authentication required, for more information see [Jellyfish docs](https://jellyfish-dev.github.io/jellyfish-docs/getting_started/authentication)): ```elixir -client = Jellyfish.Client.new("http://address-of-your-server.com") +# create client instance used in other functions to make the HTTP requests +client = Jellyfish.Client.new("http://address-of-your-server.com", "your-jellyfish-token") # Create room {:ok, %Jellyfish.Room{id: room_id}} = Jellyfish.Room.create(client, max_peers: 10) @@ -36,7 +37,7 @@ room_id # => "8878cd13-99a6-40d6-8d7e-8da23d803dab" # Add peer -{:ok, %Jellyfish.Peer{id: peer_id}, token} = Jellyfish.Room.add_peer(client, room_id, "webrtc") +{:ok, %Jellyfish.Peer{id: peer_id}, client_token} = Jellyfish.Room.add_peer(client, room_id, "webrtc") # Delete peer :ok = Jellyfish.Room.delete_peer(client, room_id, peer_id) diff --git a/lib/jellyfish/client.ex b/lib/jellyfish/client.ex index b3f4182..1f4866e 100644 --- a/lib/jellyfish/client.ex +++ b/lib/jellyfish/client.ex @@ -29,14 +29,33 @@ defmodule Jellyfish.Client do @doc """ Creates new instance of `t:Jellyfish.SDK.Client.t/0`. + ``` + #config.exs + config :jellyfish_server_sdk, token: "your-jellyfish-token" + + client = Jellyfish.Client.new("http://address-of-your-server.com") + # or pass the token explicitly + client = Jellyfish.Client.new("http://address-of-your-server.com", "your-jellyfish-token") + ``` + ## Parameters * `address` - url or IP address of the Jellyfish server instance * `token` - token used for authorizing HTTP requests. It's the same - token as the one configured in Jellyfish. + token as the one configured in Jellyfish. If not passed, value set via + `config :jellyfish_server_sdk, token: "your-token"` in `config.exs` is used. """ - @spec new(String.t(), String.t()) :: t() - def new(address, token) do + @spec new(String.t(), String.t() | nil) :: t() + def new(address, token \\ nil) + + def new(address, nil) do + token = Application.fetch_env!(:jellyfish_server_sdk, :token) + build_client(address, token) + end + + def new(address, token), do: build_client(address, token) + + defp build_client(address, token) do middleware = [ {Tesla.Middleware.BaseUrl, address}, {Tesla.Middleware.BearerAuth, token: token}, diff --git a/lib/jellyfish/room.ex b/lib/jellyfish/room.ex index 25263f8..12fdf6a 100644 --- a/lib/jellyfish/room.ex +++ b/lib/jellyfish/room.ex @@ -39,6 +39,11 @@ defmodule Jellyfish.Room do Id of the room, unique within Jellyfish instance. """ @type id :: String.t() + + @typedoc """ + Client token, created by Jellyfish. Required by client application to open connection to Jellyfish. + """ + @type client_token :: String.t() @typedoc """ Type describing room options. @@ -122,7 +127,7 @@ defmodule Jellyfish.Room do @doc """ Add a peer to the room with `room_id`. """ - @spec add_peer(Client.t(), id(), Peer.type()) :: {:ok, Peer.t()} | {:error, atom() | String.t()} + @spec add_peer(Client.t(), id(), Peer.type()) :: {:ok, Peer.t(), client_token()} | {:error, atom() | String.t()} def add_peer(client, room_id, type) do with {:ok, %Env{status: 201, body: body}} <- Tesla.post( From ee4d61d1b3b902f80751d6a25174968e9b10ee00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wala?= Date: Wed, 12 Apr 2023 17:38:31 +0200 Subject: [PATCH 2/7] Minor improvements --- README.md | 1 - lib/jellyfish/component.ex | 3 +-- lib/jellyfish/room.ex | 5 +++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 87989f0..09dc209 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,6 @@ end Make API calls to Jellyfish (authentication required, for more information see [Jellyfish docs](https://jellyfish-dev.github.io/jellyfish-docs/getting_started/authentication)): ```elixir -# create client instance used in other functions to make the HTTP requests client = Jellyfish.Client.new("http://address-of-your-server.com", "your-jellyfish-token") # Create room diff --git a/lib/jellyfish/component.ex b/lib/jellyfish/component.ex index 21ca3ce..ed1d1bf 100644 --- a/lib/jellyfish/component.ex +++ b/lib/jellyfish/component.ex @@ -26,10 +26,9 @@ defmodule Jellyfish.Component do """ @type type :: String.t() - # TODO update to proper link when it's done @typedoc """ Type describing component options. - For the list of available options, please refer to the [component's documentation](https://jellyfish-dev.github.io/jellyfish-docs/). + For the list of available options, please refer to the [documentation](https://jellyfish-dev.github.io/jellyfish-docs/). """ @type options :: Keyword.t() diff --git a/lib/jellyfish/room.ex b/lib/jellyfish/room.ex index 12fdf6a..47ad59c 100644 --- a/lib/jellyfish/room.ex +++ b/lib/jellyfish/room.ex @@ -39,7 +39,7 @@ defmodule Jellyfish.Room do Id of the room, unique within Jellyfish instance. """ @type id :: String.t() - + @typedoc """ Client token, created by Jellyfish. Required by client application to open connection to Jellyfish. """ @@ -127,7 +127,8 @@ defmodule Jellyfish.Room do @doc """ Add a peer to the room with `room_id`. """ - @spec add_peer(Client.t(), id(), Peer.type()) :: {:ok, Peer.t(), client_token()} | {:error, atom() | String.t()} + @spec add_peer(Client.t(), id(), Peer.type()) :: + {:ok, Peer.t(), client_token()} | {:error, atom() | String.t()} def add_peer(client, room_id, type) do with {:ok, %Env{status: 201, body: body}} <- Tesla.post( From 8dea1d5f05bf2aea2a75253e51b7029a86057f47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wala?= Date: Wed, 12 Apr 2023 17:50:30 +0200 Subject: [PATCH 3/7] Stylistic improvement --- lib/jellyfish/client.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/jellyfish/client.ex b/lib/jellyfish/client.ex index 1f4866e..630cb2b 100644 --- a/lib/jellyfish/client.ex +++ b/lib/jellyfish/client.ex @@ -30,10 +30,11 @@ defmodule Jellyfish.Client do Creates new instance of `t:Jellyfish.SDK.Client.t/0`. ``` - #config.exs + # in config.exs config :jellyfish_server_sdk, token: "your-jellyfish-token" client = Jellyfish.Client.new("http://address-of-your-server.com") + # or pass the token explicitly client = Jellyfish.Client.new("http://address-of-your-server.com", "your-jellyfish-token") ``` From 9b90f4454502e79457248cdf3c5c3a83d59a3119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wala?= Date: Thu, 13 Apr 2023 09:54:42 +0200 Subject: [PATCH 4/7] Refactor according to @mickel8 comment --- lib/jellyfish/client.ex | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/jellyfish/client.ex b/lib/jellyfish/client.ex index 630cb2b..b3b0210 100644 --- a/lib/jellyfish/client.ex +++ b/lib/jellyfish/client.ex @@ -27,17 +27,7 @@ defmodule Jellyfish.Client do } @doc """ - Creates new instance of `t:Jellyfish.SDK.Client.t/0`. - - ``` - # in config.exs - config :jellyfish_server_sdk, token: "your-jellyfish-token" - - client = Jellyfish.Client.new("http://address-of-your-server.com") - - # or pass the token explicitly - client = Jellyfish.Client.new("http://address-of-your-server.com", "your-jellyfish-token") - ``` + Creates new instance of `t:Jellyfish.Client.t/0`. ## Parameters @@ -46,16 +36,28 @@ defmodule Jellyfish.Client do token as the one configured in Jellyfish. If not passed, value set via `config :jellyfish_server_sdk, token: "your-token"` in `config.exs` is used. """ - @spec new(String.t(), String.t() | nil) :: t() - def new(address, token \\ nil) + @spec new(String.t(), String.t()) :: t() + def new(address, token), do: build_client(address, token) + + @doc """ + Creates new instance of `t:Jellyfish.Client.t/0`. - def new(address, nil) do + Uses token set in `config.exs`. To explicitly pass the token, see `new/2`. + ``` + # in config.exs + config :jellyfish_server_sdk, token: "your-jellyfish-token" + + client = Jellyfish.Client.new("http://address-of-your-server.com") + ``` + + See `new/2` for description of parameters. + """ + @spec new(String.t()) :: t() + def new(address) do token = Application.fetch_env!(:jellyfish_server_sdk, :token) build_client(address, token) end - def new(address, token), do: build_client(address, token) - defp build_client(address, token) do middleware = [ {Tesla.Middleware.BaseUrl, address}, From ff056f3a9883786e5b1bd501d6889b0caacecf44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wala?= Date: Thu, 13 Apr 2023 09:58:04 +0200 Subject: [PATCH 5/7] Update doc --- lib/jellyfish/client.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/jellyfish/client.ex b/lib/jellyfish/client.ex index b3b0210..71ea351 100644 --- a/lib/jellyfish/client.ex +++ b/lib/jellyfish/client.ex @@ -33,8 +33,7 @@ defmodule Jellyfish.Client do * `address` - url or IP address of the Jellyfish server instance * `token` - token used for authorizing HTTP requests. It's the same - token as the one configured in Jellyfish. If not passed, value set via - `config :jellyfish_server_sdk, token: "your-token"` in `config.exs` is used. + token as the one configured in Jellyfish. """ @spec new(String.t(), String.t()) :: t() def new(address, token), do: build_client(address, token) From 12f4426ffc2199be0918ba7cdf31736a89fa4e78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wala?= Date: Thu, 13 Apr 2023 11:18:11 +0200 Subject: [PATCH 6/7] Change client_token to peer_token in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 09dc209..e4bd1cf 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ room_id # => "8878cd13-99a6-40d6-8d7e-8da23d803dab" # Add peer -{:ok, %Jellyfish.Peer{id: peer_id}, client_token} = Jellyfish.Room.add_peer(client, room_id, "webrtc") +{:ok, %Jellyfish.Peer{id: peer_id}, peer_token} = Jellyfish.Room.add_peer(client, room_id, "webrtc") # Delete peer :ok = Jellyfish.Room.delete_peer(client, room_id, peer_id) From 2b45f6523e825720f1aae7c71f887c1af84f284a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wala?= Date: Thu, 13 Apr 2023 11:22:12 +0200 Subject: [PATCH 7/7] Rename client_token type to peer_token --- lib/jellyfish/room.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jellyfish/room.ex b/lib/jellyfish/room.ex index 47ad59c..f44137e 100644 --- a/lib/jellyfish/room.ex +++ b/lib/jellyfish/room.ex @@ -43,7 +43,7 @@ defmodule Jellyfish.Room do @typedoc """ Client token, created by Jellyfish. Required by client application to open connection to Jellyfish. """ - @type client_token :: String.t() + @type peer_token :: String.t() @typedoc """ Type describing room options. @@ -128,7 +128,7 @@ defmodule Jellyfish.Room do Add a peer to the room with `room_id`. """ @spec add_peer(Client.t(), id(), Peer.type()) :: - {:ok, Peer.t(), client_token()} | {:error, atom() | String.t()} + {:ok, Peer.t(), peer_token()} | {:error, atom() | String.t()} def add_peer(client, room_id, type) do with {:ok, %Env{status: 201, body: body}} <- Tesla.post(