Skip to content

Commit

Permalink
chore: PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jcartwright committed Nov 12, 2024
1 parent 2a15bdb commit 5b6c0d7
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 53 deletions.
27 changes: 0 additions & 27 deletions lib/postscript/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,10 @@ defmodule Postscript.Request do

request
|> config.http_client.send(config.http_client_opts)
|> maybe_validate_json_decode(config)
|> retry(request, config)
|> finish(config)
end

defp maybe_validate_json_decode({:ok, %{body: body, headers: headers}} = response, config) do
headers
|> Enum.reduce(%{}, fn {k, v}, acc -> Map.put(acc, String.downcase(k), v) end)
|> case do
%{"content-type" => "application/json"} ->
case config.json_codec.decode(body) do
{:ok, _decoded} ->
response

{:error, decode_error} ->
Logger.warning([
inspect(__MODULE__),
" received an invalid JSON response ",
inspect(body)
])

{:error, decode_error}
end

_otherwise ->
response
end
end

defp maybe_validate_json_decode(response, _config), do: response

defp retry(response, _request, %_{retry: retry}) when is_nil(retry) or retry == false do
response
end
Expand Down
26 changes: 0 additions & 26 deletions test/postscript_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ defmodule PostscriptTest do

@not_ok_resp %{body: "{\"ok\":false}", headers: [], status_code: 400}

@not_json_resp %{
body: "not json",
headers: [{"content-type", "application/json"}],
status_code: 200
}

test "sends the proper HTTP method" do
Http.Mock.start_link()

Expand Down Expand Up @@ -166,26 +160,6 @@ defmodule PostscriptTest do
assert {:error, %Response{}} = result
end

test "logs warning and returns :error when response is not valid JSON" do
Http.Mock.start_link()

response = {:ok, @not_json_resp}

Http.Mock.put_response(response)

operation = %Operation{method: :post, params: [hello: "world"], path: "/fake"}

assert {result, log} =
with_log([level: :warning], fn ->
Postscript.request(operation, http_client: Http.Mock)
end)

assert {:error, _error} = result

assert log =~ "invalid JSON response"
assert log =~ "\"not json\""
end

test "passes the response through when unrecognized" do
Http.Mock.start_link()

Expand Down

0 comments on commit 5b6c0d7

Please sign in to comment.