X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/27f5c1635d56c3f3cb6c5ef069c28db939eec2a1..61d399a57fd1e867cc243a0dab0fad73d9689734:/apps/workbench/app/models/arvados_api_client.rb diff --git a/apps/workbench/app/models/arvados_api_client.rb b/apps/workbench/app/models/arvados_api_client.rb index c6d8720c92..aa3269a43d 100644 --- a/apps/workbench/app/models/arvados_api_client.rb +++ b/apps/workbench/app/models/arvados_api_client.rb @@ -2,26 +2,94 @@ require 'httpclient' require 'thread' class ArvadosApiClient - class NotLoggedInException < StandardError + class ApiError < StandardError + attr_reader :api_response, :api_response_s, :api_status, :request_url + + def initialize(request_url, errmsg) + @request_url = request_url + @api_response ||= {} + errors = @api_response[:errors] + if not errors.is_a?(Array) + @api_response[:errors] = [errors || errmsg] + end + super(errmsg) + end end - class InvalidApiResponseException < StandardError + + class NoApiResponseException < ApiError + def initialize(request_url, exception) + @api_response_s = exception.to_s + super(request_url, + "#{exception.class.to_s} error connecting to API server") + end end - @@client_mtx = Mutex.new - @@api_client = nil + class InvalidApiResponseException < ApiError + def initialize(request_url, api_response) + @api_status = api_response.status_code + @api_response_s = api_response.content + super(request_url, "Unparseable response from API server") + end + end + + class ApiErrorResponseException < ApiError + def initialize(request_url, api_response) + @api_status = api_response.status_code + @api_response_s = api_response.content + @api_response = Oj.load(@api_response_s, :symbol_keys => true) + errors = @api_response[:errors] + if errors.respond_to?(:join) + errors = errors.join("\n\n") + else + errors = errors.to_s + end + super(request_url, "#{errors} [API: #{@api_status}]") + end + end + + class AccessForbiddenException < ApiErrorResponseException; end + class NotFoundException < ApiErrorResponseException; end + class NotLoggedInException < ApiErrorResponseException; end + + ERROR_CODE_CLASSES = { + 401 => NotLoggedInException, + 403 => AccessForbiddenException, + 404 => NotFoundException, + } + @@profiling_enabled = Rails.configuration.profiling_enabled + @@discovery = nil + + # An API client object suitable for handling API requests on behalf + # of the current thread. + def self.new_or_current + # If this thread doesn't have an API client yet, *or* this model + # has been reloaded since the existing client was created, create + # a new client. Otherwise, keep using the latest client created in + # the current thread. + unless Thread.current[:arvados_api_client].andand.class == self + Thread.current[:arvados_api_client] = new + end + Thread.current[:arvados_api_client] + end + + def initialize *args + @api_client = nil + @client_mtx = Mutex.new + end + + def api(resources_kind, action, data=nil, tokens={}) - def api(resources_kind, action, data=nil) profile_checkpoint - @@client_mtx.synchronize do - if not @@api_client - @@api_client = HTTPClient.new + if not @api_client + @client_mtx.synchronize do + @api_client = HTTPClient.new if Rails.configuration.arvados_insecure_https - @@api_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE + @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') + @api_client.ssl_config.add_trust_ca('/etc/ssl/certs') end end end @@ -33,8 +101,13 @@ class ArvadosApiClient url.sub! '/arvados/v1/../../', '/' query = { - 'api_token' => Thread.current[:arvados_api_token] || '', - 'reader_tokens' => (Thread.current[:reader_tokens] || []).to_json, + '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, } if !data.nil? data.each do |k,v| @@ -51,37 +124,40 @@ class ArvadosApiClient else query["_method"] = "GET" end + if @@profiling_enabled query["_profile"] = "true" end header = {"Accept" => "application/json"} - - profile_checkpoint { "Prepare request #{url} #{query[:uuid]} #{query[:where]} #{query[:filters]}" } - msg = @@api_client.post(url, - query, - header: header) - profile_checkpoint 'API transaction' - - if msg.status_code == 401 - raise NotLoggedInException.new + if Rails.configuration.include_accept_encoding_header_in_api_requests + header["Accept-Encoding"] = "gzip, deflate" end - json = msg.content + profile_checkpoint { "Prepare request #{url} #{query[:uuid]} #{query[:where]} #{query[:filters]} #{query[:order]}" } + msg = @client_mtx.synchronize do + begin + @api_client.post(url, query, header: header) + rescue => exception + raise NoApiResponseException.new(url, exception) + end + end + profile_checkpoint 'API transaction' begin - resp = Oj.load(json, :symbol_keys => true) + resp = Oj.load(msg.content, :symbol_keys => true) rescue Oj::ParseError - raise InvalidApiResponseException.new json + resp = nil end + if not resp.is_a? Hash - raise InvalidApiResponseException.new json - end - if msg.status_code != 200 - errors = resp[:errors] - errors = errors.join("\n\n") if errors.is_a? Array - raise "#{errors} [API: #{msg.status_code}]" + raise InvalidApiResponseException.new(url, msg) + elsif msg.status_code != 200 + error_class = ERROR_CODE_CLASSES.fetch(msg.status_code, + ApiErrorResponseException) + raise error_class.new(url, msg) end + if resp[:_profile] Rails.logger.info "API client: " \ "#{resp.delete(:_profile)[:request_time]} request_time" @@ -147,6 +223,7 @@ class ArvadosApiClient CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s) }.join('&') end + uri end def arvados_logout_url(params={}) @@ -158,7 +235,7 @@ class ArvadosApiClient end def discovery - @discovery ||= api '../../discovery/v1/apis/arvados/v1/rest', '' + @@discovery ||= api '../../discovery/v1/apis/arvados/v1/rest', '' end def kind_class(kind) @@ -169,6 +246,10 @@ class ArvadosApiClient resource_class.to_s.underscore end + def self.class_kind(resource_class) + resource_class.to_s.underscore + end + protected def profile_checkpoint label=nil return if !@@profiling_enabled