Skip to content

Commit

Permalink
update make_request for file_upload
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Nov 21, 2017
1 parent fea1cab commit c05ca29
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/stripe/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ defmodule Stripe.API do

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()
Expand All @@ -183,7 +187,7 @@ defmodule Stripe.API do
|> add_default_options()
|> add_pool_option()

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

Expand Down
6 changes: 3 additions & 3 deletions lib/stripe/core_resources/file_upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Stripe.FileUpload do
|> put_endpoint(@plural_endpoint)
|> put_method(:post)
|> put_params(params)
|> make_request()
|> make_file_upload_request()
end

@doc """
Expand All @@ -58,7 +58,7 @@ defmodule Stripe.FileUpload do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:get)
|> make_request()
|> make_file_upload_request()
end

@doc """
Expand All @@ -71,6 +71,6 @@ defmodule Stripe.FileUpload do
|> put_method(:get)
|> put_params(params)
|> cast_to_id([:ending_before, :starting_after, :limit, :purpose])
|> make_request()
|> make_file_upload_request()
end
end
14 changes: 14 additions & 0 deletions lib/stripe/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ defmodule Stripe.Request do
end
end

@doc """
Executes the request and returns the response for file uploads
"""
@spec make_file_upload_request(t) :: {:ok, struct} | {:error, Stripe.Error.t}
def make_file_upload_request(%Request{params: params, endpoint: endpoint, method: method, opts: opts} = request) do
with\
{:ok, params} <- do_cast_to_id(params, request.cast_to_id),
{:ok, endpoint} <- consolidate_endpoint(endpoint, params),
{:ok, result} <- API.request_file_upload(params, method, endpoint, %{}, opts)
do
{:ok, Converter.convert_result(result)}
end
end

defp do_cast_to_id(params, cast_to_id) do
to_cast = MapSet.to_list(cast_to_id)
params = Enum.reduce(to_cast, params, fn
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Logger.configure level: :info
{:ok, pid} = Stripe.StripeMock.start_link port: 12123, global: true
Process.sleep(250)
Application.put_env(:stripity_stripe, :api_base_url, "http://localhost:12123/v1/")
Application.put_env(:stripity_stripe, :api_upload_url, "http://localhost:12123/v1/")
Application.put_env(:stripity_stripe, :api_key, "sk_test_123")

defmodule Helper do
Expand Down

0 comments on commit c05ca29

Please sign in to comment.