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

Expose the response mappers #17

Closed
sitch opened this issue Mar 1, 2019 · 5 comments
Closed

Expose the response mappers #17

sitch opened this issue Mar 1, 2019 · 5 comments

Comments

@sitch
Copy link
Contributor

sitch commented Mar 1, 2019

Currently Plaid.Utils.map_body/2 is privately defined, but this can be useful externally to libs. For instance, implementing an Ecto.Type behaviour.

This parsing functionality could be safely moved into its own module and called from Plaid.Utils.handle_resp/2 to maintain the existing public signature.

Moving this to somewhere like Plaid.Middlewares.ResponseHandler makes a lot of sense to me.

@wfgilman
Copy link
Owner

wfgilman commented Mar 3, 2019

How would you imagine an external lib using the response mapper?

@sitch
Copy link
Contributor Author

sitch commented Mar 3, 2019

This is not my exact usecase but it demonstrates a usecase

defmodule MyApp.EctoPlaidTransactions do
  @behaviour Ecto.Type
  
  def type, do: :map

  def cast(%Plaid.Transactions{} = plaid_transactions) do
    {:ok, plaid_transactions}
  end

  def cast(plaid_transactions) when is_map(plaid_transactions) do
    # This is a bit hacky due to an unnecessary transform
    with {:ok, plaid_transactions_binary} <- dump(plaid_transactions) do
      cast(plaid_transactions_binary)
    else
      _ -> :error
    end
  end

  def cast(plaid_transactions) when is_binary(plaid_transactions) do
    # Currently `.map_body/2` is private though
    Plaid.Utils.map_body(plaid_transactions, :transactions)
  end

  def dump(%Plaid.Transactions{} = plaid_transactions) do
    Poison.encode(plaid_transactions)
  end
end

@wfgilman
Copy link
Owner

wfgilman commented Mar 3, 2019

Gotcha. Yeah I can do that.

@wfgilman
Copy link
Owner

wfgilman commented Mar 5, 2019

@sitch would this PR address your issue?

@sitch
Copy link
Contributor Author

sitch commented Mar 6, 2019

Yes sir! Very thanks 👍

@sitch sitch closed this as completed Mar 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants