-
Notifications
You must be signed in to change notification settings - Fork 3
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
ADF-3639 Ruby: Report on client agent + versions #40
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,16 @@ def start(&block) | |
end | ||
end | ||
|
||
def request(args) | ||
def request(request_uri) | ||
raise ArgumentError, "request_uri is nil" if request_uri.nil? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we checking for nil here? We did not use to do it before plus we are not checking all edge cases such as empty string so maybe I am missing something here. Passing nil to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you're right, looking into it; URI has a def URI(uri)
if uri.is_a?(URI::Generic)
uri
elsif uri = String.try_convert(uri)
URI.parse(uri)
else
raise ArgumentError,
"bad argument (expected URI object or URI string)"
end
end |
||
retries = 0 | ||
begin | ||
re_establish_connection if @connection_is_bad | ||
net_http.request(args) | ||
|
||
uri = build_uri(request_uri) | ||
request = Net::HTTP::Get.new(uri) | ||
|
||
net_http.request(request) | ||
rescue Net::ReadTimeout, Net::OpenTimeout, SocketError => error | ||
if retries < config.retries | ||
retries += 1 | ||
|
@@ -40,6 +45,19 @@ def re_establish_connection | |
net_http.start | ||
end | ||
|
||
def build_uri(request_uri) | ||
uri = URI(request_uri) | ||
encoded_ca = URI.encode_www_form_component(config.ca) | ||
|
||
if uri.query | ||
uri.query += "&ca=#{encoded_ca}" | ||
else | ||
uri.query = "ca=#{encoded_ca}" | ||
end | ||
|
||
uri.to_s | ||
end | ||
|
||
def net_http | ||
@net_http ||= begin | ||
http = Net::HTTP.new(config.hostname, config.port, config.proxy_host, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe AddressFinder::Configuration do | ||
it 'sets the client agent with the gem version' do | ||
config = AddressFinder::Configuration.new | ||
expect(config.ca).to eq("Ruby/#{AddressFinder::VERSION}") | ||
end | ||
|
||
it 'does not allow the client agent to be modified' do | ||
config = AddressFinder::Configuration.new | ||
expect { config.ca = "CustomAgent/1.0" }.to raise_error(NoMethodError) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe AddressFinder::HTTP do | ||
let(:config) { AddressFinder::Configuration.new } | ||
let(:http) { described_class.new(config) } | ||
let(:request_uri) { "https://api.addressfinder.io?param=value" } | ||
|
||
describe '#build_uri' do | ||
it 'appends the ca parameter to the query string' do | ||
uri_with_ca = http.send(:build_uri, request_uri) | ||
expect(uri_with_ca).to include("ca=Ruby%2F#{AddressFinder::VERSION}") | ||
end | ||
|
||
it 'preserves existing query parameters' do | ||
uri_with_ca = http.send(:build_uri, request_uri) | ||
expect(uri_with_ca).to include("param=value") | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you are trying to prevent modification of the
ca
by customers but this is Ruby so everything can be changed ;) We do not care too much if customers override theca
value anyway so I think you can go back toattr_accessor :ca
here and make the code fit the existing form nicely. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just wondering if we let the customers modify the
client_agent
does that make the data collected about which clients or versions are making API requests unreliable? I thought theca
returned theAddressfinder
ruby gem version and not a ruby version? Maybe I misunderstood you?"Ruby/#{AddressFinder::VERSION}"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Customer's can talk to the API directly and in that case they will not provide
ca
anyway so we are happy with the numbers being semi accurate. We are after ballpark figures and do not care much if a customer or two do not provide accurate client agents.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay sweet, I'll make the changes 👍🏽