From ffe5948699cefc62220397fa03a3ca82aa325147 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Wed, 30 Sep 2020 11:37:57 +0200 Subject: [PATCH] Enhancement: Added header support for UserAgent requests. --- lib/user_agent.rb | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/user_agent.rb b/lib/user_agent.rb index 9b3c79466..6f3fa0eef 100644 --- a/lib/user_agent.rb +++ b/lib/user_agent.rb @@ -48,7 +48,10 @@ returns http = get_http(uri, options) # prepare request - request = Net::HTTP::Get.new(uri, { 'User-Agent' => 'Zammad User Agent' }) + request = Net::HTTP::Get.new(uri) + + # set headers + request = set_headers(request, options) # http basic auth (if needed) request = set_basic_auth(request, options) @@ -104,7 +107,10 @@ returns http = get_http(uri, options) # prepare request - request = Net::HTTP::Post.new(uri, { 'User-Agent' => 'Zammad User Agent' }) + request = Net::HTTP::Post.new(uri) + + # set headers + request = set_headers(request, options) # set params request = set_params(request, params, options) @@ -159,7 +165,10 @@ returns http = get_http(uri, options) # prepare request - request = Net::HTTP::Put.new(uri, { 'User-Agent' => 'Zammad User Agent' }) + request = Net::HTTP::Put.new(uri) + + # set headers + request = set_headers(request, options) # set params request = set_params(request, params, options) @@ -210,7 +219,10 @@ returns http = get_http(uri, options) # prepare request - request = Net::HTTP::Delete.new(uri, { 'User-Agent' => 'Zammad User Agent' }) + request = Net::HTTP::Delete.new(uri) + + # set headers + request = set_headers(request, options) # http basic auth (if needed) request = set_basic_auth(request, options) @@ -334,6 +346,17 @@ returns request end + def self.set_headers(request, options) + defaults = { 'User-Agent' => 'Zammad User Agent' } + headers = defaults.merge(options.fetch(:headers, {})) + + headers.each do |header, value| + request[header] = value + end + + request + end + def self.log(url, request, response, options) return if !options[:log]