Skip to content

Commit

Permalink
Fix upload file api (#334)
Browse files Browse the repository at this point in the history
* Fix upload file api
  • Loading branch information
chubarovNick authored and begedin committed Apr 11, 2018
1 parent 8ab712c commit fc7fb73
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 33 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ config/config.secret.exs
/doc
.DS_Store

# elixir LS files

/.elixir_ls
# IntelliJ IDEA files

/.idea
*.iml
*.iml
74 changes: 42 additions & 32 deletions lib/stripe/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ defmodule Stripe.API do
Map.put(existing_headers, "Authorization", "Bearer #{api_key}")
end

@spec fetch_api_key(String.t | nil) :: String.t
@spec fetch_api_key(String.t() | nil) :: String.t()
defp fetch_api_key(api_key) do
case api_key do
key when is_binary(key) -> key
Expand Down Expand Up @@ -132,9 +132,6 @@ defmodule Stripe.API do
@spec request(body, method, String.t(), headers, list) ::
{:ok, map} | {:error, Stripe.Error.t()}
def request(body, method, endpoint, headers, opts) do
{connect_account_id, opts} = Keyword.pop(opts, :connect_account)
{api_key, opts} = Keyword.pop(opts, :api_key)

base_url = get_base_url()
req_url = base_url <> endpoint

Expand All @@ -143,50 +140,41 @@ defmodule Stripe.API do
|> Stripe.Util.map_keys_to_atoms()
|> Stripe.URI.encode_query()

req_headers =
headers
|> add_default_headers()
|> add_auth_header(api_key)
|> add_connect_header(connect_account_id)
|> Map.to_list()
perform_request(req_url, method, req_body, headers, opts)
end

req_opts =
opts
|> add_default_options()
|> add_pool_option()
@doc """
A low level utility function to make a direct request to the files Stripe API
"""
@spec request_file_upload(body, method, String.t(), headers, list) ::
{:ok, map} | {:error, Stripe.Error.t()}
def request_file_upload(body, :post, endpoint, headers, opts) do
base_url = get_upload_url()
req_url = base_url <> endpoint

@http_module.request(method, req_url, req_headers, req_body, req_opts)
|> handle_response()
parts =
body
|> Enum.map(fn {key, value} ->
{Stripe.Util.multipart_key(key), value}
end)

perform_request(req_url, :post, {:multipart, parts}, headers, opts)
end

@doc """
"""
@spec request_file_upload(body, method, String.t(), headers, list) ::
{:ok, map} | {:error, Stripe.Error.t()}
def request_file_upload(body, method, endpoint, headers, opts) do
{connect_account_id, opts} = Keyword.pop(opts, :connect_account)
{api_key, opts} = Keyword.pop(opts, :api_key)

base_url = get_upload_url()
req_url = base_url <> endpoint

req_body =
body
|> Stripe.Util.map_keys_to_atoms()
|> Stripe.URI.encode_query()
req_headers =
headers
|> add_multipart_form_headers()
|> add_auth_header(api_key)
|> add_connect_header(connect_account_id)
|> Map.to_list()

req_opts =
opts
|> add_default_options()
|> add_pool_option()

@http_module.request(method, req_url, req_headers, req_body, req_opts)
|> handle_response()
perform_request(req_url, method, req_body, headers, opts)
end

@doc """
Expand All @@ -212,6 +200,28 @@ defmodule Stripe.API do
|> handle_response()
end

@spec perform_request(String.t(), method, body, headers, list) ::
{:ok, map} | {:error, Stripe.Error.t()}
defp perform_request(req_url, method, body, headers, opts) do
{connect_account_id, opts} = Keyword.pop(opts, :connect_account)
{api_key, opts} = Keyword.pop(opts, :api_key)

req_headers =
headers
|> add_default_headers()
|> add_auth_header(api_key)
|> add_connect_header(connect_account_id)
|> Map.to_list()

req_opts =
opts
|> add_default_options()
|> add_pool_option()

@http_module.request(method, req_url, req_headers, body, req_opts)
|> handle_response()
end

@spec handle_response(http_success | http_failure) :: {:ok, map} | {:error, Stripe.Error.t()}
defp handle_response({:ok, status, headers, body}) when status >= 200 and status <= 299 do
decoded_body =
Expand Down
4 changes: 4 additions & 0 deletions lib/stripe/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ defmodule Stripe.Util do
module |> Atom.to_string() |> String.trim_leading("Elixir.")
end

def multipart_key(:file), do: :file
def multipart_key(key) when is_atom(key), do: Atom.to_string(key)
def multipart_key(key), do: key

def normalize_id(%{id: id}) when id !== nil, do: id
def normalize_id(id) when is_binary(id), do: id

Expand Down
8 changes: 8 additions & 0 deletions test/stripe/util_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ defmodule Stripe.UtilTest do
assert object_name_to_module("token") == Stripe.Token
end
end

describe "multipart_key/1" do
test "handle all multipart keys" do
assert multipart_key(:file) == :file
assert multipart_key(:foo) == "foo"
assert multipart_key("foo") == "foo"
end
end
end

0 comments on commit fc7fb73

Please sign in to comment.