Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use retry because opentok sometimes fails #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[
import_deps: [:tesla],
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
4 changes: 4 additions & 0 deletions lib/ex_opentok.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ defmodule ExOpentok do
ExOpentok.Session.init()
end

def init! do
ExOpentok.Session.init!()
end

@doc false
def version, do: @version

Expand Down
28 changes: 28 additions & 0 deletions lib/ex_opentok/archive.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ defmodule ExOpentok.Archive do
|> Client.handle_response()
end

def stop!(archive_id) do
case stop(archive_id) do
{:ok, response} -> response
{:error, error} -> raise error
end
end

@doc """
Deleting an archive

Expand All @@ -96,6 +103,13 @@ defmodule ExOpentok.Archive do
|> Client.handle_response()
end

def delete!(archive_id) do
case delete(archive_id) do
{:ok, response} -> response
{:error, error} -> raise error
end
end

@doc """
Listing archives

Expand Down Expand Up @@ -133,9 +147,23 @@ defmodule ExOpentok.Archive do
|> Client.handle_response()
end

def list!(opts \\ %{offset: 0, count: 1000}) do
case list(opts) do
{:ok, response} -> response
{:error, error} -> raise error
end
end

def find(archive_id) do
(ExOpentok.api_url() <> "#{ExOpentok.config(:key)}/archive/#{archive_id}")
|> Client.http_request()
|> Client.handle_response()
end

def find!(archive_id) do
case find(archive_id) do
{:ok, response} -> response
{:error, error} -> raise error
end
end
end
33 changes: 25 additions & 8 deletions lib/ex_opentok/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ defmodule ExOpentok.Client do
alias ExOpentok.Token
alias ExOpentok.Exception

plug(Tesla.Middleware.Timeout, timeout: 2_000)
plug Tesla.Middleware.Timeout, timeout: 2_000

plug Tesla.Middleware.Retry,
delay: 100,
max_retries: 10,
max_delay: 4_000,
should_retry: fn
{:ok, %{status: status}} when status >= 500 -> true
{:ok, _} -> false
{:error, _} -> true
end

@moduledoc """
Wrapper for HTTPotion
Wrapper for Tesla
"""

@doc """
HTTP Client's request with HTTPotion.
HTTP Client's request with Tesla.
"""
def http_request(url, type \\ :get, body \\ nil) do
do_http_request(url, type, body)
Expand Down Expand Up @@ -62,19 +72,26 @@ defmodule ExOpentok.Client do
def handle_response(response) do
case response do
{:ok, %{status: 200, body: ""}} ->
%{}
{:ok, %{}}

{:ok, %{status: 200, body: body}} ->
body |> Poison.decode!() |> handle_data_struct()
{:ok, body |> Poison.decode!() |> handle_data_struct()}

{:ok, %{status: 405, body: body}} ->
raise "405 Method not allowed"
{:ok, "405 Method not allowed"}

{:ok, response} ->
raise "Error #{response.status} -> ExOpentok query:\n #{inspect(response)}"
{:error, "Error #{response.status} -> ExOpentok query:\n #{inspect(response)}"}

{:error, error} ->
raise "Error #{inspect(error)} -> ExOpentok query:\n #{inspect(response)}"
{:error, "Error #{inspect(error)} -> ExOpentok query:\n #{inspect(response)}"}
end
end

def handle_response!(response) do
case handle_response(response) do
{:ok, output} -> output
{:error, error} -> raise error
end
end

Expand Down
54 changes: 46 additions & 8 deletions lib/ex_opentok/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ defmodule ExOpentok.Session do
|> Client.handle_response()
end

def create!(opts) do
case create(opts) do
{:ok, response} -> response
{:error, error} -> raise error
end
end

# https://tokbox.com/developer/rest/#force_mute_stream
@spec mute(String.t(), String.t()) :: %{}
def mute(session_id, stream_id) do
Expand All @@ -42,6 +49,13 @@ defmodule ExOpentok.Session do
|> Client.handle_response()
end

def mute!(session_id, stream_id) do
case mute(session_id, stream_id) do
{:ok, response} -> response
{:error, error} -> raise error
end
end

# https://tokbox.com/developer/rest/#force_mute_session
@spec mute_all(String.t(), [String.t()]) :: %{}
def mute_all(session_id, excluded_stream_ids \\ []) do
Expand All @@ -55,23 +69,40 @@ defmodule ExOpentok.Session do
|> ExOpentok.Client.handle_response()
end

def mute_all!(session_id, excluded_stream_ids) do
case mute_all(session_id, excluded_stream_ids) do
{:ok, response} -> response
{:error, error} -> raise error
end
end

@spec base_session_url(String.t(), String.t()) :: String.t()
defp base_session_url(session_id, path \\ "") do
"https://api.opentok.com/v2/project/#{ExOpentok.config(:key)}/session/#{session_id}#{path}"
end

defp format_response(session) do
Map.merge(
session,
%{
api_key: ExOpentok.config(:key),
token: Token.generate(session["session_id"])
}
)
defp format_response({:ok, session}) do
{:ok,
Map.merge(
session,
%{
api_key: ExOpentok.config(:key),
token: Token.generate(session["session_id"])
}
)}
end

defp format_response(error), do: error

def init, do: create() |> format_response()

def init! do
case init() do
{:ok, response} -> response
{:error, error} -> raise error
end
end

@doc """
Handle location
"""
Expand Down Expand Up @@ -120,4 +151,11 @@ defmodule ExOpentok.Session do
|> Client.http_request()
|> Client.handle_response()
end

def delete!(session_id, connection_id) do
case delete(session_id, connection_id) do
{:ok, response} -> response
{:error, error} -> raise error
end
end
end
16 changes: 8 additions & 8 deletions test/ex_opentok_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defmodule ExOpentokTest do
alias ExOpentok.{Archive, Client, Exception, Session, Token}

@api_key ExOpentok.config(:key)
@session ExOpentok.init()
@list ExOpentok.Archive.list()
@session ExOpentok.init!()
@list ExOpentok.Archive.list!()

# ARCHIVE

Expand All @@ -21,7 +21,7 @@ defmodule ExOpentokTest do
first_item = @list["items"] |> Enum.at(0)

first_item["id"]
|> Archive.find()
|> Archive.find!()
|> Helper.same_map?(Helper.archive_keys())
end

Expand All @@ -31,7 +31,7 @@ defmodule ExOpentokTest do

# Client.http_request()
test "should get 200 when request get" do
response =
{:ok, response} =
Client.http_request(
"https://api.opentok.com/v2/project/#{@api_key}/archive?offset=0&count=1000",
:get
Expand All @@ -40,16 +40,16 @@ defmodule ExOpentokTest do
assert response.status == 200
end

# Client.handle_response()
# Client.handle_response!()
test "should handle response 200" do
response = Mock.http_response_archive_list()
assert Client.handle_response(response) == Mock.archive_list()
assert Client.handle_response!(response) == Mock.archive_list()
end

# Client.handle_response()
# Client.handle_response!()
test "should raise if no response 200" do
assert_raise RuntimeError, fn ->
Mock.http_response_error() |> Client.handle_response()
Mock.http_response_error() |> Client.handle_response!()
end
end

Expand Down
101 changes: 52 additions & 49 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,61 +34,64 @@ end

defmodule ExOpentokTest.Mock do
def session do
%{
:api_key => "01234567",
:token =>
"T1==cGFydG5lcl9pZD00NTgxMTExMiZzaWc9OUVBOTIyQjlFQzM0NUIxNkI3NcGFydG5lcl9pZD00NTgxMTExMiZzaWc9OUVBOTIyQjlFQzM0NUIxNkI3NcGFydG5lcl9pZD00NTgxMTExMiZzaWc9OUVBOTIyQjlFQzM0NUIxNkI3NcGFydG5lcl9pZD00NTgxMTExMiZzaWc9OUVBOTIyQjlFQzM0NUIxNkI3N==",
"create_dt" => "Tue Apr 11 08:56:40 PDT 2017",
"ice_credential_expiration" => 86100,
"ice_server" => nil,
"ice_servers" => nil,
"media_server_hostname" => nil,
"media_server_url" => "",
"messaging_server_url" => nil,
"messaging_url" => nil,
"partner_id" => "01234567",
"project_id" => "01234567",
"properties" => nil,
"session_id" => "1_MX40MX40NTgxMMX40NTgxMMX40NTgxMMX40NTgxMMX40NTgxMMX40NTg",
"session_status" => nil,
"status_invalid" => nil,
"symphony_address" => nil
}
{:ok,
%{
:api_key => "01234567",
:token =>
"T1==cGFydG5lcl9pZD00NTgxMTExMiZzaWc9OUVBOTIyQjlFQzM0NUIxNkI3NcGFydG5lcl9pZD00NTgxMTExMiZzaWc9OUVBOTIyQjlFQzM0NUIxNkI3NcGFydG5lcl9pZD00NTgxMTExMiZzaWc9OUVBOTIyQjlFQzM0NUIxNkI3NcGFydG5lcl9pZD00NTgxMTExMiZzaWc9OUVBOTIyQjlFQzM0NUIxNkI3N==",
"create_dt" => "Tue Apr 11 08:56:40 PDT 2017",
"ice_credential_expiration" => 86100,
"ice_server" => nil,
"ice_servers" => nil,
"media_server_hostname" => nil,
"media_server_url" => "",
"messaging_server_url" => nil,
"messaging_url" => nil,
"partner_id" => "01234567",
"project_id" => "01234567",
"properties" => nil,
"session_id" => "1_MX40MX40NTgxMMX40NTgxMMX40NTgxMMX40NTgxMMX40NTgxMMX40NTg",
"session_status" => nil,
"status_invalid" => nil,
"symphony_address" => nil
}}
end

def http_response_archive_list do
%{
body:
"{\"count\":1,\"items\":[{\"id\":\"5c48fb13-f27a-465d-94a7-e581d8ed49f9\",\"status\":\"expired\",\"name\":\"Ruby Archiving Sample App\",\"reason\":\"user initiated\",\"sessionId\":\"1_MX40NTgxMTExMn5-MTQ5MTY4MTU4NzU4Nn5VVU8yY1FsdThVVUU5UUVzd1VkTEh1SDJ-fg\",\"projectId\":45811112,\"createdAt\":1491682306000,\"size\":1742751,\"duration\":21,\"outputMode\":\"composed\",\"hasAudio\":true,\"hasVideo\":true,\"sha256sum\":\"97+WlyjwtOvrrNr2zG8NBTvZgqpSq5PGdgonBOTK7Fw=\",\"password\":\"\",\"updatedAt\":1491943883000,\"url\":null,\"partnerId\":45811112}]}",
headers: %{
hdrs: %{
"connection" => "keep-alive",
"content-type" => "application/json",
"date" => "Wed, 12 Apr 2017 08:25:59 GMT",
"server" => "nginx",
"strict-transport-security" => "max-age=31536000; includeSubdomains",
"transfer-encoding" => "chunked"
}
},
status: 200
}
{:ok,
%{
body:
"{\"count\":1,\"items\":[{\"id\":\"5c48fb13-f27a-465d-94a7-e581d8ed49f9\",\"status\":\"expired\",\"name\":\"Ruby Archiving Sample App\",\"reason\":\"user initiated\",\"sessionId\":\"1_MX40NTgxMTExMn5-MTQ5MTY4MTU4NzU4Nn5VVU8yY1FsdThVVUU5UUVzd1VkTEh1SDJ-fg\",\"projectId\":45811112,\"createdAt\":1491682306000,\"size\":1742751,\"duration\":21,\"outputMode\":\"composed\",\"hasAudio\":true,\"hasVideo\":true,\"sha256sum\":\"97+WlyjwtOvrrNr2zG8NBTvZgqpSq5PGdgonBOTK7Fw=\",\"password\":\"\",\"updatedAt\":1491943883000,\"url\":null,\"partnerId\":45811112}]}",
headers: %{
hdrs: %{
"connection" => "keep-alive",
"content-type" => "application/json",
"date" => "Wed, 12 Apr 2017 08:25:59 GMT",
"server" => "nginx",
"strict-transport-security" => "max-age=31536000; includeSubdomains",
"transfer-encoding" => "chunked"
}
},
status: 200
}}
end

def http_response_error do
%{
body: "{}",
headers: %{
hdrs: %{
"connection" => "keep-alive",
"content-type" => "application/json",
"date" => "Wed, 12 Apr 2017 08:25:59 GMT",
"server" => "nginx",
"strict-transport-security" => "max-age=31536000; includeSubdomains",
"transfer-encoding" => "chunked"
}
},
status: 400
}
{:ok,
%{
body: "{}",
headers: %{
hdrs: %{
"connection" => "keep-alive",
"content-type" => "application/json",
"date" => "Wed, 12 Apr 2017 08:25:59 GMT",
"server" => "nginx",
"strict-transport-security" => "max-age=31536000; includeSubdomains",
"transfer-encoding" => "chunked"
}
},
status: 400
}}
end

def archive_list do
Expand Down