Skip to content

Commit

Permalink
Merge 9e52257 into 1f3977c
Browse files Browse the repository at this point in the history
  • Loading branch information
hpopp committed Jul 3, 2020
2 parents 1f3977c + 9e52257 commit c13d27d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ scratchpad.txt
.DS_Store
.iex.exs
*.swp

# Dialyzer artifacts
/priv/plts/*.plt
/priv/plts/*.plt.hash
12 changes: 12 additions & 0 deletions lib/pigeon.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ defmodule Pigeon do
end
end

@doc """
Returns the configured JSON encoding library for Pigeon.
To customize the JSON library, include the following in your config/config.exs:
config :phoenix, :json_library, Jason
"""
@spec json_library :: module
def json_library do
Application.get_env(:pigeon, :json_library, Jason)
end

@doc false
def start_connection(state) do
opts = [restart: :temporary, id: :erlang.make_ref()]
Expand Down
11 changes: 6 additions & 5 deletions lib/pigeon/adm/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Pigeon.ADM.Worker do

def initialize_worker(config) do
Pigeon.ADM.Config.validate!(config)

{:ok,
%{
config: config,
Expand Down Expand Up @@ -102,7 +103,7 @@ defmodule Pigeon.ADM.Worker do

case post do
{:ok, %{status_code: 200, body: response_body}} ->
{:ok, response_json} = Poison.decode(response_body)
{:ok, response_json} = Pigeon.json_library().decode(response_body)

%{
"access_token" => access_token,
Expand All @@ -123,7 +124,7 @@ defmodule Pigeon.ADM.Worker do
}}

{:ok, %{body: response_body}} ->
{:ok, response_json} = Poison.decode(response_body)
{:ok, response_json} = Pigeon.json_library().decode(response_body)
Logger.error("Refresh token response: #{inspect(response_json)}")
{:error, response_json["reason"]}
end
Expand Down Expand Up @@ -192,7 +193,7 @@ defmodule Pigeon.ADM.Worker do
|> put_consolidation_key(notification.consolidation_key)
|> put_expires_after(notification.expires_after)
|> put_md5(notification.md5)
|> Poison.encode!()
|> Pigeon.json_library().encode!()
end

defp put_consolidation_key(payload, nil), do: payload
Expand Down Expand Up @@ -220,12 +221,12 @@ defmodule Pigeon.ADM.Worker do
do: handle_error_status_code(status, body, notification, on_response)

defp handle_200_status(body, notification, on_response) do
{:ok, json} = Poison.decode(body)
{:ok, json} = Pigeon.json_library().decode(body)
parse_result(notification, json, on_response)
end

defp handle_error_status_code(status, body, notification, on_response) do
case Poison.decode(body) do
case Pigeon.json_library().decode(body) do
{:ok, %{"reason" => _reason} = result_json} ->
parse_result(notification, result_json, on_response)

Expand Down
2 changes: 1 addition & 1 deletion lib/pigeon/apns/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Pigeon.APNS.Error do
@spec parse(binary) :: error_response
def parse(data) do
data
|> Poison.decode!()
|> Pigeon.json_library().decode!()
|> Map.get("reason")
|> parse_response()
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pigeon/apns/shared.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Pigeon.APNS.Shared do

@spec push_headers(config, Notification.t(), opts) :: headers()
def push_headers(_config, notification, _opts) do
json = Poison.encode!(notification.payload)
json = Pigeon.json_library().encode!(notification.payload)

[
{":method", "POST"},
Expand All @@ -42,7 +42,7 @@ defmodule Pigeon.APNS.Shared do

@spec push_payload(config, Notification.t(), opts) :: iodata | no_return
def push_payload(_config, notification, _opts) do
Poison.encode!(notification.payload)
Pigeon.json_library().encode!(notification.payload)
end

def handle_end_stream(_config, stream, notification, on_response) do
Expand Down
6 changes: 3 additions & 3 deletions lib/pigeon/fcm/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ defimpl Pigeon.Configurable, for: Pigeon.FCM.Config do
end

defp do_handle_end_stream(200, body, notif, on_response) do
result = Poison.decode!(body)
result = Pigeon.json_library().decode!(body)
notif = %{notif | status: :success}
parse_result(notif.registration_id, result, on_response, notif)
end
Expand Down Expand Up @@ -174,12 +174,12 @@ defimpl Pigeon.Configurable, for: Pigeon.FCM.Config do
end

def parse_error(data) do
case Poison.decode(data) do
case Pigeon.json_library().decode(data) do
{:ok, response} ->
response["reason"] |> Macro.underscore() |> String.to_existing_atom()

error ->
"Poison parse failed: #{inspect(error)}, body: #{inspect(data)}"
"JSON parse failed: #{inspect(error)}, body: #{inspect(data)}"
|> Logger.error()
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pigeon/fcm/notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ defimpl Pigeon.Encodable, for: Pigeon.FCM.Notification do
|> encode_attr("content_available", notif.content_available)
|> encode_attr("mutable_content", notif.mutable_content)
|> encode_attr("condition", notif.condition)
|> Poison.encode!()
|> Pigeon.json_library().encode!()
end

defp encode_attr(map, _key, nil), do: map
Expand Down
17 changes: 10 additions & 7 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
defmodule Pigeon.Mixfile do
use Mix.Project

@version "1.5.1"
@version "2.0.0"

def project do
[
app: :pigeon,
build_embedded: Mix.env() == :prod,
deps: deps(),
description: description(),
dialyzer: [
plt_add_apps: [:kadabra, :poison],
ignore_warnings: "config/dialyzer.ignore-warnings"
],
dialyzer: dialyzer(),
docs: [
main: "getting-started",
extras: [
Expand Down Expand Up @@ -57,9 +54,9 @@ defmodule Pigeon.Mixfile do
{:ex_doc, "~> 0.18", only: :dev},
{:gen_stage, "~> 0.12 or ~> 1.0"},
{:httpoison, "~> 0.7 or ~> 1.0"},
{:jason, "~> 1.0", optional: true},
{:joken, "~> 2.1"},
{:kadabra, "~> 0.4.3", optional: true},
{:poison, "~> 2.0 or ~> 3.0 or ~> 4.0"}
{:kadabra, "~> 0.4.3", optional: true}
]
end

Expand All @@ -78,4 +75,10 @@ defmodule Pigeon.Mixfile do
maintainers: ["Henry Popp", "Tyler Hurst"]
]
end

defp dialyzer do
[
plt_file: {:no_warn, "priv/plts/dialyzer.plt"}
]
end
end

0 comments on commit c13d27d

Please sign in to comment.