Skip to content

Commit

Permalink
Update StripeClient to only allow streaming requests with a block.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcr-stripe committed Jun 22, 2021
1 parent a4abf31 commit 24c64e6
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 102 deletions.
11 changes: 6 additions & 5 deletions lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,19 @@ def execute_request_stream(method, path,
api_base: nil, api_key: nil,
headers: {}, params: {},
&read_body_chunk_block)
unless block_given?
raise ArgumentError,
"execute_request_stream requires a read_body_chunk_block"
end

http_resp, api_key = execute_request_internal(
method, path, api_base, api_key, headers, params, &read_body_chunk_block
)

# When the read_body_chunk_block is given, we no longer have access to the
# response body at this point and so return a response object containing
# only the headers. This is because the body was consumed by the block.
resp = if block_given?
StripeHeadersOnlyResponse.from_net_http(http_resp)
else
StripeStreamResponse.from_net_http(http_resp)
end
resp = StripeHeadersOnlyResponse.from_net_http(http_resp)

[resp, api_key]
end
Expand Down
18 changes: 0 additions & 18 deletions lib/stripe/stripe_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,6 @@ def self.from_net_http(http_resp)
# compatible.
StripeResponse::Headers = StripeResponseHeaders

# StripeStreamResponse includes header-related vitals of the
# response as well as as the raw HTTP body content.
class StripeStreamResponse
include StripeResponseBase

# The raw HTTP body of the response.
attr_accessor :http_body

# Initializes a StripeStreamResponse object from a Net::HTTP::HTTPResponse
# object.
def self.from_net_http(http_resp)
resp = StripeStreamResponse.new
resp.http_body = http_resp.body
StripeResponseBase.populate_for_net_http(resp, http_resp)
resp
end
end

# StripeHeadersOnlyResponse includes only header-related vitals of the
# response. This is used for streaming requests where the response was read
# directly in a block and we explicitly don't want to store the body of the
Expand Down
9 changes: 4 additions & 5 deletions test/stripe/api_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -654,15 +654,14 @@ def read_stream(params = {}, opts = {}, &read_body_chunk_block)
assert_equal "response body", accumulated_body
end

should "supports requesting without a block" do
should "fail when requesting without a block" do
stub_request(:get, "#{Stripe.api_base}/v1/streams/hi_123/read")
.with(query: { foo: "bar" }, headers: { "Stripe-Account" => "acct_hi" })
.to_return(body: "response body")

resp = StreamTestAPIResource.new(id: "hi_123").read_stream({ foo: "bar" }, stripe_account: "acct_hi")

assert_instance_of Stripe::StripeStreamResponse, resp
assert_equal "response body", resp.http_body
e = assert_raises ArgumentError do
resp = StreamTestAPIResource.new(id: "hi_123").read_stream({ foo: "bar" }, stripe_account: "acct_hi")
end
end
end

Expand Down
Loading

0 comments on commit 24c64e6

Please sign in to comment.