Skip to content

Commit cc05e61

Browse files
committed
query acme
1 parent d52f55d commit cc05e61

4 files changed

Lines changed: 14 additions & 105 deletions

File tree

lib/peek_app_sdk.ex

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,11 @@ defmodule PeekAppSDK do
8686
defdelegate query_peek_pro(install_id, gql_query, gql_variables \\ %{}, config_id \\ nil),
8787
to: PeekAppSDK.Client
8888

89-
defdelegate query_platform(install_id, method, url, body),
90-
to: PeekAppSDK.Client
91-
9289
@doc """
9390
Queries a platform API and returns the raw response body.
94-
Unlike `query_platform/4`, this does not expect a `{data: ...}` wrapper around the response.
9591
"""
96-
@spec query_platform_raw(String.t(), atom(), String.t(), map()) ::
92+
@spec query_platform(String.t(), atom(), String.t(), map()) ::
9793
{:ok, map()} | {:error, list()} | {:error, {integer(), any()}}
98-
defdelegate query_platform_raw(install_id, method, url, body),
94+
defdelegate query_platform(install_id, method, url, body),
9995
to: PeekAppSDK.Client
10096
end

lib/peek_app_sdk/client.ex

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,35 +77,12 @@ defmodule PeekAppSDK.Client do
7777
end
7878
end
7979

80-
def query_platform(install_id, method, url, body_params) do
81-
config = Config.get_config(nil)
82-
peek_api_key = config.peek_api_key
83-
84-
case Tesla.request(client(),
85-
method: method,
86-
url: url,
87-
body: body_params,
88-
headers: headers(install_id, nil, peek_api_key)
89-
) do
90-
{:ok, %Tesla.Env{status: 200, body: %{errors: [_error | _rest] = errors}}} ->
91-
{:error, errors}
92-
93-
{:ok, %Tesla.Env{status: 200, body: %{data: data}}} ->
94-
{:ok, data}
95-
96-
{:ok, %Tesla.Env{status: status, body: body}} ->
97-
Logger.error("Unexpected Platform response when hitting #{url} - (#{status}): #{inspect(body)}")
98-
{:error, status}
99-
end
100-
end
101-
10280
@doc """
10381
Queries a platform API and returns the raw response body.
104-
Unlike `query_platform/4`, this does not expect a `{data: ...}` wrapper around the response.
10582
"""
106-
@spec query_platform_raw(String.t(), atom(), String.t(), map()) ::
83+
@spec query_platform(String.t(), atom(), String.t(), map()) ::
10784
{:ok, map()} | {:error, list()} | {:error, {integer(), any()}}
108-
def query_platform_raw(install_id, method, url, body_params) do
85+
def query_platform(install_id, method, url, body_params) do
10986
config = Config.get_config(nil)
11087
peek_api_key = config.peek_api_key
11188

test/peek_app_sdk/client_test.exs

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -225,89 +225,26 @@ defmodule PeekAppSDK.ClientTest do
225225
end
226226

227227
describe "query_platform/4" do
228-
test "successfully queries platform with GET method using full URL" do
228+
test "successfully returns raw response body for 200 status" do
229229
install_id = "test_install_id"
230230
url = "https://api.example.com/some/endpoint"
231231
body_params = %{}
232-
response_data = %{result: "success"}
232+
response_body = %{result: "success", other_field: "value"}
233233

234234
Tesla.Adapter.Finch
235235
|> Mimic.stub(:call, fn env, _opts ->
236236
assert env.method == :get
237-
assert env.url == "https://api.example.com/some/endpoint"
237+
assert env.url == url
238238
assert Jason.decode!(env.body) == body_params
239239

240240
assert Enum.any?(env.headers, fn {k, v} ->
241241
k == "X-Peek-Auth" && String.starts_with?(v, "Bearer ")
242242
end)
243243

244-
{:ok, %Tesla.Env{status: 200, body: %{data: response_data}}}
245-
end)
246-
247-
assert {:ok, ^response_data} = PeekAppSDK.query_platform(install_id, :get, url, body_params)
248-
end
249-
250-
test "successfully queries platform with POST method and body" do
251-
install_id = "test_install_id"
252-
url = "https://api.example.com/api/resource"
253-
body_params = %{"key" => "value"}
254-
response_data = %{created: true}
255-
256-
Tesla.Adapter.Finch
257-
|> Mimic.stub(:call, fn env, _opts ->
258-
assert env.method == :post
259-
assert env.url == "https://api.example.com/api/resource"
260-
assert Jason.decode!(env.body) == body_params
261-
262-
{:ok, %Tesla.Env{status: 200, body: %{data: response_data}}}
263-
end)
264-
265-
assert {:ok, ^response_data} = Client.query_platform(install_id, :post, url, body_params)
266-
end
267-
268-
test "handles error response" do
269-
install_id = "test_install_id"
270-
url = "https://api.example.com/api/resource"
271-
272-
Tesla.Adapter.Finch
273-
|> Mimic.stub(:call, fn _env, _opts ->
274-
{:ok, %Tesla.Env{status: 404, body: %{error: "Not found"}}}
275-
end)
276-
277-
assert {:error, 404} = Client.query_platform(install_id, :get, url, %{})
278-
end
279-
280-
test "bubbles up errors when status is 200 but errors key is present" do
281-
install_id = "test_install_id"
282-
url = "https://api.example.com/api/resource"
283-
284-
errors = [%{message: "Validation failed"}]
285-
286-
Tesla.Adapter.Finch
287-
|> Mimic.stub(:call, fn _env, _opts ->
288-
{:ok, %Tesla.Env{status: 200, body: %{errors: errors}}}
289-
end)
290-
291-
assert {:error, ^errors} = Client.query_platform(install_id, :post, url, %{})
292-
end
293-
end
294-
295-
describe "query_platform_raw/4" do
296-
test "successfully returns raw response body for 200 status" do
297-
install_id = "test_install_id"
298-
url = "https://api.example.com/some/endpoint"
299-
body_params = %{}
300-
response_body = %{result: "success", other_field: "value"}
301-
302-
Tesla.Adapter.Finch
303-
|> Mimic.stub(:call, fn env, _opts ->
304-
assert env.method == :get
305-
assert env.url == url
306-
307244
{:ok, %Tesla.Env{status: 200, body: response_body}}
308245
end)
309246

310-
assert {:ok, ^response_body} = Client.query_platform_raw(install_id, :get, url, body_params)
247+
assert {:ok, ^response_body} = PeekAppSDK.query_platform(install_id, :get, url, body_params)
311248
end
312249

313250
test "successfully returns raw response body for 201 status" do
@@ -320,11 +257,12 @@ defmodule PeekAppSDK.ClientTest do
320257
|> Mimic.stub(:call, fn env, _opts ->
321258
assert env.method == :post
322259
assert env.url == url
260+
assert Jason.decode!(env.body) == body_params
323261

324262
{:ok, %Tesla.Env{status: 201, body: response_body}}
325263
end)
326264

327-
assert {:ok, ^response_body} = Client.query_platform_raw(install_id, :post, url, body_params)
265+
assert {:ok, ^response_body} = Client.query_platform(install_id, :post, url, body_params)
328266
end
329267

330268
test "handles error response with status and body tuple" do
@@ -337,7 +275,7 @@ defmodule PeekAppSDK.ClientTest do
337275
{:ok, %Tesla.Env{status: 404, body: error_body}}
338276
end)
339277

340-
assert {:error, {404, ^error_body}} = Client.query_platform_raw(install_id, :get, url, %{})
278+
assert {:error, {404, ^error_body}} = Client.query_platform(install_id, :get, url, %{})
341279
end
342280

343281
test "bubbles up errors when status is 200 but errors key is present" do
@@ -351,7 +289,7 @@ defmodule PeekAppSDK.ClientTest do
351289
{:ok, %Tesla.Env{status: 200, body: %{errors: errors}}}
352290
end)
353291

354-
assert {:error, ^errors} = Client.query_platform_raw(install_id, :post, url, %{})
292+
assert {:error, ^errors} = Client.query_platform(install_id, :post, url, %{})
355293
end
356294
end
357295

test/peek_app_sdk/ui/odyssey_test.exs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ defmodule PeekAppSDK.UI.OdysseyTest do
168168

169169
assert html =~ "<fieldset"
170170
assert html =~ "class=\"fieldset mb-2\""
171-
assert html =~ "<label>"
172-
assert html =~ ~r/<span class="label mb-1">.*Time Unit.*<\/span>/s
171+
assert html =~ ~r/<span class="label mb-1 block">.*Time Unit.*<\/span>/s
173172
assert html =~ "class=\"inline-flex rounded-lg\""
174173
end
175174

@@ -189,8 +188,7 @@ defmodule PeekAppSDK.UI.OdysseyTest do
189188

190189
assert html =~ "<fieldset"
191190
assert html =~ "class=\"fieldset mb-2\""
192-
assert html =~ "<label>"
193-
assert html =~ ~r/<span class="label mb-1">.*Communication Channel.*<\/span>/s
191+
assert html =~ ~r/<span class="label mb-1 block">.*Communication Channel.*<\/span>/s
194192
assert html =~ "Email"
195193
assert html =~ "Text Message"
196194
# Should have hidden input for form integration

0 commit comments

Comments
 (0)