Skip to content

Commit 00b4f86

Browse files
committed
Update StripeClient to optionally take app_info in parameter
This app_info will take precedence over the globally defined Stripe.app_info
1 parent 5304640 commit 00b4f86

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

Diff for: lib/stripe/api_requestor.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def initialize(config_arg = {})
3030
else
3131
raise ArgumentError, "Can't handle argument: #{config_arg}"
3232
end
33+
34+
@app_info = config.app_info
3335
end
3436

3537
attr_reader :config, :options
@@ -918,7 +920,7 @@ def self.maybe_gc_connection_managers
918920

919921
private def request_headers(method, api_mode, req_opts)
920922
user_agent = "Stripe/#{api_mode} RubyBindings/#{Stripe::VERSION}"
921-
user_agent += " " + format_app_info(Stripe.app_info) unless Stripe.app_info.nil?
923+
user_agent += " " + format_app_info(fetch_app_info) unless fetch_app_info.nil?
922924

923925
headers = {
924926
"User-Agent" => user_agent,
@@ -963,6 +965,10 @@ def self.maybe_gc_connection_managers
963965
headers.update(req_opts[:headers])
964966
end
965967

968+
private def fetch_app_info
969+
@app_info || Stripe.app_info
970+
end
971+
966972
private def log_request(context, num_retries)
967973
Util.log_info("Request to Stripe API",
968974
account: context.account,
@@ -1090,7 +1096,7 @@ def user_agent
10901096
"(#{RUBY_RELEASE_DATE})"
10911097

10921098
{
1093-
application: Stripe.app_info,
1099+
application: fetch_app_info,
10941100
bindings_version: Stripe::VERSION,
10951101
lang: "ruby",
10961102
lang_version: lang_version,

Diff for: lib/stripe/stripe_client.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def initialize(api_key, # rubocop:todo Metrics/ParameterLists
2323
uploads_base: nil,
2424
connect_base: nil,
2525
meter_events_base: nil,
26-
client_id: nil)
26+
client_id: nil,
27+
app_info: nil,)
2728
unless api_key
2829
raise AuthenticationError, "No API key provided. " \
2930
'Set your API key using "client = Stripe::StripeClient.new(<API-KEY>)". ' \
@@ -42,6 +43,7 @@ def initialize(api_key, # rubocop:todo Metrics/ParameterLists
4243
connect_base: connect_base,
4344
meter_events_base: meter_events_base,
4445
client_id: client_id,
46+
app_info: app_info
4547
}.reject { |_k, v| v.nil? }
4648

4749
config = StripeConfiguration.client_init(config_opts)

Diff for: test/stripe/api_requestor_test.rb

+39
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,45 @@ class RequestorTest < Test::Unit::TestCase
834834
Stripe.app_info = old
835835
end
836836
end
837+
838+
should "send app_info if set on the APIRequestor's config" do
839+
begin
840+
old = Stripe.app_info
841+
Stripe.app_info = nil
842+
843+
custom_config = Stripe::StripeConfiguration.setup do |c|
844+
c.app_info = {
845+
name: "MyAwesomePluginInConfig",
846+
partner_id: "partner_4567",
847+
url: "https://myawesomeplugininconfig.info",
848+
version: "4.5.6",
849+
}
850+
end
851+
852+
stub_request(:post, "#{Stripe::DEFAULT_API_BASE}/v1/account")
853+
.with do |req|
854+
expect_user_agent = "Stripe/v1 RubyBindings/#{Stripe::VERSION}" \
855+
"MyAwesomePluginInConfig/4.5.6 (https://myawesomeplugininconfig.info)"
856+
assert_equal expect_user_agent, req.headers["User-Agent"]
857+
858+
data = JSON.parse(req.headers["X-Stripe-Client-User-Agent"], symbolize_names: true)
859+
assert_equal({
860+
name: "MyAwesomePluginInConfig",
861+
partner_id: "partner_4567",
862+
url: "https://myawesomeplugininconfig.info",
863+
version: "4.5.6",
864+
}, data[:application])
865+
866+
true
867+
end
868+
.to_return(body: JSON.generate(object: "account"))
869+
870+
client = APIRequestor.new(custom_config)
871+
client.execute_request(:post, "/v1/account", :api)
872+
ensure
873+
Stripe.app_info = old
874+
end
875+
end
837876
end
838877

839878
context "error handling" do

Diff for: test/stripe/stripe_client_test.rb

+47
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,20 @@ class StripeClientTest < Test::Unit::TestCase
2121
@orig_stripe_account = Stripe.stripe_account
2222
@orig_open_timeout = Stripe.open_timeout
2323
@orig_api_version = Stripe.api_version
24+
@orig_app_info = Stripe.app_info
2425

2526
Stripe.api_key = "DONT_USE_THIS_KEY"
2627
Stripe.stripe_account = "DONT_USE_THIS_ACCOUNT"
2728
Stripe.open_timeout = 30_000
29+
Stripe.app_info = nil
2830
end
2931

3032
teardown do
3133
Stripe.api_key = @orig_api_key
3234
Stripe.stripe_account = @orig_stripe_account
3335
Stripe.open_timeout = @orig_open_timeout
3436
Stripe.api_version = @orig_api_version
37+
Stripe.app_info = @orig_app_info
3538
end
3639

3740
should "use default config options" do
@@ -112,6 +115,50 @@ class StripeClientTest < Test::Unit::TestCase
112115

113116
refute_equal APIRequestor.active_requestor, client.instance_variable_get(:@requestor)
114117
end
118+
119+
should "use client app_info over global app_info" do
120+
Stripe.app_info = { name: "global_app", version: "1.2.3" }
121+
client_app_info = { name: "client_app", version: "4.5.6" }
122+
client = StripeClient.new("test_123", app_info: client_app_info)
123+
124+
req = nil
125+
stub_request(:get, "#{Stripe::DEFAULT_API_BASE}/v1/customers/cus_123")
126+
.with { |request| req = request }
127+
.to_return(body: JSON.generate(object: "customer"))
128+
129+
client.v1.customers.retrieve("cus_123")
130+
131+
assert_match(/client_app\/4\.5\.6/, req.headers["User-Agent"])
132+
assert_no_match(/global_app/, req.headers["User-Agent"])
133+
end
134+
135+
should "fall back to global app_info when client app_info not provided" do
136+
Stripe.app_info = { name: "global_app", version: "1.2.3" }
137+
client = StripeClient.new("test_123")
138+
139+
req = nil
140+
stub_request(:get, "#{Stripe::DEFAULT_API_BASE}/v1/customers/cus_123")
141+
.with { |request| req = request }
142+
.to_return(body: JSON.generate(object: "customer"))
143+
144+
client.v1.customers.retrieve("cus_123")
145+
146+
assert_match(%r{global_app/1\.2\.3}, req.headers["User-Agent"])
147+
end
148+
149+
should "work with no app_info set" do
150+
Stripe.app_info = nil
151+
client = StripeClient.new("test_123")
152+
153+
req = nil
154+
stub_request(:get, "#{Stripe::DEFAULT_API_BASE}/v1/customers/cus_123")
155+
.with { |request| req = request }
156+
.to_return(body: JSON.generate(object: "customer"))
157+
158+
client.v1.customers.retrieve("cus_123")
159+
160+
assert_no_match(%r{app/}, req.headers["User-Agent"])
161+
end
115162
end
116163

117164
context "#request" do

0 commit comments

Comments
 (0)