Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Automatically decode JSON payloads from HTTP (#2329)
Adam Rutkowski28cec9d93956
diff --git a/lib/plausible/billing/paddle_api.ex b/lib/plausible/billing/paddle_api.exindex 820a329992..555dd917e6 100644--- a/lib/plausible/billing/paddle_api.ex+++ b/lib/plausible/billing/paddle_api.ex@@ -22,12 +22,10 @@ defmodule Plausible.Billing.PaddleApi docase HTTPClient.post(preview_update_url(), @headers, params) do{:ok, response} ->- body = Jason.decode!(response.body)-- if body["success"] do- {:ok, body["response"]}+ if response.body["success"] do+ {:ok, response.body["response"]}else- {:error, body["error"]}+ {:error, response.body["error"]}end{:error, error} ->@@ -51,12 +49,10 @@ defmodule Plausible.Billing.PaddleApi docase HTTPClient.post(update_subscription_url(), @headers, params) do{:ok, response} ->- body = Jason.decode!(response.body)-- if body["success"] do- {:ok, body["response"]}+ if response.body["success"] do+ {:ok, response.body["response"]}else- {:error, body["error"]}+ {:error, response.body["error"]}end{:error, %{reason: reason}} ->@@ -75,13 +71,11 @@ defmodule Plausible.Billing.PaddleApi docase HTTPClient.post(get_subscription_url(), @headers, params) do{:ok, response} ->- body = Jason.decode!(response.body)-- if body["success"] do- [subscription] = body["response"]+ if response.body["success"] do+ [subscription] = response.body["response"]{:ok, subscription}else- {:error, body["error"]}+ {:error, response.body["error"]}end{:error, %{reason: reason}} ->@@ -108,8 +102,7 @@ defmodule Plausible.Billing.PaddleApi doto: Timex.shift(Timex.today(), days: 1) |> Timex.format!("{YYYY}-{0M}-{0D}")}- with {:ok, response} <- HTTPClient.post(invoices_url(), @headers, params),- {:ok, body} <- Jason.decode(response.body),+ with {:ok, %{body: body}} <- HTTPClient.post(invoices_url(), @headers, params),true <- Map.get(body, "success"),[_ | _] = response <- Map.get(body, "response") doEnum.sort(response, fn %{"payout_date" => d1}, %{"payout_date" => d2} ->diff --git a/lib/plausible/google/http.ex b/lib/plausible/google/http.exindex 372fd19126..bb324168fd 100644--- a/lib/plausible/google/http.ex+++ b/lib/plausible/google/http.ex@@ -105,11 +105,8 @@ defmodule Plausible.Google.HTTP doheaders = [{"Content-Type", "application/json"}, {"Authorization", "Bearer #{access_token}"}]case HTTPClient.get(url, headers) do- {:ok, response} ->- response- |> Map.get(:body)- |> Jason.decode!()- |> then(&{:ok, &1})+ {:ok, %{body: body}} ->+ {:ok, body}{:error, reason} = e ->Logger.error("Google Analytics: failed to list sites: #{inspect(reason)}")@@ -131,9 +128,7 @@ defmodule Plausible.Google.HTTP do{:ok, response} = HTTPClient.post(url, headers, params)- response- |> Map.get(:body)- |> Jason.decode!()+ response.bodyenddef list_views_for_user(access_token) do@@ -143,7 +138,7 @@ defmodule Plausible.Google.HTTP docase HTTPClient.get(url, headers) do{:ok, %Finch.Response{body: body, status: 200}} ->- {:ok, Jason.decode!(body)}+ {:ok, body}{:error, %{reason: %Finch.Response{body: body}}} ->Sentry.capture_message("Error fetching Google view ID", extra: %{body: inspect(body)})@@ -179,7 +174,7 @@ defmodule Plausible.Google.HTTP docase HTTPClient.post(url, headers, params) do{:ok, %Finch.Response{body: body, status: 200}} ->- {:ok, Jason.decode!(body)}+ {:ok, body}{:error, %{reason: %Finch.Response{body: body, status: 401}}} ->Sentry.capture_message("Error fetching Google queries", extra: %{body: inspect(body)})@@ -216,11 +211,10 @@ defmodule Plausible.Google.HTTP docase HTTPClient.post(url, headers, params) do{:ok, %Finch.Response{body: body, status: 200}} ->- {:ok, Jason.decode!(body)}+ {:ok, body}{:error, %{reason: %Finch.Response{body: body, status: _non_http_200}}} ->body- |> Jason.decode!()|> Map.get("error")|> then(&{:error, &1})@@ -254,7 +248,7 @@ defmodule Plausible.Google.HTTP docase HTTPClient.post(url, headers, params) do{:ok, %Finch.Response{body: body, status: 200}} ->- report = List.first(Jason.decode!(body)["reports"])+ report = List.first(body["reports"])date =case report["data"]["rows"] dodiff --git a/lib/plausible/http_client.ex b/lib/plausible/http_client.exindex 5de47e0954..55a3fc0e5b 100644--- a/lib/plausible/http_client.ex+++ b/lib/plausible/http_client.ex@@ -65,6 +65,7 @@ defmodule Plausible.HTTPClient domethod|> build_request(url, headers, params)|> do_request()+ |> maybe_decode_body()|> tag_error()end@@ -112,4 +113,28 @@ defmodule Plausible.HTTPClient dodefp tag_error({:error, _} = error) doerrorend++ defp maybe_decode_body({:ok, %{headers: headers, body: body} = resp})+ when is_binary(body) and body != "" do+ if json?(headers) do+ {:ok, update_in(resp.body, &Jason.decode!/1)}+ else+ {:ok, resp}+ end+ end++ defp maybe_decode_body(resp), do: resp++ defp json?(headers) do+ found =+ Enum.find(headers, fn+ {"content-type", "application/json" <> _} ->+ true++ _ ->+ false+ end)++ is_tuple(found)+ endenddiff --git a/test/plausible/http_client_test.exs b/test/plausible/http_client_test.exsindex 669270d4f2..37cf25766b 100644--- a/test/plausible/http_client_test.exs+++ b/test/plausible/http_client_test.exs@@ -124,8 +124,9 @@ defmodule Plausible.HTTPClientTest dotest "header keys are downcased but values are not", %{bypass: bypass} doBypass.expect_once(bypass, "GET", "/get", fn conn ->- Conn.resp(conn, 200, "ok")+ conn|> Conn.put_resp_header("Some-Header", "Header-Value")+ |> Conn.resp(200, "ok")end)assert {:ok, res} = HTTPClient.get(bypass_url(bypass, path: "/get"))@@ -138,4 +139,16 @@ defmodule Plausible.HTTPClientTest do"http://localhost:#{port}#{path}"end++ test "decodes json in case content-type is found", %{bypass: bypass} do+ Bypass.expect_once(bypass, "GET", "/json", fn conn ->+ conn+ |> Conn.put_resp_header("content-type", "application/json; something")+ |> Conn.resp(200, """+ {"answer": 42}+ """)+ end)++ assert {:ok, %{body: %{"answer" => 42}}} = HTTPClient.get(bypass_url(bypass, path: "/json"))+ endenddiff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exsindex f0d0090643..205b43fa46 100644--- a/test/plausible_web/controllers/site_controller_test.exs+++ b/test/plausible_web/controllers/site_controller_test.exs@@ -734,7 +734,10 @@ defmodule PlausibleWeb.SiteControllerTest do"/analytics/v3/management/accounts/~all/webproperties/~all/profiles",fn conn ->response_body = File.read!("fixture/ga_list_views.json")- Plug.Conn.resp(conn, 200, response_body)++ conn+ |> Plug.Conn.put_resp_header("content-type", "application/json")+ |> Plug.Conn.resp(200, response_body)end)