Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make JSON library configurable #374

Merged
merged 2 commits into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/bamboo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/bamboo/adapters/mandrill_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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} ->
Expand Down
4 changes: 2 additions & 2 deletions lib/bamboo/adapters/send_grid_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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} ->
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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