Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
joeltaylor committed Aug 25, 2020
1 parent 1c67bee commit 7a8fa03
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class StripeClient
#
# Takes a connection manager object for backwards compatibility only, and
# that use is DEPRECATED.
def initialize(_connection_manager = nil, api_key: nil)
def initialize(user_options = {})
@system_profiler = SystemProfiler.new
@last_request_metrics = nil
@api_key = api_key
@options = Util.my_options(user_options)
end

attr_reader :api_key
attr_reader :options

# Gets a currently active `StripeClient`. Set for the current thread when
# `StripeClient#request` is being run so that API operations being executed
Expand Down Expand Up @@ -193,7 +193,7 @@ def execute_request(method, path,
unless path.is_a?(String)

api_base ||= Stripe.api_base
api_key ||= self.api_key || Stripe.api_key
api_key ||= options.api_key
params = Util.objects_to_ids(params)

check_api_key!(api_key)
Expand Down Expand Up @@ -752,8 +752,8 @@ def self.maybe_gc_connection_managers
headers["Idempotency-Key"] ||= SecureRandom.uuid
end

headers["Stripe-Version"] = Stripe.api_version if Stripe.api_version
headers["Stripe-Account"] = Stripe.stripe_account if Stripe.stripe_account
headers["Stripe-Version"] = options.api_version if options.api_version
headers["Stripe-Account"] = options.stripe_account if options.stripe_account

user_agent = @system_profiler.user_agent
begin
Expand Down
8 changes: 8 additions & 0 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ module Util
OPTS_USER_SPECIFIED + Set[:client] - Set[:idempotency_key]
).freeze

def self.my_options(c)
OpenStruct.new(
api_key: c[:api_key] || Stripe.api_key,
api_version: c[:api_version] || Stripe.api_version,
stripe_account: c[:stripe_account] || Stripe.stripe_account
)
end

def self.objects_to_ids(obj)
case obj
when APIResource
Expand Down
15 changes: 15 additions & 0 deletions test/stripe/stripe_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@ class StripeClientTest < Test::Unit::TestCase
end
end

should "stripe_version" do
client = StripeClient.new(api_version: "2011-01-01")
account = client.accounts.retrieve("acct_1234")
assert_requested(:get,
"#{Stripe.api_base}/v1/accounts/acct_1234",
headers: { "Stripe-Version" => "2011-01-01" })
end
should "stripe_account" do
client = StripeClient.new(stripe_account: "foo")
account = client.accounts.retrieve("acct_1234")
assert_requested(:get,
"#{Stripe.api_base}/v1/accounts/acct_1234",
headers: { "Stripe-Account" => "foo" })
end

should "make expected request on a singular API resource" do
client = StripeClient.new(api_key: "sk_test_local")
account = client.accounts.retrieve("acct_1234")
Expand Down

0 comments on commit 7a8fa03

Please sign in to comment.