Skip to content

Commit d52f55d

Browse files
committed
add query_platform_raw for acme
1 parent 7afe202 commit d52f55d

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

lib/peek_app_sdk.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,13 @@ defmodule PeekAppSDK do
8888

8989
defdelegate query_platform(install_id, method, url, body),
9090
to: PeekAppSDK.Client
91+
92+
@doc """
93+
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.
95+
"""
96+
@spec query_platform_raw(String.t(), atom(), String.t(), map()) ::
97+
{:ok, map()} | {:error, list()} | {:error, {integer(), any()}}
98+
defdelegate query_platform_raw(install_id, method, url, body),
99+
to: PeekAppSDK.Client
91100
end

lib/peek_app_sdk/client.ex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,35 @@ defmodule PeekAppSDK.Client do
9999
end
100100
end
101101

102+
@doc """
103+
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.
105+
"""
106+
@spec query_platform_raw(String.t(), atom(), String.t(), map()) ::
107+
{:ok, map()} | {:error, list()} | {:error, {integer(), any()}}
108+
def query_platform_raw(install_id, method, url, body_params) do
109+
config = Config.get_config(nil)
110+
peek_api_key = config.peek_api_key
111+
112+
case Tesla.request(client(),
113+
method: method,
114+
url: url,
115+
body: body_params,
116+
headers: headers(install_id, nil, peek_api_key)
117+
) do
118+
{:ok, %Tesla.Env{status: 200, body: %{errors: [_error | _rest] = errors}}} ->
119+
{:error, errors}
120+
121+
{:ok, %Tesla.Env{status: status, body: body}} when status in 200..299 ->
122+
{:ok, body}
123+
124+
{:ok, %Tesla.Env{status: status, body: body}} ->
125+
Logger.error("Unexpected Platform response when hitting #{url} - (#{status}): #{inspect(body)}")
126+
127+
{:error, {status, body}}
128+
end
129+
end
130+
102131
def operation_name(query) do
103132
regex = ~r/\b(query|mutation|subscription)\s+(\w+)/
104133

test/peek_app_sdk/client_test.exs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,69 @@ defmodule PeekAppSDK.ClientTest do
292292
end
293293
end
294294

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+
307+
{:ok, %Tesla.Env{status: 200, body: response_body}}
308+
end)
309+
310+
assert {:ok, ^response_body} = Client.query_platform_raw(install_id, :get, url, body_params)
311+
end
312+
313+
test "successfully returns raw response body for 201 status" do
314+
install_id = "test_install_id"
315+
url = "https://api.example.com/api/resource"
316+
body_params = %{"key" => "value"}
317+
response_body = %{created: true, id: 123}
318+
319+
Tesla.Adapter.Finch
320+
|> Mimic.stub(:call, fn env, _opts ->
321+
assert env.method == :post
322+
assert env.url == url
323+
324+
{:ok, %Tesla.Env{status: 201, body: response_body}}
325+
end)
326+
327+
assert {:ok, ^response_body} = Client.query_platform_raw(install_id, :post, url, body_params)
328+
end
329+
330+
test "handles error response with status and body tuple" do
331+
install_id = "test_install_id"
332+
url = "https://api.example.com/api/resource"
333+
error_body = %{error: "Not found"}
334+
335+
Tesla.Adapter.Finch
336+
|> Mimic.stub(:call, fn _env, _opts ->
337+
{:ok, %Tesla.Env{status: 404, body: error_body}}
338+
end)
339+
340+
assert {:error, {404, ^error_body}} = Client.query_platform_raw(install_id, :get, url, %{})
341+
end
342+
343+
test "bubbles up errors when status is 200 but errors key is present" do
344+
install_id = "test_install_id"
345+
url = "https://api.example.com/api/resource"
346+
347+
errors = [%{message: "Validation failed"}]
348+
349+
Tesla.Adapter.Finch
350+
|> Mimic.stub(:call, fn _env, _opts ->
351+
{:ok, %Tesla.Env{status: 200, body: %{errors: errors}}}
352+
end)
353+
354+
assert {:error, ^errors} = Client.query_platform_raw(install_id, :post, url, %{})
355+
end
356+
end
357+
295358
describe "operation_name/1" do
296359
test "extracts operation name from query" do
297360
query = "query TestOperation { test }"

0 commit comments

Comments
 (0)