From 72930769a8c6280de05c640db49e0df87053b8d8 Mon Sep 17 00:00:00 2001 From: Josef Strzibny Date: Fri, 27 Nov 2026 14:54:16 +0100 Subject: [PATCH] Move client tracking from the source parameter to the User-Agent header Send the library version, Ruby runtime, and platform in the User-Agent header, e.g. serpapi-ruby/1.0.3 ruby/3.4.1 (darwin; arm64), instead of the source query parameter. Requests previously used the http.rb default User-Agent. Co-authored-by: Claude Opus 5.5 (max effort) --- CHANGELOG.md | 1 + lib/serpapi/client.rb | 19 +++++++--- spec/serpapi/client/error_handling_spec.rb | 2 +- spec/serpapi/client/extract_api_spec.rb | 2 +- spec/serpapi/client/image_api_spec.rb | 2 +- spec/serpapi/client/user_agent_spec.rb | 44 ++++++++++++++++++++++ 6 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 spec/serpapi/client/user_agent_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 48136fe..057c028 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # Changelog + * [Unreleased] Identify the library, Ruby runtime, and platform in the `User-Agent` header (e.g. `serpapi-ruby/1.0.3 ruby/3.4.1 (darwin; arm64)`) instead of the `source` query parameter * [Unreleased] **BREAKING:** JSON responses with HTTP 200 and a top-level `error` key no longer raise `SerpApiError`. SerpApi returns searches with empty results this way (e.g. "Google hasn't returned any results for this query."), so `search`, `search_archive`, and `extract` now return them as a Hash with the message in `results[:error]`. `SerpApiError` is raised only for non-200 responses. Code that rescued `SerpApiError` to detect empty results must check `results[:error]` instead. With `symbolize_names: false`, these responses were already returned, with the message in `results["error"]` #17 * [Unreleased] Set `SerpApiError#serpapi_error` and `#search_id` when using `symbolize_names: false` * [Unreleased] Raise `SerpApiError` instead of `TypeError` for non-200 responses whose `error` or `search_metadata.id` is not a String diff --git a/lib/serpapi/client.rb b/lib/serpapi/client.rb index 433eed2..d076dd7 100644 --- a/lib/serpapi/client.rb +++ b/lib/serpapi/client.rb @@ -29,6 +29,13 @@ class Client # HTTP.rb client :socket + # User-Agent sent with every request so SerpApi can report library usage by version, Ruby runtime, and platform. + # + # @return [String] e.g. "serpapi-ruby/1.0.3 ruby/3.4.1 (darwin; arm64)" + def self.user_agent + "serpapi-ruby/#{SerpApi::VERSION} #{RUBY_ENGINE}/#{RUBY_ENGINE_VERSION} (#{Gem::Platform.local.os}; #{Gem::Platform.local.cpu})" + end + # Constructor # The `Serpapi::Client` constructor takes a hash of options as input. # @@ -91,16 +98,13 @@ def initialize(params = {}) # set default query parameters @params = params.clone || {} - # track ruby library as a client for statistic purpose - @params[:source] = 'serpapi-ruby:' << SerpApi::VERSION - # ensure default parameter would not be modified later @params.freeze # create connection socket return unless persistent? - @socket = HTTP.timeout(timeout).persistent("https://#{BACKEND}") + @socket = http_client.persistent("https://#{BACKEND}") end # perform a search using SerpApi.com @@ -341,11 +345,16 @@ def post(endpoint, decoder = :json, params = {}, form = {}) end def execute_request(method, endpoint, options) - client = persistent? ? @socket : HTTP.timeout(timeout) + client = persistent? ? @socket : http_client url = persistent? ? endpoint : "https://#{BACKEND}#{endpoint}" client.public_send(method, url, **options) end + # @return [HTTP::Client] HTTP client with the request timeout and the library User-Agent + def http_client + HTTP.timeout(timeout).headers('User-Agent' => self.class.user_agent) + end + def response_decoder(response, default) content_type = response.headers['Content-Type'].to_s.split(';').first CONTENT_TYPE_DECODERS.fetch(content_type, default) diff --git a/spec/serpapi/client/error_handling_spec.rb b/spec/serpapi/client/error_handling_spec.rb index 77c1780..dc261ef 100644 --- a/spec/serpapi/client/error_handling_spec.rb +++ b/spec/serpapi/client/error_handling_spec.rb @@ -32,7 +32,7 @@ def raw_response(status, body, content_type) end before do - allow(HTTP).to receive(:timeout).and_return(double(persistent: socket)) + allow(HTTP).to receive(:timeout).and_return(double(headers: double(persistent: socket))) end describe 'searches with empty results' do diff --git a/spec/serpapi/client/extract_api_spec.rb b/spec/serpapi/client/extract_api_spec.rb index d0618cf..babb958 100644 --- a/spec/serpapi/client/extract_api_spec.rb +++ b/spec/serpapi/client/extract_api_spec.rb @@ -8,7 +8,7 @@ def sent_params http = double response = double(status: 200, body: '{}', headers: { 'Content-Type' => 'application/json' }) sent = nil - allow(HTTP).to receive(:timeout).and_return(http) + allow(HTTP).to receive(:timeout).and_return(double(headers: http)) allow(http).to receive(:get) do |_url, params:| sent = params response diff --git a/spec/serpapi/client/image_api_spec.rb b/spec/serpapi/client/image_api_spec.rb index 3503479..c0cb9de 100644 --- a/spec/serpapi/client/image_api_spec.rb +++ b/spec/serpapi/client/image_api_spec.rb @@ -13,7 +13,7 @@ let(:client) { SerpApi::Client.new(api_key: api_key) } before do - allow(HTTP).to receive(:timeout).and_return(double(persistent: socket)) + allow(HTTP).to receive(:timeout).and_return(double(headers: double(persistent: socket))) allow(response).to receive(:flush) end diff --git a/spec/serpapi/client/user_agent_spec.rb b/spec/serpapi/client/user_agent_spec.rb new file mode 100644 index 0000000..0747ffc --- /dev/null +++ b/spec/serpapi/client/user_agent_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +describe 'SerpApi client usage reporting' do + describe '.user_agent' do + { + ['ruby', '3.4.1', 'arm64-darwin-23'] => 'serpapi-ruby/1.0.3 ruby/3.4.1 (darwin; arm64)', + ['ruby', '3.3.6', 'x86_64-linux'] => 'serpapi-ruby/1.0.3 ruby/3.3.6 (linux; x86_64)', + ['ruby', '3.4.1', 'aarch64-linux-musl'] => 'serpapi-ruby/1.0.3 ruby/3.4.1 (linux; aarch64)', + ['ruby', '3.4.1', 'x64-mingw-ucrt'] => 'serpapi-ruby/1.0.3 ruby/3.4.1 (mingw; x64)', + ['jruby', '9.4.9.0', 'universal-java-21'] => 'serpapi-ruby/1.0.3 jruby/9.4.9.0 (java; universal)' + }.each do |(engine, engine_version, platform), user_agent| + it "is #{user_agent} for #{engine} #{engine_version} on #{platform}" do + stub_const('SerpApi::VERSION', '1.0.3') + stub_const('RUBY_ENGINE', engine) + stub_const('RUBY_ENGINE_VERSION', engine_version) + allow(Gem::Platform).to receive(:local).and_return(Gem::Platform.new(platform)) + + expect(SerpApi::Client.user_agent).to eq(user_agent) + end + end + end + + describe 'requests' do + let(:response) do + double(status: 200, body: '{}', headers: { 'Content-Type' => 'application/json' }, flush: nil) + end + + [true, false].each do |persistent| + it "send the User-Agent header instead of a source parameter when persistent is #{persistent}" do + client = SerpApi::Client.new(api_key: 'dummy_secret_key', persistent: persistent) + sent = nil + allow_any_instance_of(HTTP::Client).to receive(:perform) do |_http, request, _options| + sent = request + response + end + + client.search(q: 'Coffee') + + expect(sent.headers['User-Agent']).to eq(SerpApi::Client.user_agent) + expect(URI.decode_www_form(sent.uri.query).to_h).to eq('api_key' => 'dummy_secret_key', 'q' => 'Coffee') + end + end + end +end