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

Include client framework versions in API requests #780

Merged
merged 3 commits into from
Dec 4, 2023
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
15 changes: 15 additions & 0 deletions lib/flipper/adapters/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class Client

HTTPS_SCHEME = "https".freeze

CLIENT_FRAMEWORKS = {
rails: -> { Rails.version if defined?(Rails) },
sinatra: -> { Sinatra::VERSION if defined?(Sinatra) },
hanami: -> { Hanami::VERSION if defined?(Hanami) },
}

attr_reader :uri, :headers
attr_reader :basic_auth_username, :basic_auth_password
attr_reader :read_timeout, :open_timeout, :write_timeout, :max_retries, :debug_output
Expand Down Expand Up @@ -93,6 +99,11 @@ def build_request(http_method, uri, headers, options)
body = options[:body]
request = http_method.new(uri.request_uri)
request.initialize_http_header(request_headers)

client_frameworks.each do |framework, version|
request.add_field("Client-Framework", [framework, version].join("="))
end

request.body = body if body

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

request
end

def client_frameworks
CLIENT_FRAMEWORKS.transform_values { |detect| detect.call rescue nil }.select { |_, version| version }
end
end
end
end
Expand Down
34 changes: 34 additions & 0 deletions spec/flipper/adapters/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,40 @@
adapter.get(flipper[:feature_panel])
end

it "sends framework versions" do
stub_const("Rails", Struct.new(:version).new("7.1.0"))
stub_const("Sinatra::VERSION", "3.1.0")
stub_const("Hanami::VERSION", "0.7.2")

headers = {
"Client-Framework" => ["rails=7.1.0", "sinatra=3.1.0", "hanami=0.7.2"]
}

stub_request(:get, "http://app.com/flipper/features/feature_panel")
.with(headers: headers)
.to_return(status: 404, body: "", headers: {})

adapter = described_class.new(url: 'http://app.com/flipper')
adapter.get(flipper[:feature_panel])
end

it "does not send undefined framework versions" do
stub_const("Rails", Struct.new(:version).new("7.1.0"))
stub_const("Sinatra::VERSION", "3.1.0")

headers = {
"Client-Framework" => ["rails=7.1.0", "sinatra=3.1.0"]
}

stub_request(:get, "http://app.com/flipper/features/feature_panel")
.with(headers: headers)
.to_return(status: 404, body: "", headers: {})

adapter = described_class.new(url: 'http://app.com/flipper')
adapter.get(flipper[:feature_panel])
end


describe "#get" do
it "raises error when not successful response" do
stub_request(:get, "http://app.com/flipper/features/feature_panel")
Expand Down
4 changes: 2 additions & 2 deletions spec/flipper/cloud/telemetry/submitter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
'User-Agent' => "Flipper HTTP Adapter v#{Flipper::VERSION}",
}
stub_request(:post, "https://www.flippercloud.io/adapter/telemetry").
with { |request|
with(headers: expected_headers) { |request|
gunzipped = Flipper::Typecast.from_gzip(request.body)
body = Flipper::Typecast.from_json(gunzipped)
body == expected_body && request.headers == expected_headers
body == expected_body
}.to_return(status: 200, body: "{}", headers: {})
subject.call(enabled_metrics)
end
Expand Down