diff --git a/README.md b/README.md index 4dca6644..640a0916 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,20 @@ SendGrid offers extra features on top of regular SMTP email like transactional templates with substitution tags. See [Bamboo.SendGridHelper](https://hexdocs.pm/bamboo/Bamboo.SendGridHelper.html). +## JSON support + +Postgrex comes with JSON support out of the box via the [Poison](https://github.com/devinus/poison) library. To use it, add `:poison` to your dependencies: + +```elixir +{:poison, ">= 1.5.0"} +``` + +You can customize it to use another library via the `:json_library` configuration: + +```elixir +config :bamboo, :json_library, SomeOtherLib +``` + ## Testing You can use the Bamboo.TestAdapter along with [Bamboo.Test] to make testing your diff --git a/lib/bamboo.ex b/lib/bamboo.ex index 2b753b0e..9fd9076b 100644 --- a/lib/bamboo.ex +++ b/lib/bamboo.ex @@ -41,4 +41,8 @@ defmodule Bamboo do opts = [strategy: :one_for_one, name: Bamboo.Supervisor] Supervisor.start_link(children, opts) end + + def json_library do + Application.get_env(:bamboo, :json_library, Poison) + end end diff --git a/lib/bamboo/adapters/mandrill_adapter.ex b/lib/bamboo/adapters/mandrill_adapter.ex index 5a4e7193..746b8653 100644 --- a/lib/bamboo/adapters/mandrill_adapter.ex +++ b/lib/bamboo/adapters/mandrill_adapter.ex @@ -29,12 +29,12 @@ defmodule Bamboo.MandrillAdapter do def deliver(email, config) do api_key = get_key(config) - params = email |> convert_to_mandrill_params(api_key) |> Poison.encode!() + params = email |> convert_to_mandrill_params(api_key) |> Bamboo.json_library().encode!() uri = [base_uri(), "/", api_path(email)] case :hackney.post(uri, headers(), params, [:with_body]) do {:ok, status, _headers, response} when status > 299 -> - filtered_params = params |> Poison.decode!() |> Map.put("key", "[FILTERED]") + filtered_params = params |> Bamboo.json_library().decode!() |> Map.put("key", "[FILTERED]") raise_api_error(@service_name, response, filtered_params) {:ok, status, headers, response} -> diff --git a/lib/bamboo/adapters/send_grid_adapter.ex b/lib/bamboo/adapters/send_grid_adapter.ex index d41dd3d6..9ef15e08 100644 --- a/lib/bamboo/adapters/send_grid_adapter.ex +++ b/lib/bamboo/adapters/send_grid_adapter.ex @@ -37,12 +37,12 @@ defmodule Bamboo.SendGridAdapter do def deliver(email, config) do api_key = get_key(config) - body = email |> to_sendgrid_body(config) |> Poison.encode!() + body = email |> to_sendgrid_body(config) |> Bamboo.json_library().encode!() url = [base_uri(), @send_message_path] case :hackney.post(url, headers(api_key), body, [:with_body]) do {:ok, status, _headers, response} when status > 299 -> - filtered_params = body |> Poison.decode!() |> Map.put("key", "[FILTERED]") + filtered_params = body |> Bamboo.json_library().decode!() |> Map.put("key", "[FILTERED]") raise_api_error(@service_name, response, filtered_params) {:ok, status, headers, response} -> diff --git a/mix.exs b/mix.exs index 20ff4b38..cbe539d7 100644 --- a/mix.exs +++ b/mix.exs @@ -33,7 +33,7 @@ defmodule Bamboo.Mixfile do # Type "mix help compile.app" for more information def application do [ - applications: [:logger, :hackney, :poison], + applications: [:logger, :hackney], mod: {Bamboo, []} ] end @@ -62,7 +62,7 @@ defmodule Bamboo.Mixfile do {:ex_doc, "~> 0.19", only: :dev}, {:earmark, ">= 0.0.0", only: :dev}, {:hackney, ">= 1.13.0"}, - {:poison, ">= 1.5.0"} + {:poison, ">= 1.5.0", optional: true}, ] end end