mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-08-14 00:02:11 -04:00
Compare commits
3 Commits
0f7b05fbb1
...
a0b5b50bc1
Author | SHA1 | Date | |
---|---|---|---|
|
a0b5b50bc1 | ||
|
dfb7cf1768 | ||
|
f1111458c0 |
@ -140,6 +140,11 @@ module Stripe
|
|||||||
req = RawRequest.new
|
req = RawRequest.new
|
||||||
req.execute(method, url, params, opts)
|
req.execute(method, url, params, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.deserialize(data)
|
||||||
|
data = JSON.parse(data) if data.is_a?(String)
|
||||||
|
Util.convert_to_stripe_object(data, {})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Stripe.log_level = ENV["STRIPE_LOG"] unless ENV["STRIPE_LOG"].nil?
|
Stripe.log_level = ENV["STRIPE_LOG"] unless ENV["STRIPE_LOG"].nil?
|
||||||
|
@ -47,6 +47,7 @@ module Stripe
|
|||||||
api_key = headers.delete(:api_key)
|
api_key = headers.delete(:api_key)
|
||||||
api_base = headers.delete(:api_base)
|
api_base = headers.delete(:api_base)
|
||||||
client = headers.delete(:client)
|
client = headers.delete(:client)
|
||||||
|
encoding = headers.delete(:encoding)
|
||||||
# Assume all remaining opts must be headers
|
# Assume all remaining opts must be headers
|
||||||
|
|
||||||
resp, opts[:api_key] = client.send(
|
resp, opts[:api_key] = client.send(
|
||||||
@ -54,6 +55,7 @@ module Stripe
|
|||||||
method, url,
|
method, url,
|
||||||
api_base: api_base, api_key: api_key,
|
api_base: api_base, api_key: api_key,
|
||||||
headers: headers, params: params,
|
headers: headers, params: params,
|
||||||
|
encoding: encoding,
|
||||||
&read_body_chunk_block
|
&read_body_chunk_block
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -212,9 +212,9 @@ module Stripe
|
|||||||
end
|
end
|
||||||
|
|
||||||
def execute_request(method, path,
|
def execute_request(method, path,
|
||||||
api_base: nil, api_key: nil, headers: {}, params: {})
|
api_base: nil, api_key: nil, headers: {}, params: {}, encoding: :form)
|
||||||
http_resp, api_key = execute_request_internal(
|
http_resp, api_key = execute_request_internal(
|
||||||
method, path, api_base, api_key, headers, params
|
method, path, api_base, api_key, headers, params, encoding
|
||||||
)
|
)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
@ -245,6 +245,7 @@ module Stripe
|
|||||||
def execute_request_stream(method, path,
|
def execute_request_stream(method, path,
|
||||||
api_base: nil, api_key: nil,
|
api_base: nil, api_key: nil,
|
||||||
headers: {}, params: {},
|
headers: {}, params: {},
|
||||||
|
encoding: :form,
|
||||||
&read_body_chunk_block)
|
&read_body_chunk_block)
|
||||||
unless block_given?
|
unless block_given?
|
||||||
raise ArgumentError,
|
raise ArgumentError,
|
||||||
@ -252,7 +253,7 @@ module Stripe
|
|||||||
end
|
end
|
||||||
|
|
||||||
http_resp, api_key = execute_request_internal(
|
http_resp, api_key = execute_request_internal(
|
||||||
method, path, api_base, api_key, headers, params, &read_body_chunk_block
|
method, path, api_base, api_key, headers, params, encoding, &read_body_chunk_block
|
||||||
)
|
)
|
||||||
|
|
||||||
# When the read_body_chunk_block is given, we no longer have access to the
|
# When the read_body_chunk_block is given, we no longer have access to the
|
||||||
@ -431,14 +432,13 @@ module Stripe
|
|||||||
end
|
end
|
||||||
|
|
||||||
private def execute_request_internal(method, path,
|
private def execute_request_internal(method, path,
|
||||||
api_base, api_key, headers, params,
|
api_base, api_key, headers, params, encoding,
|
||||||
&read_body_chunk_block)
|
&read_body_chunk_block)
|
||||||
raise ArgumentError, "method should be a symbol" \
|
raise ArgumentError, "method should be a symbol" \
|
||||||
unless method.is_a?(Symbol)
|
unless method.is_a?(Symbol)
|
||||||
raise ArgumentError, "path should be a string" \
|
raise ArgumentError, "path should be a string" \
|
||||||
unless path.is_a?(String)
|
unless path.is_a?(String)
|
||||||
|
|
||||||
json = false
|
|
||||||
api_base ||= config.api_base
|
api_base ||= config.api_base
|
||||||
api_key ||= config.api_key
|
api_key ||= config.api_key
|
||||||
authenticator ||= config.authenticator
|
authenticator ||= config.authenticator
|
||||||
@ -469,7 +469,7 @@ module Stripe
|
|||||||
# a log-friendly variant of the encoded form. File objects are displayed
|
# a log-friendly variant of the encoded form. File objects are displayed
|
||||||
# as such instead of as their file contents.
|
# as such instead of as their file contents.
|
||||||
body, body_log =
|
body, body_log =
|
||||||
body_params ? encode_body(body_params, headers, json) : [nil, nil]
|
body_params ? encode_body(body_params, headers, encoding) : [nil, nil]
|
||||||
|
|
||||||
authenticator.authenticate(method, headers, body) unless api_key
|
authenticator.authenticate(method, headers, body) unless api_key
|
||||||
|
|
||||||
@ -545,7 +545,7 @@ module Stripe
|
|||||||
# Encodes a set of body parameters using multipart if `Content-Type` is set
|
# Encodes a set of body parameters using multipart if `Content-Type` is set
|
||||||
# for that, or standard form-encoding otherwise. Returns the encoded body
|
# for that, or standard form-encoding otherwise. Returns the encoded body
|
||||||
# and a version of the encoded body that's safe to be logged.
|
# and a version of the encoded body that's safe to be logged.
|
||||||
private def encode_body(body_params, headers, json)
|
private def encode_body(body_params, headers, encoding)
|
||||||
body = nil
|
body = nil
|
||||||
flattened_params = Util.flatten_params(body_params)
|
flattened_params = Util.flatten_params(body_params)
|
||||||
|
|
||||||
@ -561,14 +561,14 @@ module Stripe
|
|||||||
flattened_params =
|
flattened_params =
|
||||||
flattened_params.map { |k, v| [k, v.is_a?(String) ? v : v.to_s] }.to_h
|
flattened_params.map { |k, v| [k, v.is_a?(String) ? v : v.to_s] }.to_h
|
||||||
|
|
||||||
elsif json
|
elsif encoding == :json
|
||||||
body = JSON.generate(body_params)
|
body = JSON.generate(body_params)
|
||||||
headers["Content-Type"] = "application/json"
|
headers["Content-Type"] = "application/json"
|
||||||
else
|
else
|
||||||
body = Util.encode_parameters(body_params)
|
body = Util.encode_parameters(body_params)
|
||||||
end
|
end
|
||||||
|
|
||||||
if json
|
if encoding == :json
|
||||||
body_log = body
|
body_log = body
|
||||||
else
|
else
|
||||||
# We don't use `Util.encode_parameters` partly as an optimization (to not
|
# We don't use `Util.encode_parameters` partly as an optimization (to not
|
||||||
|
@ -141,7 +141,7 @@ class StripeTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "raw_request" do
|
context "raw_request" do
|
||||||
should "send request and return a response" do
|
should "send get request and return a response" do
|
||||||
expected_body = "{\"id\": \"acc_123\"}"
|
expected_body = "{\"id\": \"acc_123\"}"
|
||||||
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
|
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
|
||||||
.to_return(body: expected_body)
|
.to_return(body: expected_body)
|
||||||
@ -151,5 +151,78 @@ class StripeTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
assert_equal resp.http_body, expected_body
|
assert_equal resp.http_body, expected_body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "send post request with body and return a response" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\"}"
|
||||||
|
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acc_123")
|
||||||
|
.with(body: "p1=1&p2=string")
|
||||||
|
.to_return(body: expected_body)
|
||||||
|
|
||||||
|
resp = Stripe.raw_request(:post, "/v1/accounts/acc_123", { p1: 1, p2: "string" })
|
||||||
|
|
||||||
|
assert_equal resp.http_body, expected_body
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
should "send post request with json body and return a response" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\"}"
|
||||||
|
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acc_123")
|
||||||
|
.with(body: "{\"p1\":1,\"p2\":\"string\"}")
|
||||||
|
.to_return(body: expected_body)
|
||||||
|
|
||||||
|
resp = Stripe.raw_request(:post, "/v1/accounts/acc_123", { p1: 1, p2: "string" }, { encoding: :json })
|
||||||
|
|
||||||
|
assert_equal resp.http_body, expected_body
|
||||||
|
end
|
||||||
|
|
||||||
|
should "send post request with json body and headers and return a response" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\"}"
|
||||||
|
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acc_123")
|
||||||
|
.with(body: "{\"p1\":1,\"p2\":\"string\"}", headers: { "Stripe-Account" => "bar" })
|
||||||
|
.to_return(body: expected_body)
|
||||||
|
|
||||||
|
resp = Stripe.raw_request(:post, "/v1/accounts/acc_123", { p1: 1, p2: "string" }, { encoding: :json, "Stripe-Account": "bar" })
|
||||||
|
|
||||||
|
assert_equal resp.http_body, expected_body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
context "deserialize" do
|
||||||
|
should "deserializes string into known object" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\", \"object\": \"account\"}"
|
||||||
|
|
||||||
|
obj = Stripe.deserialize(expected_body)
|
||||||
|
|
||||||
|
assert_equal obj.class, Stripe::Account
|
||||||
|
assert_equal obj.id, "acc_123"
|
||||||
|
end
|
||||||
|
|
||||||
|
should "deserializes string into unknown object" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\", \"object\": \"unknown\"}"
|
||||||
|
|
||||||
|
obj = Stripe.deserialize(expected_body)
|
||||||
|
|
||||||
|
assert_equal obj.class, Stripe::StripeObject
|
||||||
|
assert_equal obj.id, "acc_123"
|
||||||
|
end
|
||||||
|
|
||||||
|
should "deserializes hash into known object" do
|
||||||
|
expected_body = {"id" => "acc_123", "object" => "account"}
|
||||||
|
|
||||||
|
obj = Stripe.deserialize(expected_body)
|
||||||
|
|
||||||
|
assert_equal obj.class, Stripe::Account
|
||||||
|
assert_equal obj.id, "acc_123"
|
||||||
|
end
|
||||||
|
|
||||||
|
should "deserializes hash into unknown object" do
|
||||||
|
expected_body = {"id" => "acc_123", "object" => "unknown"}
|
||||||
|
|
||||||
|
obj = Stripe.deserialize(expected_body)
|
||||||
|
|
||||||
|
assert_equal obj.class, Stripe::StripeObject
|
||||||
|
assert_equal obj.id, "acc_123"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user