diff --git a/lib/unleash/client.rb b/lib/unleash/client.rb index a67d605b..c5047ae2 100644 --- a/lib/unleash/client.rb +++ b/lib/unleash/client.rb @@ -3,6 +3,7 @@ require 'unleash/metrics_reporter' require 'unleash/scheduled_executor' require 'unleash/feature_toggle' +require 'unleash/util/http' require 'logger' require 'time' @@ -135,29 +136,12 @@ def register # Send the request, if possible begin - response = http_send_request(info.to_json) + response = Unleash::Util::Http.post(Unleash.configuration.client_register_url, info.to_json) rescue Exception => e Unleash.logger.error "unable to register client with unleash server due to exception #{e.class}:'#{e}'." Unleash.logger.error "stacktrace: #{e.backtrace}" end Unleash.logger.debug "client registered: #{response}" end - - # TODO: FIXME: de-duplicate code - def http_send_request(body) - uri = URI(Unleash.configuration.client_register_url) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true if uri.scheme == 'https' - http.open_timeout = Unleash.configuration.timeout - http.read_timeout = Unleash.configuration.timeout - - headers = (Unleash.configuration.http_headers || {}).dup - headers['Content-Type'] = 'application/json' - - request = Net::HTTP::Post.new(uri.request_uri, headers) - request.body = body - - http.request(request) - end end end diff --git a/lib/unleash/metrics_reporter.rb b/lib/unleash/metrics_reporter.rb index ac539ac3..019dca9d 100755 --- a/lib/unleash/metrics_reporter.rb +++ b/lib/unleash/metrics_reporter.rb @@ -36,7 +36,7 @@ def generate_report def send Unleash.logger.debug "send() Report" - response = http_send_request(self.generate_report.to_json) + response = Unleash::Util::Http.post(Unleash.configuration.client_metrics_url, self.generate_report.to_json) if ['200', '202'].include? response.code Unleash.logger.debug "Report sent to unleash server sucessfully. Server responded with http code #{response.code}" @@ -44,23 +44,5 @@ def send Unleash.logger.error "Error when sending report to unleash server. Server responded with http code #{response.code}." end end - - private - - def http_send_request(body) - uri = URI(Unleash.configuration.client_metrics_url) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true if uri.scheme == 'https' - http.open_timeout = Unleash.configuration.timeout - http.read_timeout = Unleash.configuration.timeout - - headers = (Unleash.configuration.http_headers || {}).dup - headers['Content-Type'] = 'application/json' - - request = Net::HTTP::Post.new(uri.request_uri, headers) - request.body = body - - http.request(request) - end end end diff --git a/lib/unleash/toggle_fetcher.rb b/lib/unleash/toggle_fetcher.rb index a11a7ba3..bd9cb57f 100755 --- a/lib/unleash/toggle_fetcher.rb +++ b/lib/unleash/toggle_fetcher.rb @@ -36,7 +36,7 @@ def toggles # rename to refresh_from_server! ?? def fetch Unleash.logger.debug "fetch()" - response = http_fetch_request + response = Unleash::Util::Http.get(Unleash.configuration.fetch_toggles_url, etag) if response.code == '304' Unleash.logger.debug "No changes according to the unleash server, nothing to do." @@ -85,27 +85,6 @@ def save! private - def http_fetch_request - Unleash.logger.debug "ETag: #{self.etag}" unless self.etag.nil? - - uri = URI(Unleash.configuration.fetch_toggles_url) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true if uri.scheme == 'https' - http.open_timeout = Unleash.configuration.timeout # in seconds - http.read_timeout = Unleash.configuration.timeout # in seconds - - request = Net::HTTP::Get.new(uri.request_uri, http_headers) - http.request(request) - end - - def http_headers - headers = (Unleash.configuration.http_headers || {}).dup - headers['Content-Type'] = 'application/json' - headers['If-None-Match'] = self.etag unless self.etag.nil? - - headers - end - def synchronize_with_local_cache!(features) if self.toggle_cache != features self.toggle_lock.synchronize do diff --git a/lib/unleash/util/http.rb b/lib/unleash/util/http.rb new file mode 100644 index 00000000..25e450a9 --- /dev/null +++ b/lib/unleash/util/http.rb @@ -0,0 +1,48 @@ +require 'net/http' +require 'uri' + +module Unleash + module Util + module Http + def self.get(url, etag = nil) + uri = URI(url) + http = http_connection(uri) + + request = Net::HTTP::Get.new(uri.request_uri, http_headers(etag)) + + http.request(request) + end + + def self.post(url, body) + uri = URI(url) + http = http_connection(uri) + + request = Net::HTTP::Post.new(uri.request_uri, http_headers) + request.body = body + + http.request(request) + end + + private + + def self.http_connection(uri) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true if uri.scheme == 'https' + http.open_timeout = Unleash.configuration.timeout # in seconds + http.read_timeout = Unleash.configuration.timeout # in seconds + + http + end + + def self.http_headers(etag = nil) + Unleash.logger.debug "ETag: #{etag}" unless etag.nil? + + headers = (Unleash.configuration.http_headers || {}).dup + headers['Content-Type'] = 'application/json' + headers['If-None-Match'] = etag unless etag.nil? + + headers + end + end + end +end