-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
61 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule Stripe.Request do | ||
alias Stripe.Util | ||
|
||
@type stripe_response :: {:ok, struct} | {:error, Exception.t} | ||
@type stripe_delete_response :: :ok | {:error, Exception.t} | ||
|
||
@spec create(String.t, struct, map, struct, Keyword.t) :: stripe_response | ||
def create(endpoint, struct, valid_keys, return_struct, opts) do | ||
body = | ||
struct | ||
|> Map.take(valid_keys) | ||
|> Util.drop_nil_keys() | ||
|
||
case Stripe.request(:post, endpoint, body, %{}, opts) do | ||
{:ok, result} -> {:ok, Util.stripe_response_to_struct(return_struct, result)} | ||
{:error, error} -> {:error, error} | ||
end | ||
end | ||
|
||
@spec retrieve(String.t, struct, Keyword.t) :: stripe_response | ||
def retrieve(endpoint, return_struct, opts) do | ||
case Stripe.request(:get, endpoint, %{}, %{}, opts) do | ||
{:ok, result} -> {:ok, Util.stripe_response_to_struct(return_struct, result)} | ||
{:error, error} -> {:error, error} | ||
end | ||
end | ||
|
||
@spec update(String.t, map, map, struct, Keyword.t) :: stripe_response | ||
def update(endpoint, changes, valid_keys, return_struct, opts) do | ||
body = | ||
changes | ||
|> Util.map_keys_to_atoms() | ||
|> Map.take(valid_keys) | ||
|> Util.drop_nil_keys() | ||
|
||
case Stripe.request(:post, endpoint, body, %{}, opts) do | ||
{:ok, result} -> {:ok, Util.stripe_response_to_struct(return_struct, result)} | ||
{:error, error} -> {:error, error} | ||
end | ||
end | ||
|
||
@spec delete(String.t, Keyword.t) :: stripe_delete_response | ||
def delete(endpoint, opts) do | ||
case Stripe.request(:delete, endpoint, %{}, %{}, opts) do | ||
{:ok, _} -> :ok | ||
{:error, error} -> {:error, error} | ||
end | ||
end | ||
end |