Skip to content

Commit

Permalink
Fix specs for functions returning HTTP errors (#193)
Browse files Browse the repository at this point in the history
* Fix specs for functions returning HTTP errors

* Fix Transaction amount typespec and ClientToken compiler warning

* make default amount same type as true typespec
  • Loading branch information
heroinbob authored Mar 15, 2024
1 parent 4953f61 commit ebb20f7
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 61 deletions.
3 changes: 1 addition & 2 deletions lib/add_on.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ defmodule Braintree.AddOn do

use Braintree.Construction

alias Braintree.ErrorResponse, as: Error
alias Braintree.HTTP

@type t :: %__MODULE__{
Expand Down Expand Up @@ -44,7 +43,7 @@ defmodule Braintree.AddOn do
{:ok, addons} = Braintree.AddOns.all()
"""
@spec all(Keyword.t()) :: {:ok, [t]} | {:error, Error.t()}
@spec all(Keyword.t()) :: {:ok, [t]} | HTTP.error()
def all(opts \\ []) do
with {:ok, %{"add_ons" => add_ons}} <- HTTP.get("add_ons", opts) do
{:ok, new(add_ons)}
Expand Down
9 changes: 4 additions & 5 deletions lib/address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ defmodule Braintree.Address do

use Braintree.Construction

alias Braintree.ErrorResponse, as: Error
alias Braintree.HTTP

@type t :: %__MODULE__{
Expand Down Expand Up @@ -59,7 +58,7 @@ defmodule Braintree.Address do
address.company # Braintree
"""
@spec create(binary, map, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec create(binary, map, Keyword.t()) :: {:ok, t} | HTTP.error()
def create(customer_id, params \\ %{}, opts \\ []) when is_binary(customer_id) do
with {:ok, payload} <-
HTTP.post("customers/#{customer_id}/addresses/", %{address: params}, opts) do
Expand All @@ -74,7 +73,7 @@ defmodule Braintree.Address do
:ok = Braintree.Address.delete("customer_id", "address_id")
"""
@spec delete(binary, binary, Keyword.t()) :: :ok | {:error, Error.t()}
@spec delete(binary, binary, Keyword.t()) :: :ok | HTTP.error()
def delete(customer_id, id, opts \\ []) when is_binary(customer_id) and is_binary(id) do
with {:ok, _reponse} <- HTTP.delete("customers/#{customer_id}/addresses/" <> id, opts) do
:ok
Expand All @@ -94,7 +93,7 @@ defmodule Braintree.Address do
address.company # "New Company Name"
"""
@spec update(binary, binary, map, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec update(binary, binary, map, Keyword.t()) :: {:ok, t} | HTTP.error()
def update(customer_id, id, params, opts \\ []) when is_binary(customer_id) and is_binary(id) do
with {:ok, payload} <-
HTTP.put("customers/#{customer_id}/addresses/" <> id, %{address: params}, opts) do
Expand All @@ -110,7 +109,7 @@ defmodule Braintree.Address do
address = Braintree.Address.find("customer_id", "address_id")
"""
@spec find(binary, binary, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec find(binary, binary, Keyword.t()) :: {:ok, t} | HTTP.error()
def find(customer_id, id, opts \\ []) when is_binary(customer_id) and is_binary(id) do
with {:ok, payload} <- HTTP.get("customers/#{customer_id}/addresses/" <> id, opts) do
{:ok, new(payload)}
Expand Down
1 change: 0 additions & 1 deletion lib/client_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Braintree.ClientToken do
https://developers.braintreepayments.com/reference/request/client-token/generate/ruby
"""

alias Braintree.ErrorResponse, as: Error
alias Braintree.HTTP

@version 2
Expand Down
12 changes: 5 additions & 7 deletions lib/customer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ defmodule Braintree.Customer do
UsBankAccount
}

alias Braintree.ErrorResponse, as: Error

@type t :: %__MODULE__{
id: String.t(),
company: String.t(),
Expand Down Expand Up @@ -79,7 +77,7 @@ defmodule Braintree.Customer do
customer.company # Braintree
"""
@spec create(map, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec create(map, Keyword.t()) :: {:ok, t} | HTTP.error()
def create(params \\ %{}, opts \\ []) do
with {:ok, payload} <- HTTP.post("customers", %{customer: params}, opts) do
{:ok, new(payload)}
Expand All @@ -95,7 +93,7 @@ defmodule Braintree.Customer do
:ok = Braintree.Customer.delete("customer_id")
"""
@spec delete(binary, Keyword.t()) :: :ok | {:error, Error.t()}
@spec delete(binary, Keyword.t()) :: :ok | HTTP.error()
def delete(id, opts \\ []) when is_binary(id) do
with {:ok, _response} <- HTTP.delete("customers/" <> id, opts) do
:ok
Expand All @@ -109,7 +107,7 @@ defmodule Braintree.Customer do
customer = Braintree.Customer.find("customer_id")
"""
@spec find(binary, Keyword.t()) :: {:ok, t} | {:error, :not_found | Error.t()}
@spec find(binary, Keyword.t()) :: {:ok, t} | HTTP.error()
def find(id, opts \\ []) when is_binary(id) do
with {:ok, payload} <- HTTP.get("customers/" <> id, opts) do
{:ok, new(payload)}
Expand All @@ -129,7 +127,7 @@ defmodule Braintree.Customer do
customer.company # "New Company Name"
"""
@spec update(binary, map, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec update(binary, map, Keyword.t()) :: {:ok, t} | HTTP.error()
def update(id, params, opts \\ []) when is_binary(id) and is_map(params) do
with {:ok, payload} <- HTTP.put("customers/" <> id, %{customer: params}, opts) do
{:ok, new(payload)}
Expand All @@ -144,7 +142,7 @@ defmodule Braintree.Customer do
{:ok, customers} = Braintree.Customer.search(%{first_name: %{is: "Jenna"}})
"""
@spec search(map, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec search(map, Keyword.t()) :: {:ok, t} | HTTP.error()
def search(params, opts \\ []) when is_map(params) do
Search.perform(params, "customers", &new/1, opts)
end
Expand Down
3 changes: 1 addition & 2 deletions lib/discount.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ defmodule Braintree.Discount do

use Braintree.Construction

alias Braintree.ErrorResponse, as: Error
alias Braintree.HTTP

@type t :: %__MODULE__{
Expand Down Expand Up @@ -44,7 +43,7 @@ defmodule Braintree.Discount do
{:ok, discounts} = Braintree.Discount.all()
"""
@spec all(Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec all(Keyword.t()) :: {:ok, t} | HTTP.error()
def all(opts \\ []) do
with {:ok, payload} <- HTTP.get("discounts", opts) do
%{"discounts" => discounts} = payload
Expand Down
7 changes: 3 additions & 4 deletions lib/merchant/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ defmodule Braintree.Merchant.Account do

use Braintree.Construction

alias Braintree.ErrorResponse, as: Error
alias Braintree.HTTP
alias Braintree.Merchant.{Business, Funding, Individual}

Expand Down Expand Up @@ -41,7 +40,7 @@ defmodule Braintree.Merchant.Account do
tos_accepted: true,
})
"""
@spec create(map, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec create(map, Keyword.t()) :: {:ok, t} | HTTP.error()
def create(params \\ %{}, opts \\ []) do
with {:ok, payload} <-
HTTP.post("merchant_accounts/create_via_api", %{merchant_account: params}, opts) do
Expand All @@ -62,7 +61,7 @@ defmodule Braintree.Merchant.Account do
merchant.funding_details.account_number # "1234567890"
"""
@spec update(binary, map, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec update(binary, map, Keyword.t()) :: {:ok, t} | HTTP.error()
def update(id, params, opts \\ []) when is_binary(id) do
with {:ok, payload} <-
HTTP.put("merchant_accounts/#{id}/update_via_api", %{merchant_account: params}, opts) do
Expand All @@ -77,7 +76,7 @@ defmodule Braintree.Merchant.Account do
merchant = Braintree.Merchant.find("merchant_id")
"""
@spec find(binary, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec find(binary, Keyword.t()) :: {:ok, t} | HTTP.error()
def find(id, opts \\ []) when is_binary(id) do
with {:ok, payload} <- HTTP.get("merchant_accounts/" <> id, opts) do
{:ok, new(payload)}
Expand Down
19 changes: 13 additions & 6 deletions lib/payment_method.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ defmodule Braintree.PaymentMethod do
may be a `CreditCard` or a `PaypalAccount`.
"""

alias Braintree.{AndroidPayCard, ApplePayCard, CreditCard, HTTP, PaypalAccount, UsBankAccount, VenmoAccount}
alias Braintree.ErrorResponse, as: Error
alias Braintree.{
AndroidPayCard,
ApplePayCard,
CreditCard,
HTTP,
PaypalAccount,
UsBankAccount,
VenmoAccount
}

@doc """
Create a payment method record, or return an error response with after failed
Expand All @@ -30,7 +37,7 @@ defmodule Braintree.PaymentMethod do
| {:ok, PaypalAccount.t()}
| {:ok, UsBankAccount.t()}
| {:ok, VenmoAccount.t()}
| {:error, Error.t()}
| HTTP.error()
def create(params \\ %{}, opts \\ []) do
with {:ok, payload} <- HTTP.post("payment_methods", %{payment_method: params}, opts) do
{:ok, new(payload)}
Expand Down Expand Up @@ -62,7 +69,7 @@ defmodule Braintree.PaymentMethod do
payment_method.cardholder_name # "NEW"
"""
@spec update(String.t(), map, Keyword.t()) ::
{:ok, CreditCard.t()} | {:ok, PaypalAccount.t()} | {:error, Error.t()}
{:ok, CreditCard.t()} | {:ok, PaypalAccount.t()} | HTTP.error()
def update(token, params \\ %{}, opts \\ []) do
path = "payment_methods/any/" <> token

Expand All @@ -78,7 +85,7 @@ defmodule Braintree.PaymentMethod do
{:ok, "Success"} = Braintree.PaymentMethod.delete(token)
"""
@spec delete(String.t(), Keyword.t()) :: :ok | {:error, Error.t()}
@spec delete(String.t(), Keyword.t()) :: :ok | HTTP.error()
def delete(token, opts \\ []) do
path = "payment_methods/any/" <> token

Expand All @@ -100,7 +107,7 @@ defmodule Braintree.PaymentMethod do
{:ok, CreditCard.t()}
| {:ok, PaypalAccount.t()}
| {:ok, UsBankAccount.t()}
| {:error, Error.t()}
| HTTP.error()
def find(token, opts \\ []) do
path = "payment_methods/any/" <> token

Expand Down
5 changes: 2 additions & 3 deletions lib/payment_method_nonce.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule Braintree.PaymentMethodNonce do

use Braintree.Construction

alias Braintree.ErrorResponse, as: Error
alias Braintree.HTTP

@type t :: %__MODULE__{
Expand Down Expand Up @@ -39,7 +38,7 @@ defmodule Braintree.PaymentMethodNonce do
payment_method_nonce.nonce
"""
@spec create(String.t(), Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec create(String.t(), Keyword.t()) :: {:ok, t} | HTTP.error()
def create(payment_method_token, opts \\ []) do
path = "payment_methods/#{payment_method_token}/nonces"

Expand All @@ -57,7 +56,7 @@ defmodule Braintree.PaymentMethodNonce do
payment_method.type #CreditCard
"""
@spec find(String.t(), Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec find(String.t(), Keyword.t()) :: {:ok, t} | HTTP.error()
def find(nonce, opts \\ []) do
path = "payment_method_nonces/" <> nonce

Expand Down
7 changes: 3 additions & 4 deletions lib/paypal_account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule Braintree.PaypalAccount do

use Braintree.Construction

alias Braintree.ErrorResponse, as: Error
alias Braintree.HTTP

@type t :: %__MODULE__{
Expand Down Expand Up @@ -42,7 +41,7 @@ defmodule Braintree.PaypalAccount do
{:ok, paypal_account} = Braintree.PaypalAccount.find(token)
"""
@spec find(String.t(), Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec find(String.t(), Keyword.t()) :: {:ok, t} | HTTP.error()
def find(token, opts \\ []) do
path = "payment_methods/paypal_account/" <> token

Expand All @@ -62,7 +61,7 @@ defmodule Braintree.PaypalAccount do
%{options: %{make_default: true}
)
"""
@spec update(String.t(), map, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec update(String.t(), map, Keyword.t()) :: {:ok, t} | HTTP.error()
def update(token, params, opts \\ []) do
path = "payment_methods/paypal_account/" <> token

Expand All @@ -79,7 +78,7 @@ defmodule Braintree.PaypalAccount do
{:ok, paypal_account} = Braintree.PaypalAccount.delete(token)
"""
@spec delete(String.t(), Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec delete(String.t(), Keyword.t()) :: {:ok, t} | HTTP.error()
def delete(token, opts \\ []) do
path = "payment_methods/paypal_account/" <> token

Expand Down
11 changes: 5 additions & 6 deletions lib/plan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ defmodule Braintree.Plan do

use Braintree.Construction

alias Braintree.ErrorResponse, as: Error
alias Braintree.HTTP

@type t :: %__MODULE__{
Expand Down Expand Up @@ -56,7 +55,7 @@ defmodule Braintree.Plan do
{:ok, plans} = Braintree.Plan.all()
"""
@spec all(Keyword.t()) :: {:ok, [t]} | {:error, Error.t()}
@spec all(Keyword.t()) :: {:ok, [t]} | HTTP.error()
def all(opts \\ []) do
with {:ok, %{"plans" => plans}} <- HTTP.get("plans", opts) do
{:ok, new(plans)}
Expand All @@ -75,7 +74,7 @@ defmodule Braintree.Plan do
price: "10.00"
})
"""
@spec create(map, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec create(map, Keyword.t()) :: {:ok, t} | HTTP.error()
def create(params, opts \\ []) do
with {:ok, %{"plan" => plan}} <- HTTP.post("plans", %{plan: params}, opts) do
{:ok, new(plan)}
Expand All @@ -92,7 +91,7 @@ defmodule Braintree.Plan do
{:error, :not_found} = Braintree.Plan.find("non-existing plan_id")
"""
@spec find(String.t(), Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec find(String.t(), Keyword.t()) :: {:ok, t} | HTTP.error()
def find(id, opts \\ []) when is_binary(id) do
with {:ok, %{"plan" => plan}} <- HTTP.get("plans/#{id}", opts) do
{:ok, new(plan)}
Expand All @@ -109,7 +108,7 @@ defmodule Braintree.Plan do
{:error, :not_found} = Braintree.Plan.find("non-existing plan_id")
"""
@spec update(String.t(), map, Keyword.t()) :: {:ok, t} | {:error, Error.t()}
@spec update(String.t(), map, Keyword.t()) :: {:ok, t} | HTTP.error()
def update(id, params, opts \\ []) when is_binary(id) and is_map(params) do
with {:ok, %{"plan" => plan}} <- HTTP.put("plans/#{id}", %{plan: params}, opts) do
{:ok, new(plan)}
Expand All @@ -121,7 +120,7 @@ defmodule Braintree.Plan do
A plan can't be deleted if it has any former or current subscriptions associated with it.
If there is no plan with the specified id, `{:error, :not_found}` is returned.
"""
@spec delete(String.t(), Keyword.t()) :: :ok | {:error, Error.t()}
@spec delete(String.t(), Keyword.t()) :: :ok | HTTP.error()
def delete(id, opts \\ []) when is_binary(id) do
with {:ok, _response} <- HTTP.delete("plans/#{id}", opts) do
:ok
Expand Down
3 changes: 1 addition & 2 deletions lib/search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Braintree.Search do
https://developers.braintreepayments.com/reference/general/searching/search-fields/ruby
"""

alias Braintree.ErrorResponse, as: Error
alias Braintree.HTTP

@doc """
Expand All @@ -18,7 +17,7 @@ defmodule Braintree.Search do
{:ok, customers} = Braintree.Search.perform(search_params, "customers", &Braintree.Customer.new/1)
"""
@spec perform(map, String.t(), fun(), Keyword.t()) :: {:ok, [any]} | {:error, :not_found | Error.t()}
@spec perform(map, String.t(), fun(), Keyword.t()) :: {:ok, [any]} | HTTP.error()
def perform(params, resource, initializer, opts \\ []) when is_map(params) do
with {:ok, payload} <- HTTP.post(resource <> "/advanced_search_ids", %{search: params}, opts) do
fetch_all_records(payload, resource, initializer, opts)
Expand Down
3 changes: 1 addition & 2 deletions lib/settlement_batch_summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ defmodule Braintree.SettlementBatchSummary do

import Braintree.Util, only: [atomize: 1]

alias Braintree.ErrorResponse, as: Error
alias Braintree.HTTP

defmodule Record do
Expand Down Expand Up @@ -65,7 +64,7 @@ defmodule Braintree.SettlementBatchSummary do
Braintree.SettlementBatchSummary("2016-9-5", "custom_field_1")
"""
@spec generate(binary, binary | nil, Keyword.t()) :: {:ok, [t]} | {:error, Error.t()}
@spec generate(binary, binary | nil, Keyword.t()) :: {:ok, [t]} | HTTP.error()
def generate(settlement_date, custom_field \\ nil, opts \\ []) do
criteria = build_criteria(settlement_date, custom_field)
params = %{settlement_batch_summary: criteria}
Expand Down
Loading

0 comments on commit ebb20f7

Please sign in to comment.