Skip to content

Commit

Permalink
Fix query param validation
Browse files Browse the repository at this point in the history
Sends request params in the query string, to account for stripe/stripe-mock@09826da introducing parameter validation (and removed request body validation) for `GET` requests, since the body of a GET request has no semantic meaning.

This broke endpoints like `/invoices/upcoming?customer=cus_1234`, because we were sending the `customer` param in the request body.
  • Loading branch information
mcrumm authored and snewcomer committed Sep 6, 2018
1 parent af87fa9 commit 1625fb7
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions lib/stripe/api.ex
Expand Up @@ -135,6 +135,20 @@ defmodule Stripe.API do
"""
@spec request(body, method, String.t(), headers, list) ::
{:ok, map} | {:error, Stripe.Error.t()}
def request(body, :get, endpoint, headers, opts) do
{expansion, opts} = Keyword.pop(opts, :expand)
base_url = get_base_url()

req_url =
body
|> Stripe.Util.map_keys_to_atoms()
|> add_object_expansion(expansion)
|> Stripe.URI.encode_query()
|> prepend_url("#{base_url}#{endpoint}")

perform_request(req_url, :get, "", headers, opts)
end

def request(body, method, endpoint, headers, opts) do
{expansion, opts} = Keyword.pop(opts, :expand)
base_url = get_base_url()
Expand Down Expand Up @@ -171,10 +185,6 @@ defmodule Stripe.API do
perform_request(req_url, :post, {:multipart, parts}, req_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
base_url = get_upload_url()
req_url = base_url <> endpoint
Expand Down Expand Up @@ -276,13 +286,18 @@ defmodule Stripe.API do
end
end

defp add_object_expansion(url, expansion) when is_list(expansion) do
expand_str =
expansion
|> Enum.map(&"expand[]=#{&1}")
|> Enum.join("&")
defp prepend_url("", url), do: url
defp prepend_url(query, url), do: "#{url}?#{query}"

defp add_object_expansion(query, expansion) when is_map(query) and is_list(expansion) do
query |> Map.put(:expand, expansion)
end

"#{url}?#{expand_str}"
defp add_object_expansion(url, expansion) when is_list(expansion) do
expansion
|> Enum.map(&"expand[]=#{&1}")
|> Enum.join("&")
|> prepend_url(url)
end

defp add_object_expansion(url, _), do: url
Expand Down

0 comments on commit 1625fb7

Please sign in to comment.