Skip to content

Commit

Permalink
Merge pull request #7 from aarongraham/ag/add-basic-support-for-insig…
Browse files Browse the repository at this point in the history
…hts-advanced

add basic support for insights advanced
  • Loading branch information
brentjr authored Dec 12, 2023
2 parents 5e42aad + d2d9e68 commit c09cb58
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 39 deletions.
4 changes: 4 additions & 0 deletions lib/ex_twilio/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ defmodule ExTwilio.Config do
"""
def base_url, do: "#{protocol()}://#{api_domain()}/#{api_version()}"

def events_url, do: "https://events.twilio.com/v1"

def fax_url, do: "https://fax.twilio.com/v1"

def task_router_url, do: "https://taskrouter.twilio.com/v1"
Expand All @@ -69,6 +71,8 @@ defmodule ExTwilio.Config do

def video_url, do: "https://video.twilio.com/v1"

def voice_insights_url, do: "https://insights.twilio.com/v1/Voice"

def preview_url, do: "https://preview.twilio.com/marketplace"

@doc """
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_twilio/request_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule ExTwilio.RequestValidator do

defp compute_hmac(data, key), do: hmac(:sha, key, data)

# TODO: remove when we require OTP 22
# remove when we require OTP 22
if System.otp_release() >= "22" do
defp hmac(digest, key, data), do: :crypto.mac(:hmac, digest, key, data)
else
Expand Down
23 changes: 23 additions & 0 deletions lib/ex_twilio/resources/events/sinks.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule ExTwilio.Events.Sinks do
@moduledoc """
Represents Event Streams Sink resource.
Sinks are the destinations to which events selected in a subscription will be delivered
- [Twilio docs](https://www.twilio.com/docs/events/event-streams/sink-resource)
"""

defstruct [
:date_created,
:date_updated,
:description,
:sid,
:sink_configuration,
:sink_type,
:status,
:url,
:links
]

use ExTwilio.Resource, import: [:create]
end
22 changes: 22 additions & 0 deletions lib/ex_twilio/resources/events/subscriptions.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule ExTwilio.Events.Subscriptions do
@moduledoc """
Represents Event Streams Subscriptions resource.
You can use the Subscriptions API to subscribe to specific Twilio events and versions, and manage your subscriptions.
- [Twilio docs](https://www.twilio.com/docs/events/event-streams/subscription)
"""

defstruct [
:account_sid,
:sid,
:date_created,
:date_updated,
:description,
:sink_sid,
:url,
:links
]

use ExTwilio.Resource, import: [:create]
end
17 changes: 17 additions & 0 deletions lib/ex_twilio/resources/insights/voice/settings.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule ExTwilio.Insights.Voice.Settings do
@moduledoc """
Represents Voice Insights Settings Resource.
- [Twilio docs](https://www.twilio.com/docs/voice/voice-insights/api/call/voice-insights-settings-resource)
"""

defstruct [
:account_sid,
:advanced_features,
:voice_trace,
:url,
:subaccount_sid
]

use ExTwilio.Resource, import: [:find, :create]
end
95 changes: 57 additions & 38 deletions lib/ex_twilio/url_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,44 +41,8 @@ defmodule ExTwilio.UrlGenerator do
@spec build_url(atom, String.t() | nil, list) :: String.t()
def build_url(module, id \\ nil, options \\ []) do
{url, options} =
case Module.split(module) do
["ExTwilio", "TaskRouter" | _] ->
options = add_workspace_to_options(module, options)
url = add_segments(Config.task_router_url(), module, id, options)
{url, options}

["ExTwilio", "ProgrammableChat" | _] ->
url = add_segments(Config.programmable_chat_url(), module, id, options)
{url, options}

["ExTwilio", "Notify" | _] ->
url = add_segments(Config.notify_url(), module, id, options)
{url, options}

["ExTwilio", "Fax" | _] ->
url = add_segments(Config.fax_url(), module, id, options)
{url, options}

["ExTwilio", "Studio" | _] ->
options = add_flow_to_options(module, options)
url = add_segments(Config.studio_url(), module, id, options)
{url, options}

["ExTwilio", "Video" | _] ->
url = add_segments(Config.video_url(), module, id, options)
{url, options}

["ExTwilio", "InstalledAddOn" | _] ->
options = add_installed_add_on_to_options(module, options)
url = add_segments(Config.preview_url(), module, id, options)
{url, options}

_ ->
# Add Account SID segment if not already present
options = add_account_to_options(module, options)
url = add_segments(Config.base_url(), module, id, options) <> ".json"
{url, options}
end
Module.split(module)
|> construct_url(module, id, options)

# Append querystring
if Keyword.has_key?(options, :query) do
Expand All @@ -88,6 +52,61 @@ defmodule ExTwilio.UrlGenerator do
end
end

defp construct_url(["ExTwilio", "TaskRouter" | _], module, id, options) do
options = add_workspace_to_options(module, options)
url = add_segments(Config.task_router_url(), module, id, options)
{url, options}
end

defp construct_url(["ExTwilio", "ProgrammableChat" | _], module, id, options) do
url = add_segments(Config.programmable_chat_url(), module, id, options)
{url, options}
end

defp construct_url(["ExTwilio", "Notify" | _], module, id, options) do
url = add_segments(Config.notify_url(), module, id, options)
{url, options}
end

defp construct_url(["ExTwilio", "Events" | _], module, id, options) do
url = add_segments(Config.events_url(), module, id, options)
{url, options}
end

defp construct_url(["ExTwilio", "Fax" | _], module, id, options) do
url = add_segments(Config.fax_url(), module, id, options)
{url, options}
end

defp construct_url(["ExTwilio", "Studio" | _], module, id, options) do
options = add_flow_to_options(module, options)
url = add_segments(Config.studio_url(), module, id, options)
{url, options}
end

defp construct_url(["ExTwilio", "Video" | _], module, id, options) do
url = add_segments(Config.video_url(), module, id, options)
{url, options}
end

defp construct_url(["ExTwilio", "Insights", "Voice" | _], module, id, options) do
url = add_segments(Config.voice_insights_url(), module, id, options)
{url, options}
end

defp construct_url(["ExTwilio", "InstalledAddOn" | _], module, id, options) do
options = add_installed_add_on_to_options(module, options)
url = add_segments(Config.preview_url(), module, id, options)
{url, options}
end

defp construct_url(_, module, id, options) do
# Add Account SID segment if not already present
options = add_account_to_options(module, options)
url = add_segments(Config.base_url(), module, id, options) <> ".json"
{url, options}
end

defp add_segments(url, module, id, options) do
# Append parents
url = url <> build_segments(:parent, normalize_parents(module.parents), options)
Expand Down

0 comments on commit c09cb58

Please sign in to comment.