From 7a8fa037f5e770fa493fd4765964752bc9021e44 Mon Sep 17 00:00:00 2001 From: Joel Taylor Date: Sun, 9 Aug 2020 11:49:49 -0700 Subject: [PATCH] WIP --- lib/stripe/stripe_client.rb | 12 ++++++------ lib/stripe/util.rb | 8 ++++++++ test/stripe/stripe_client_test.rb | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/stripe/stripe_client.rb b/lib/stripe/stripe_client.rb index a247b5136..95233447a 100644 --- a/lib/stripe/stripe_client.rb +++ b/lib/stripe/stripe_client.rb @@ -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 @@ -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) @@ -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 diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index 389c0adfa..497d10274 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -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 diff --git a/test/stripe/stripe_client_test.rb b/test/stripe/stripe_client_test.rb index c97681c81..55d379be4 100644 --- a/test/stripe/stripe_client_test.rb +++ b/test/stripe/stripe_client_test.rb @@ -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")