Skip to content

Commit bd6af9b

Browse files
committed
fix tesla warning
1 parent 87c013f commit bd6af9b

6 files changed

Lines changed: 150 additions & 18 deletions

File tree

coveralls.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
"test/*",
55
"lib/peek_app_sdk/application.ex",
66
"lib/peek_app_sdk/ui/core_components.ex",
7-
"lib/peek_app_sdk/ui/get_text.ex",
8-
"lib/peek_app_sdk/metrics.ex"
7+
"lib/peek_app_sdk/ui/get_text.ex"
98
],
109
"print_summary": true
11-
}
10+
}

lib/peek_app_sdk/client.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
defmodule PeekAppSDK.Client do
22
require Logger
33

4-
use Tesla
5-
64
alias PeekAppSDK.Config
75

8-
plug Tesla.Middleware.JSON, engine_opts: [keys: &String.to_atom/1]
6+
@doc """
7+
Creates a Tesla client with the appropriate middleware configuration.
8+
"""
9+
def client do
10+
middleware = [
11+
{Tesla.Middleware.JSON, engine_opts: [keys: &String.to_atom/1]}
12+
]
13+
14+
Tesla.client(middleware)
15+
end
916

1017
@doc """
1118
Queries the Peek Pro API.
@@ -37,7 +44,7 @@ defmodule PeekAppSDK.Client do
3744
operation_name = operation_name(gql_query)
3845
url = "#{config.peek_api_url |> String.trim()}/#{peek_app_id}/#{operation_name}"
3946

40-
case request(
47+
case Tesla.request(client(),
4148
method: :post,
4249
url: url,
4350
body: body_params,

lib/peek_app_sdk/metrics/client.ex

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
defmodule PeekAppSDK.Metrics.Client do
2-
use Tesla
3-
42
@moduledoc """
53
Client for sending metrics to the Peek Pro metrics service.
64
"""
75

8-
plug Tesla.Middleware.JSON
9-
plug Tesla.Middleware.Headers, [{"Content-Type", "application/json"}]
10-
plug Tesla.Middleware.Retry, delay: 500, max_retries: 10
6+
@doc """
7+
Creates a Tesla client with the appropriate middleware configuration.
8+
"""
9+
def client do
10+
middleware = [
11+
Tesla.Middleware.JSON,
12+
{Tesla.Middleware.Headers, [{"Content-Type", "application/json"}]},
13+
{Tesla.Middleware.Retry, delay: 500, max_retries: 10}
14+
]
15+
16+
Tesla.client(middleware)
17+
end
1118

1219
@doc """
1320
Tracks an event with the given event ID and payload.
@@ -119,7 +126,7 @@ defmodule PeekAppSDK.Metrics.Client do
119126
do: "https://ahem.peeklabs.com/events/#{Application.fetch_env!(:peek_app_sdk, :peek_app_id)}"
120127

121128
defp do_post!(body, _opts \\ []) do
122-
response = post!(event_url(), body)
129+
response = Tesla.post!(client(), event_url(), body)
123130

124131
case response do
125132
%Tesla.Env{status: 202} ->

lib/peek_app_sdk/ui/core_components.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ defmodule PeekAppSDK.UI.CoreComponents do
1717
use Phoenix.Component
1818
use Gettext, backend: PeekAppSDK.UI.Gettext
1919

20+
alias Phoenix.HTML.Form
2021
alias Phoenix.LiveView.JS
2122

2223
@doc """
@@ -347,7 +348,7 @@ defmodule PeekAppSDK.UI.CoreComponents do
347348
def input(%{type: "checkbox"} = assigns) do
348349
assigns =
349350
assign_new(assigns, :checked, fn ->
350-
Phoenix.HTML.Form.normalize_value("checkbox", assigns[:value])
351+
Form.normalize_value("checkbox", assigns[:value])
351352
end)
352353

353354
~H"""

mix.lock

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
"cowlib": {:hex, :cowlib, "2.15.0", "3c97a318a933962d1c12b96ab7c1d728267d2c523c25a5b57b0f93392b6e9e25", [:make, :rebar3], [], "hexpm", "4f00c879a64b4fe7c8fcb42a4281925e9ffdb928820b03c3ad325a617e857532"},
77
"credo": {:hex, :credo, "1.7.12", "9e3c20463de4b5f3f23721527fcaf16722ec815e70ff6c60b86412c695d426c1", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "8493d45c656c5427d9c729235b99d498bd133421f3e0a683e5c1b561471291e5"},
88
"excoveralls": {:hex, :excoveralls, "0.18.5", "e229d0a65982613332ec30f07940038fe451a2e5b29bce2a5022165f0c9b157e", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "523fe8a15603f86d64852aab2abe8ddbd78e68579c8525ae765facc5eae01562"},
9-
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm", "32e95820a97cffea67830e91514a2ad53b888850442d6d395f53a1ac60c82e07"},
109
"expo": {:hex, :expo, "1.1.0", "f7b9ed7fb5745ebe1eeedf3d6f29226c5dd52897ac67c0f8af62a07e661e5c75", [:mix], [], "hexpm", "fbadf93f4700fb44c331362177bdca9eeb8097e8b0ef525c9cc501cb9917c960"},
11-
"exvcr": {:hex, :exvcr, "0.17.1", "3bae83d698a464a48212ad87c8ea4bcfb6bd76d53b937129472764e557616228", [:mix], [{:exjsx, "~> 4.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:finch, "~> 0.16", [hex: :finch, repo: "hexpm", optional: true]}, {:httpoison, "~> 1.0 or ~> 2.0", [hex: :httpoison, repo: "hexpm", optional: true]}, {:ibrowse, "~> 4.4", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:meck, "~> 1.0", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "a39d86980da183011366a878972e0ed43a5441814b701edd2e11360564e7bcab"},
1210
"file_system": {:hex, :file_system, "1.1.0", "08d232062284546c6c34426997dd7ef6ec9f8bbd090eb91780283c9016840e8f", [:mix], [], "hexpm", "bfcf81244f416871f2a2e15c1b515287faa5db9c6bcf290222206d120b3d43f6"},
1311
"finch": {:hex, :finch, "0.20.0", "5330aefb6b010f424dcbbc4615d914e9e3deae40095e73ab0c1bb0968933cadf", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2658131a74d051aabfcba936093c903b8e89da9a1b63e430bee62045fa9b2ee2"},
1412
"gettext": {:hex, :gettext, "0.26.2", "5978aa7b21fada6deabf1f6341ddba50bc69c999e812211903b169799208f2a8", [:mix], [{:expo, "~> 0.5.1 or ~> 1.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "aa978504bcf76511efdc22d580ba08e2279caab1066b76bb9aa81c4a1e0a32a5"},
@@ -17,8 +15,6 @@
1715
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
1816
"joken": {:hex, :joken, "2.6.2", "5daaf82259ca603af4f0b065475099ada1b2b849ff140ccd37f4b6828ca6892a", [:mix], [{:jose, "~> 1.11.10", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "5134b5b0a6e37494e46dbf9e4dad53808e5e787904b7c73972651b51cce3d72b"},
1917
"jose": {:hex, :jose, "1.11.10", "a903f5227417bd2a08c8a00a0cbcc458118be84480955e8d251297a425723f83", [:mix, :rebar3], [], "hexpm", "0d6cd36ff8ba174db29148fc112b5842186b68a90ce9fc2b3ec3afe76593e614"},
20-
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm", "fc3499fed7a726995aa659143a248534adc754ebd16ccd437cd93b649a95091f"},
21-
"meck": {:hex, :meck, "1.0.0", "24676cb6ee6951530093a93edcd410cfe4cb59fe89444b875d35c9d3909a15d0", [:rebar3], [], "hexpm", "680a9bcfe52764350beb9fb0335fb75fee8e7329821416cee0a19fec35433882"},
2218
"mime": {:hex, :mime, "2.0.7", "b8d739037be7cd402aee1ba0306edfdef982687ee7e9859bee6198c1e7e2f128", [:mix], [], "hexpm", "6171188e399ee16023ffc5b76ce445eb6d9672e2e241d2df6050f3c771e80ccd"},
2319
"mimic": {:hex, :mimic, "1.12.0", "34c9d1fb8e756df09ca5f96861d273f2bb01063df1a6a51a4c101f9ad7f07a9c", [:mix], [{:ham, "~> 0.2", [hex: :ham, repo: "hexpm", optional: false]}], "hexpm", "eaa43d495d6f3bc8099b28886e05a1b09a2a6be083f6385c3abc17599e5e2c43"},
2420
"mint": {:hex, :mint, "1.7.1", "113fdb2b2f3b59e47c7955971854641c61f378549d73e829e1768de90fc1abf1", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "fceba0a4d0f24301ddee3024ae116df1c3f4bb7a563a731f45fdfeb9d39a231b"},

test/peek_app_sdk/metrics_test.exs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
defmodule PeekAppSDK.MetricsTest do
2+
use ExUnit.Case, async: false
3+
4+
alias PeekAppSDK.Metrics
5+
6+
describe "track_install/4" do
7+
test "delegates to PeekAppSDK.Metrics.Client.track_install/4" do
8+
external_refid = "partner-123"
9+
name = "Partner Name"
10+
is_test = false
11+
opts = [post_message: true]
12+
13+
Tesla.Adapter.Finch
14+
|> Mimic.stub(:call, fn env, _opts ->
15+
assert env.method == :post
16+
17+
payload = Jason.decode!(env.body)
18+
assert payload["eventId"] == "app.install"
19+
assert payload["anonymousId"] == external_refid
20+
assert payload["usageDetails"] == name
21+
assert payload["customFields"]["partnerIsTest"] == is_test
22+
23+
{:ok, %Tesla.Env{status: 202}}
24+
end)
25+
26+
assert {:ok, _} = Metrics.track_install(external_refid, name, is_test, opts)
27+
end
28+
29+
test "delegates to PeekAppSDK.Metrics.Client.track_install/3 with default opts" do
30+
external_refid = "partner-456"
31+
name = "Another Partner"
32+
is_test = true
33+
34+
Tesla.Adapter.Finch
35+
|> Mimic.stub(:call, fn env, _opts ->
36+
assert env.method == :post
37+
38+
payload = Jason.decode!(env.body)
39+
assert payload["eventId"] == "app.install"
40+
assert payload["anonymousId"] == external_refid
41+
assert payload["usageDetails"] == name
42+
assert payload["customFields"]["partnerIsTest"] == is_test
43+
44+
{:ok, %Tesla.Env{status: 202}}
45+
end)
46+
47+
assert {:ok, _} = Metrics.track_install(external_refid, name, is_test)
48+
end
49+
end
50+
51+
describe "track_uninstall/4" do
52+
test "delegates to PeekAppSDK.Metrics.Client.track_uninstall/4" do
53+
external_refid = "partner-789"
54+
name = "Test Partner"
55+
is_test = false
56+
opts = [post_message: true]
57+
58+
Tesla.Adapter.Finch
59+
|> Mimic.stub(:call, fn env, _opts ->
60+
assert env.method == :post
61+
62+
payload = Jason.decode!(env.body)
63+
assert payload["eventId"] == "app.uninstall"
64+
assert payload["anonymousId"] == external_refid
65+
assert payload["usageDetails"] == name
66+
assert payload["customFields"]["partnerIsTest"] == is_test
67+
68+
{:ok, %Tesla.Env{status: 202}}
69+
end)
70+
71+
assert {:ok, _} = Metrics.track_uninstall(external_refid, name, is_test, opts)
72+
end
73+
74+
test "delegates to PeekAppSDK.Metrics.Client.track_uninstall/3 with default opts" do
75+
external_refid = "partner-101"
76+
name = "Final Partner"
77+
is_test = true
78+
79+
Tesla.Adapter.Finch
80+
|> Mimic.stub(:call, fn env, _opts ->
81+
assert env.method == :post
82+
83+
payload = Jason.decode!(env.body)
84+
assert payload["eventId"] == "app.uninstall"
85+
assert payload["anonymousId"] == external_refid
86+
assert payload["usageDetails"] == name
87+
assert payload["customFields"]["partnerIsTest"] == is_test
88+
89+
{:ok, %Tesla.Env{status: 202}}
90+
end)
91+
92+
assert {:ok, _} = Metrics.track_uninstall(external_refid, name, is_test)
93+
end
94+
end
95+
96+
describe "track/2" do
97+
test "delegates to PeekAppSDK.Metrics.Client.track/2" do
98+
event_id = "custom.event"
99+
100+
payload = %{
101+
anonymousId: "user-123",
102+
level: "info",
103+
customField: "test value"
104+
}
105+
106+
Tesla.Adapter.Finch
107+
|> Mimic.stub(:call, fn env, _opts ->
108+
assert env.method == :post
109+
110+
sent_payload = Jason.decode!(env.body)
111+
assert sent_payload["eventId"] == event_id
112+
assert sent_payload["anonymousId"] == "user-123"
113+
assert sent_payload["level"] == "info"
114+
assert sent_payload["customField"] == "test value"
115+
116+
{:ok, %Tesla.Env{status: 202}}
117+
end)
118+
119+
assert {:ok, _} = Metrics.track(event_id, payload)
120+
end
121+
end
122+
end

0 commit comments

Comments
 (0)