5 class ApiError < StandardError
6 attr_reader :api_response, :api_response_s, :api_status, :request_url
8 def initialize(request_url, errmsg)
9 @request_url = request_url
15 class NoApiResponseException < ApiError
16 def initialize(request_url, exception)
17 @api_response_s = exception.to_s
19 "#{exception.class.to_s} error connecting to API server")
23 class InvalidApiResponseException < ApiError
24 def initialize(request_url, api_response)
25 @api_status = api_response.status_code
26 @api_response_s = api_response.content
27 super(request_url, "Unparseable response from API server")
31 class ApiErrorResponseException < ApiError
32 def initialize(request_url, api_response)
33 @api_status = api_response.status_code
34 @api_response_s = api_response.content
35 @api_response = Oj.load(@api_response_s, :symbol_keys => true)
36 errors = @api_response[:errors]
37 if errors.respond_to?(:join)
38 errors = errors.join("\n\n")
42 super(request_url, "#{errors} [API: #{@api_status}]")
46 class AccessForbiddenException < ApiErrorResponseException; end
47 class NotFoundException < ApiErrorResponseException; end
48 class NotLoggedInException < ApiErrorResponseException; end
50 ERROR_CODE_CLASSES = {
51 401 => NotLoggedInException,
52 403 => AccessForbiddenException,
53 404 => NotFoundException,
56 @@profiling_enabled = Rails.configuration.profiling_enabled
59 # An API client object suitable for handling API requests on behalf
60 # of the current thread.
61 def self.new_or_current
62 # If this thread doesn't have an API client yet, *or* this model
63 # has been reloaded since the existing client was created, create
64 # a new client. Otherwise, keep using the latest client created in
66 unless Thread.current[:arvados_api_client].andand.class == self
67 Thread.current[:arvados_api_client] = new
69 Thread.current[:arvados_api_client]
74 @client_mtx = Mutex.new
77 def api(resources_kind, action, data=nil)
81 @client_mtx.synchronize do
82 @api_client = HTTPClient.new
83 if Rails.configuration.arvados_insecure_https
84 @api_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
86 # Use system CA certificates
87 @api_client.ssl_config.add_trust_ca('/etc/ssl/certs')
92 resources_kind = class_kind(resources_kind).pluralize if resources_kind.is_a? Class
93 url = "#{self.arvados_v1_base}/#{resources_kind}#{action}"
95 # Clean up /arvados/v1/../../discovery/v1 to /discovery/v1
96 url.sub! '/arvados/v1/../../', '/'
99 'api_token' => Thread.current[:arvados_api_token] || '',
100 'reader_tokens' => (Thread.current[:reader_tokens] || []).to_json,
104 if v.is_a? String or v.nil?
111 query[k] = JSON.dump(v)
115 query["_method"] = "GET"
117 if @@profiling_enabled
118 query["_profile"] = "true"
121 header = {"Accept" => "application/json"}
123 profile_checkpoint { "Prepare request #{url} #{query[:uuid]} #{query[:where]} #{query[:filters]}" }
124 msg = @client_mtx.synchronize do
126 @api_client.post(url, query, header: header)
128 raise NoApiResponseException.new(url, exception)
131 profile_checkpoint 'API transaction'
134 resp = Oj.load(msg.content, :symbol_keys => true)
135 rescue Oj::ParseError
138 if not resp.is_a? Hash
139 raise InvalidApiResponseException.new(url, msg)
140 elsif msg.status_code != 200
141 error_class = ERROR_CODE_CLASSES.fetch(msg.status_code, ApiError)
142 raise error_class.new(url, msg)
146 Rails.logger.info "API client: " \
147 "#{resp.delete(:_profile)[:request_time]} request_time"
149 profile_checkpoint 'Parse response'
153 def self.patch_paging_vars(ary, items_available, offset, limit, links=nil)
155 (class << ary; self; end).class_eval { attr_accessor :items_available }
156 ary.items_available = items_available
159 (class << ary; self; end).class_eval { attr_accessor :offset }
163 (class << ary; self; end).class_eval { attr_accessor :limit }
167 (class << ary; self; end).class_eval { attr_accessor :links }
173 def unpack_api_response(j, kind=nil)
174 if j.is_a? Hash and j[:items].is_a? Array and j[:kind].match(/(_list|List)$/)
175 ary = j[:items].collect { |x| unpack_api_response x, x[:kind] }
176 links = ArvadosResourceList.new Link
177 links.results = (j[:links] || []).collect do |x|
178 unpack_api_response x, x[:kind]
180 self.class.patch_paging_vars(ary, j[:items_available], j[:offset], j[:limit], links)
181 elsif j.is_a? Hash and (kind || j[:kind])
182 oclass = self.kind_class(kind || j[:kind])
185 childkind = j["#{k.to_s}_kind".to_sym]
187 j[k] = self.unpack_api_response(j[k], childkind)
190 oclass.new.private_reload(j)
199 def arvados_login_url(params={})
200 if Rails.configuration.respond_to? :arvados_login_base
201 uri = Rails.configuration.arvados_login_base
203 uri = self.arvados_v1_base.sub(%r{/arvados/v\d+.*}, '/login')
206 uri += '?' << params.collect { |k,v|
207 CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s)
212 def arvados_logout_url(params={})
213 arvados_login_url(params).sub('/login','/logout')
217 Rails.configuration.arvados_v1_base
221 @@discovery ||= api '../../discovery/v1/apis/arvados/v1/rest', ''
225 kind.match(/^arvados\#(.+?)(_list|List)?$/)[1].pluralize.classify.constantize rescue nil
228 def class_kind(resource_class)
229 resource_class.to_s.underscore
232 def self.class_kind(resource_class)
233 resource_class.to_s.underscore
237 def profile_checkpoint label=nil
238 return if !@@profiling_enabled
239 label = yield if block_given?
241 if label and @profile_t0
242 Rails.logger.info "API client: #{t - @profile_t0} #{label}"