+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
require 'httpclient'
require 'thread'
@client_mtx = Mutex.new
end
- def api(resources_kind, action, data=nil, tokens={})
+ def api(resources_kind, action, data=nil, tokens={}, include_anon_token=true)
profile_checkpoint
if not @api_client
@client_mtx.synchronize do
@api_client = HTTPClient.new
+ @api_client.ssl_config.timeout = Rails.configuration.api_client_connect_timeout
+ @api_client.connect_timeout = Rails.configuration.api_client_connect_timeout
+ @api_client.receive_timeout = Rails.configuration.api_client_receive_timeout
if Rails.configuration.arvados_insecure_https
@api_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
else
# Use system CA certificates
- @api_client.ssl_config.add_trust_ca('/etc/ssl/certs')
+ ["/etc/ssl/certs/ca-certificates.crt",
+ "/etc/pki/tls/certs/ca-bundle.crt"]
+ .select { |ca_path| File.readable?(ca_path) }
+ .each { |ca_path| @api_client.ssl_config.add_trust_ca(ca_path) }
+ end
+ if Rails.configuration.api_response_compression
+ @api_client.transparent_gzip_decompression = true
end
end
end
url.sub! '/arvados/v1/../../', '/'
query = {
- 'api_token' => (tokens[:arvados_api_token] ||
- Thread.current[:arvados_api_token] ||
- ''),
'reader_tokens' => ((tokens[:reader_tokens] ||
Thread.current[:reader_tokens] ||
[]) +
- [Rails.configuration.anonymous_user_token]).to_json,
+ (include_anon_token ? [Rails.configuration.anonymous_user_token] : [])).to_json,
}
if !data.nil?
data.each do |k,v|
elsif v == false
query[k] = 0
else
- query[k] = JSON.dump(v)
+ query[k] = Oj.dump(v, mode: :compat)
end
end
else
query["_profile"] = "true"
end
- header = {"Accept" => "application/json"}
+ headers = {
+ "Accept" => "application/json",
+ "Authorization" => "OAuth2 " +
+ (tokens[:arvados_api_token] ||
+ Thread.current[:arvados_api_token] ||
+ ''),
+ "X-Request-Id" => Thread.current[:request_id] || '',
+ }
- profile_checkpoint { "Prepare request #{url} #{query[:uuid]} #{query[:where]} #{query[:filters]} #{query[:order]}" }
+ profile_checkpoint { "Prepare request #{query["_method"] or "POST"} #{url} #{query[:uuid]} #{query.inspect[0,256]}" }
msg = @client_mtx.synchronize do
begin
- @api_client.post(url, query, header: header)
+ @api_client.post(url, query, headers)
rescue => exception
raise NoApiResponseException.new(url, exception)
end
end
profile_checkpoint 'API transaction'
+ if @@profiling_enabled
+ if msg.headers['X-Runtime']
+ Rails.logger.info "API server: #{msg.headers['X-Runtime']} runtime reported"
+ end
+ Rails.logger.info "Content-Encoding #{msg.headers['Content-Encoding'].inspect}, Content-Length #{msg.headers['Content-Length'].inspect}, actual content size #{msg.content.size}"
+ end
begin
resp = Oj.load(msg.content, :symbol_keys => true)