Skip to content

Commit

Permalink
Allow StripeClient to be configured per instance
Browse files Browse the repository at this point in the history
This changes allows for each instance of StripeClient to have its own
configuration object instead of relying on the global config. Each
instance can be configured to override any global config values
previously set.
  • Loading branch information
joeltaylor committed Mar 30, 2021
1 parent 7d46045 commit 588dd93
Show file tree
Hide file tree
Showing 13 changed files with 438 additions and 106 deletions.
2 changes: 2 additions & 0 deletions lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ module Stripe
class << self
extend Forwardable

attr_reader :configuration

# User configurable options
def_delegators :@configuration, :api_key, :api_key=
def_delegators :@configuration, :api_version, :api_version=
Expand Down
18 changes: 10 additions & 8 deletions lib/stripe/connection_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class ConnectionManager
# by `StripeClient` to determine whether a connection manager should be
# garbage collected or not.
attr_reader :last_used
attr_reader :config

def initialize
def initialize(config = Stripe.configuration)
@config = config
@active_connections = {}
@last_used = Util.monotonic_time

Expand Down Expand Up @@ -117,17 +119,17 @@ def execute_request(method, uri, body: nil, headers: nil, query: nil)
# reused Go's default for `DefaultTransport`.
connection.keep_alive_timeout = 30

connection.open_timeout = Stripe.open_timeout
connection.read_timeout = Stripe.read_timeout
connection.open_timeout = config.open_timeout
connection.read_timeout = config.read_timeout
if connection.respond_to?(:write_timeout=)
connection.write_timeout = Stripe.write_timeout
connection.write_timeout = config.write_timeout
end

connection.use_ssl = uri.scheme == "https"

if Stripe.verify_ssl_certs
if config.verify_ssl_certs
connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
connection.cert_store = Stripe.ca_store
connection.cert_store = config.ca_store
else
connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
warn_ssl_verify_none
Expand All @@ -141,10 +143,10 @@ def execute_request(method, uri, body: nil, headers: nil, query: nil)
# out those pieces to make passing them into a new connection a little less
# ugly.
private def proxy_parts
if Stripe.proxy.nil?
if config.proxy.nil?
[nil, nil, nil, nil]
else
u = URI.parse(Stripe.proxy)
u = URI.parse(config.proxy)
[u.host, u.port, u.user, u.password]
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/stripe/oauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module OAuthOperations

def self.execute_resource_request(method, url, params, opts)
opts = Util.normalize_opts(opts)
opts[:client] ||= StripeClient.active_client
opts[:api_base] ||= Stripe.connect_base
opts[:client] ||= opts[:client] || StripeClient.active_client
opts[:api_base] ||= opts[:client].config.connect_base

super(method, url, params, opts)
end
Expand All @@ -29,7 +29,8 @@ def self.get_client_id(params = {})
end

def self.authorize_url(params = {}, opts = {})
base = opts[:connect_base] || Stripe.connect_base
client = opts[:client] || StripeClient.active_client
base = opts[:connect_base] || client.config.connect_base

path = "/oauth/authorize"
path = "/express" + path if opts[:express]
Expand Down
11 changes: 3 additions & 8 deletions lib/stripe/resources/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@ def resource_url
end

# @override To make id optional
def self.retrieve(id = ARGUMENT_NOT_PROVIDED, opts = {})
id = if id.equal?(ARGUMENT_NOT_PROVIDED)
nil
else
Util.check_string_argument!(id)
end
def self.retrieve(id = nil, opts = {})
Util.check_string_argument!(id) if id

# Account used to be a singleton, where this method's signature was
# `(opts={})`. For the sake of not breaking folks who pass in an OAuth
Expand Down Expand Up @@ -136,11 +132,10 @@ def deauthorize(client_id = nil, opts = {})
client_id: client_id,
stripe_user_id: id,
}
opts = @opts.merge(Util.normalize_opts(opts))
OAuth.deauthorize(params, opts)
end

ARGUMENT_NOT_PROVIDED = Object.new

private def serialize_additional_owners(legal_entity, additional_owners)
original_value =
legal_entity
Expand Down
3 changes: 2 additions & 1 deletion lib/stripe/resources/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ def self.create(params = {}, opts = {})
end
end

config = opts[:client]&.config || Stripe.configuration
opts = {
api_base: Stripe.uploads_base,
api_base: config.uploads_base,
content_type: MultipartEncoder::MULTIPART_FORM_DATA,
}.merge(Util.normalize_opts(opts))
super
Expand Down
Loading

0 comments on commit 588dd93

Please sign in to comment.