Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow StripeClient to be configured per instance #968

Merged
merged 1 commit into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines -48 to -53
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is a chunk of work pulled out of #954. For context on this change:

ARGUMENT_NOT_PROVIDED doesn't play very well with how we merge options. The args object in method_missing is an array and merging options when an ID is omitted results in [nil, {}] being sent to the resource, which isn't desirable.

However, I wasn't able to re-create any of the scenarios that warranted the addition of it in c269e9b and believe it's safe for removal.

#954 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for checking those. Not sure if it there was something different going on when we originally put this in, but re-reading it again, it looks safe to take it out `ARGUMENT_NOT_PROVIDED.

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