5 class NotLoggedInException < StandardError
7 class InvalidApiResponseException < StandardError
9 class AccessForbiddenException < StandardError
12 @@profiling_enabled = Rails.configuration.profiling_enabled
15 # An API client object suitable for handling API requests on behalf
16 # of the current thread.
17 def self.new_or_current
18 # If this thread doesn't have an API client yet, *or* this model
19 # has been reloaded since the existing client was created, create
20 # a new client. Otherwise, keep using the latest client created in
22 unless Thread.current[:arvados_api_client].andand.class == self
23 Thread.current[:arvados_api_client] = new
25 Thread.current[:arvados_api_client]
30 @client_mtx = Mutex.new
33 def api(resources_kind, action, data=nil)
37 @client_mtx.synchronize do
38 @api_client = HTTPClient.new
39 if Rails.configuration.arvados_insecure_https
40 @api_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
42 # Use system CA certificates
43 @api_client.ssl_config.add_trust_ca('/etc/ssl/certs')
48 resources_kind = class_kind(resources_kind).pluralize if resources_kind.is_a? Class
49 url = "#{self.arvados_v1_base}/#{resources_kind}#{action}"
51 # Clean up /arvados/v1/../../discovery/v1 to /discovery/v1
52 url.sub! '/arvados/v1/../../', '/'
55 'api_token' => Thread.current[:arvados_api_token] || '',
56 'reader_tokens' => (Thread.current[:reader_tokens] || []).to_json,
60 if v.is_a? String or v.nil?
67 query[k] = JSON.dump(v)
71 query["_method"] = "GET"
73 if @@profiling_enabled
74 query["_profile"] = "true"
77 header = {"Accept" => "application/json"}
79 profile_checkpoint { "Prepare request #{url} #{query[:uuid]} #{query[:where]} #{query[:filters]}" }
80 msg = @client_mtx.synchronize do
85 profile_checkpoint 'API transaction'
87 if msg.status_code == 401
88 raise NotLoggedInException.new
94 resp = Oj.load(json, :symbol_keys => true)
96 raise InvalidApiResponseException.new json
98 if not resp.is_a? Hash
99 raise InvalidApiResponseException.new json
101 if msg.status_code != 200
102 errors = resp[:errors]
103 errors = errors.join("\n\n") if errors.is_a? Array
104 if msg.status_code == 403
105 raise AccessForbiddenException.new "#{errors} [API: #{msg.status_code}]"
107 raise "#{errors} [API: #{msg.status_code}]"
111 Rails.logger.info "API client: " \
112 "#{resp.delete(:_profile)[:request_time]} request_time"
114 profile_checkpoint 'Parse response'
118 def self.patch_paging_vars(ary, items_available, offset, limit, links=nil)
120 (class << ary; self; end).class_eval { attr_accessor :items_available }
121 ary.items_available = items_available
124 (class << ary; self; end).class_eval { attr_accessor :offset }
128 (class << ary; self; end).class_eval { attr_accessor :limit }
132 (class << ary; self; end).class_eval { attr_accessor :links }
138 def unpack_api_response(j, kind=nil)
139 if j.is_a? Hash and j[:items].is_a? Array and j[:kind].match(/(_list|List)$/)
140 ary = j[:items].collect { |x| unpack_api_response x, x[:kind] }
141 links = ArvadosResourceList.new Link
142 links.results = (j[:links] || []).collect do |x|
143 unpack_api_response x, x[:kind]
145 self.class.patch_paging_vars(ary, j[:items_available], j[:offset], j[:limit], links)
146 elsif j.is_a? Hash and (kind || j[:kind])
147 oclass = self.kind_class(kind || j[:kind])
150 childkind = j["#{k.to_s}_kind".to_sym]
152 j[k] = self.unpack_api_response(j[k], childkind)
155 oclass.new.private_reload(j)
164 def arvados_login_url(params={})
165 if Rails.configuration.respond_to? :arvados_login_base
166 uri = Rails.configuration.arvados_login_base
168 uri = self.arvados_v1_base.sub(%r{/arvados/v\d+.*}, '/login')
171 uri += '?' << params.collect { |k,v|
172 CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s)
177 def arvados_logout_url(params={})
178 arvados_login_url(params).sub('/login','/logout')
182 Rails.configuration.arvados_v1_base
186 @@discovery ||= api '../../discovery/v1/apis/arvados/v1/rest', ''
190 kind.match(/^arvados\#(.+?)(_list|List)?$/)[1].pluralize.classify.constantize rescue nil
193 def class_kind(resource_class)
194 resource_class.to_s.underscore
198 def profile_checkpoint label=nil
199 return if !@@profiling_enabled
200 label = yield if block_given?
202 if label and @profile_t0
203 Rails.logger.info "API client: #{t - @profile_t0} #{label}"