Skip to content

Commit aaf222f

Browse files
committed
Include client framework versions in API requests
1 parent 53ab8ed commit aaf222f

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lib/flipper/adapters/http/client.rb

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ class Client
1414

1515
HTTPS_SCHEME = "https".freeze
1616

17+
CLIENT_FRAMEWORKS = {
18+
rails: -> { Rails.version if defined?(Rails) },
19+
sinatra: -> { Sinatra::VERSION if defined?(Sinatra) },
20+
hanami: -> { Hanami::VERSION if defined?(Hanami) },
21+
}
22+
1723
attr_reader :uri, :headers
1824
attr_reader :basic_auth_username, :basic_auth_password
1925
attr_reader :read_timeout, :open_timeout, :write_timeout, :max_retries, :debug_output
@@ -93,6 +99,11 @@ def build_request(http_method, uri, headers, options)
9399
body = options[:body]
94100
request = http_method.new(uri.request_uri)
95101
request.initialize_http_header(request_headers)
102+
103+
client_frameworks.each do |framework, version|
104+
request.add_field("Client-Framework", [framework, version].join("="))
105+
end
106+
96107
request.body = body if body
97108

98109
if @basic_auth_username && @basic_auth_password
@@ -101,6 +112,10 @@ def build_request(http_method, uri, headers, options)
101112

102113
request
103114
end
115+
116+
def client_frameworks
117+
CLIENT_FRAMEWORKS.transform_values(&:call).select { |_, version| version }
118+
end
104119
end
105120
end
106121
end

spec/flipper/adapters/http_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,40 @@
9090
adapter.get(flipper[:feature_panel])
9191
end
9292

93+
it "sends framework versions" do
94+
stub_const("Rails", Struct.new(:version).new("7.1.0"))
95+
stub_const("Sinatra::VERSION", "3.1.0")
96+
stub_const("Hanami::VERSION", "0.7.2")
97+
98+
headers = {
99+
"Client-Framework" => ["rails=7.1.0", "sinatra=3.1.0", "hanami=0.7.2"]
100+
}
101+
102+
stub_request(:get, "http://app.com/flipper/features/feature_panel")
103+
.with(headers: headers)
104+
.to_return(status: 404, body: "", headers: {})
105+
106+
adapter = described_class.new(url: 'http://app.com/flipper')
107+
adapter.get(flipper[:feature_panel])
108+
end
109+
110+
it "does not send undefined framework versions" do
111+
stub_const("Rails", Struct.new(:version).new("7.1.0"))
112+
stub_const("Sinatra::VERSION", "3.1.0")
113+
114+
headers = {
115+
"Client-Framework" => ["rails=7.1.0", "sinatra=3.1.0"]
116+
}
117+
118+
stub_request(:get, "http://app.com/flipper/features/feature_panel")
119+
.with(headers: headers)
120+
.to_return(status: 404, body: "", headers: {})
121+
122+
adapter = described_class.new(url: 'http://app.com/flipper')
123+
adapter.get(flipper[:feature_panel])
124+
end
125+
126+
93127
describe "#get" do
94128
it "raises error when not successful response" do
95129
stub_request(:get, "http://app.com/flipper/features/feature_panel")

0 commit comments

Comments
 (0)