diff --git a/lib/ontologies_api_client/config.rb b/lib/ontologies_api_client/config.rb index 5909729..c376a16 100644 --- a/lib/ontologies_api_client/config.rb +++ b/lib/ontologies_api_client/config.rb @@ -38,7 +38,7 @@ def config(&block) def config_connection(options = {}) return if @settings_run_connection store = options[:cache_store] || ActiveSupport::Cache::RedisCacheStore.new - @settings.conn = faraday_connection(@settings.rest_url, @settings.apikey, store) + @settings.conn = faraday_connection(@settings.rest_url, @settings.apikey, store, current_portal: true) @settings.federated_conn = @settings.federated_portals.map do |portal_name, portal_info| [portal_name, faraday_connection(portal_info[:api], portal_info[:apikey], store)] end.to_h @@ -51,21 +51,24 @@ def connection_configured? end private - def faraday_connection(url, apikey, store) + def faraday_connection(url, apikey, store, current_portal: false) Faraday.new(url.to_s.chomp('/')) do |faraday| + if @settings.enable_long_request_log require_relative 'middleware/faraday-long-requests' faraday.use :long_requests end - require_relative 'middleware/faraday-user-apikey' - faraday.use :user_apikey + if current_portal + require_relative 'middleware/faraday-user-apikey' + faraday.use :user_apikey - require_relative 'middleware/faraday-slices' - faraday.use :ncbo_slices + require_relative 'middleware/faraday-slices' + faraday.use :ncbo_slices - require_relative 'middleware/faraday-last-updated' - faraday.use :last_updated + require_relative 'middleware/faraday-last-updated' + faraday.use :last_updated + end if @settings.cache begin diff --git a/lib/ontologies_api_client/request_federation.rb b/lib/ontologies_api_client/request_federation.rb index 458af90..ff1c9e3 100644 --- a/lib/ontologies_api_client/request_federation.rb +++ b/lib/ontologies_api_client/request_federation.rb @@ -11,11 +11,13 @@ def self.included(base) module ClassMethods def federated_get(params = {}, &link) portals = request_portals(params) + main_thread_locals = Thread.current.keys.map { |key| [key, Thread.current[key]] }.to_h connections = Parallel.map(portals, in_threads: portals.size) do |conn| + main_thread_locals.each { |key, value| Thread.current[key] = value } begin HTTP.get(link.call(conn.url_prefix.to_s.chomp('/')), params, connection: conn) - rescue StandardError => e + rescue Exception => e [OpenStruct.new(errors: "Problem retrieving #{link.call(conn.url_prefix.to_s.chomp('/')) || conn.url_prefix}")] end end