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
11 errors = @api_response[:errors]
12 if not errors.is_a?(Array)
13 @api_response[:errors] = [errors || errmsg]
19 class NoApiResponseException < ApiError
20 def initialize(request_url, exception)
21 @api_response_s = exception.to_s
23 "#{exception.class.to_s} error connecting to API server")
27 class InvalidApiResponseException < ApiError
28 def initialize(request_url, api_response)
29 @api_status = api_response.status_code
30 @api_response_s = api_response.content
31 super(request_url, "Unparseable response from API server")
35 class ApiErrorResponseException < ApiError
36 def initialize(request_url, api_response)
37 @api_status = api_response.status_code
38 @api_response_s = api_response.content
39 @api_response = Oj.load(@api_response_s, :symbol_keys => true)
40 errors = @api_response[:errors]
41 if errors.respond_to?(:join)
42 errors = errors.join("\n\n")
46 super(request_url, "#{errors} [API: #{@api_status}]")
50 class AccessForbiddenException < ApiErrorResponseException; end
51 class NotFoundException < ApiErrorResponseException; end
52 class NotLoggedInException < ApiErrorResponseException; end
54 ERROR_CODE_CLASSES = {
55 401 => NotLoggedInException,
56 403 => AccessForbiddenException,
57 404 => NotFoundException,
60 @@profiling_enabled = Rails.configuration.profiling_enabled
63 # An API client object suitable for handling API requests on behalf
64 # of the current thread.
65 def self.new_or_current
66 # If this thread doesn't have an API client yet, *or* this model
67 # has been reloaded since the existing client was created, create
68 # a new client. Otherwise, keep using the latest client created in
70 unless Thread.current[:arvados_api_client].andand.class == self
71 Thread.current[:arvados_api_client] = new
73 Thread.current[:arvados_api_client]
78 @client_mtx = Mutex.new
81 def api(resources_kind, action, data=nil, tokens={})
86 @client_mtx.synchronize do
87 @api_client = HTTPClient.new
88 if Rails.configuration.arvados_insecure_https
89 @api_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
91 # Use system CA certificates
92 ["/etc/ssl/certs/ca-certificates.crt",
93 "/etc/pki/tls/certs/ca-bundle.crt"]
94 .select { |ca_path| File.readable?(ca_path) }
95 .each { |ca_path| @api_client.ssl_config.add_trust_ca(ca_path) }
97 if Rails.configuration.api_response_compression
98 @api_client.transparent_gzip_decompression = true
103 resources_kind = class_kind(resources_kind).pluralize if resources_kind.is_a? Class
104 url = "#{self.arvados_v1_base}/#{resources_kind}#{action}"
106 # Clean up /arvados/v1/../../discovery/v1 to /discovery/v1
107 url.sub! '/arvados/v1/../../', '/'
110 'api_token' => (tokens[:arvados_api_token] ||
111 Thread.current[:arvados_api_token] ||
113 'reader_tokens' => ((tokens[:reader_tokens] ||
114 Thread.current[:reader_tokens] ||
116 [Rails.configuration.anonymous_user_token]).to_json,
120 if v.is_a? String or v.nil?
127 query[k] = Oj.dump(v, mode: :compat)
131 query["_method"] = "GET"
134 if @@profiling_enabled
135 query["_profile"] = "true"
138 header = {"Accept" => "application/json"}
140 profile_checkpoint { "Prepare request #{query["_method"] or "POST"} #{url} #{query[:uuid]} #{query.inspect[0,256]}" }
141 msg = @client_mtx.synchronize do
143 @api_client.post(url, query, header: header)
145 raise NoApiResponseException.new(url, exception)
148 profile_checkpoint 'API transaction'
149 if @@profiling_enabled
150 if msg.headers['X-Runtime']
151 Rails.logger.info "API server: #{msg.headers['X-Runtime']} runtime reported"
153 Rails.logger.info "Content-Encoding #{msg.headers['Content-Encoding'].inspect}, Content-Length #{msg.headers['Content-Length'].inspect}, actual content size #{msg.content.size}"
157 resp = Oj.load(msg.content, :symbol_keys => true)
158 rescue Oj::ParseError
162 if not resp.is_a? Hash
163 raise InvalidApiResponseException.new(url, msg)
164 elsif msg.status_code != 200
165 error_class = ERROR_CODE_CLASSES.fetch(msg.status_code,
166 ApiErrorResponseException)
167 raise error_class.new(url, msg)
171 Rails.logger.info "API client: " \
172 "#{resp.delete(:_profile)[:request_time]} request_time"
174 profile_checkpoint 'Parse response'
178 def self.patch_paging_vars(ary, items_available, offset, limit, links=nil)
180 (class << ary; self; end).class_eval { attr_accessor :items_available }
181 ary.items_available = items_available
184 (class << ary; self; end).class_eval { attr_accessor :offset }
188 (class << ary; self; end).class_eval { attr_accessor :limit }
192 (class << ary; self; end).class_eval { attr_accessor :links }
198 def unpack_api_response(j, kind=nil)
199 if j.is_a? Hash and j[:items].is_a? Array and j[:kind].match(/(_list|List)$/)
200 ary = j[:items].collect { |x| unpack_api_response x, x[:kind] }
201 links = ArvadosResourceList.new Link
202 links.results = (j[:links] || []).collect do |x|
203 unpack_api_response x, x[:kind]
205 self.class.patch_paging_vars(ary, j[:items_available], j[:offset], j[:limit], links)
206 elsif j.is_a? Hash and (kind || j[:kind])
207 oclass = self.kind_class(kind || j[:kind])
210 childkind = j["#{k.to_s}_kind".to_sym]
212 j[k] = self.unpack_api_response(j[k], childkind)
215 oclass.new.private_reload(j)
224 def arvados_login_url(params={})
225 if Rails.configuration.respond_to? :arvados_login_base
226 uri = Rails.configuration.arvados_login_base
228 uri = self.arvados_v1_base.sub(%r{/arvados/v\d+.*}, '/login')
231 uri += '?' << params.collect { |k,v|
232 CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s)
238 def arvados_logout_url(params={})
239 arvados_login_url(params).sub('/login','/logout')
243 Rails.configuration.arvados_v1_base
247 @@discovery ||= api '../../discovery/v1/apis/arvados/v1/rest', ''
251 kind.match(/^arvados\#(.+?)(_list|List)?$/)[1].pluralize.classify.constantize rescue nil
254 def class_kind(resource_class)
255 resource_class.to_s.underscore
258 def self.class_kind(resource_class)
259 resource_class.to_s.underscore
263 def profile_checkpoint label=nil
264 return if !@@profiling_enabled
265 label = yield if block_given?
267 if label and @profile_t0
268 Rails.logger.info "API client: #{t - @profile_t0} #{label}"