2891: Workbench converts API HTTP errors to API exceptions.
[arvados.git] / apps / workbench / app / models / arvados_api_client.rb
1 require 'httpclient'
2 require 'thread'
3
4 class ArvadosApiClient
5   class ApiError < StandardError
6     attr_reader :api_response, :api_response_s, :api_status, :request_url
7
8     def initialize(request_url, errmsg)
9       @request_url = request_url
10       @api_response ||= {}
11       super(errmsg)
12     end
13   end
14
15   class NoApiResponseException < ApiError
16     def initialize(request_url, exception)
17       @api_response_s = exception.to_s
18       super(request_url,
19             "#{exception.class.to_s} error connecting to API server")
20     end
21   end
22
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")
28     end
29   end
30
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")
39       else
40         errors = errors.to_s
41       end
42       super(request_url, "#{errors} [API: #{@api_status}]")
43     end
44   end
45
46   class AccessForbiddenException < ApiErrorResponseException; end
47   class NotFoundException < ApiErrorResponseException; end
48   class NotLoggedInException < ApiErrorResponseException; end
49
50   ERROR_CODE_CLASSES = {
51     401 => NotLoggedInException,
52     403 => AccessForbiddenException,
53     404 => NotFoundException,
54   }
55
56   @@profiling_enabled = Rails.configuration.profiling_enabled
57   @@discovery = nil
58
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
65     # the current thread.
66     unless Thread.current[:arvados_api_client].andand.class == self
67       Thread.current[:arvados_api_client] = new
68     end
69     Thread.current[:arvados_api_client]
70   end
71
72   def initialize *args
73     @api_client = nil
74     @client_mtx = Mutex.new
75   end
76
77   def api(resources_kind, action, data=nil)
78     profile_checkpoint
79
80     if not @api_client
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
85         else
86           # Use system CA certificates
87           @api_client.ssl_config.add_trust_ca('/etc/ssl/certs')
88         end
89       end
90     end
91
92     resources_kind = class_kind(resources_kind).pluralize if resources_kind.is_a? Class
93     url = "#{self.arvados_v1_base}/#{resources_kind}#{action}"
94
95     # Clean up /arvados/v1/../../discovery/v1 to /discovery/v1
96     url.sub! '/arvados/v1/../../', '/'
97
98     query = {
99       'api_token' => Thread.current[:arvados_api_token] || '',
100       'reader_tokens' => (Thread.current[:reader_tokens] || []).to_json,
101     }
102     if !data.nil?
103       data.each do |k,v|
104         if v.is_a? String or v.nil?
105           query[k] = v
106         elsif v == true
107           query[k] = 1
108         elsif v == false
109           query[k] = 0
110         else
111           query[k] = JSON.dump(v)
112         end
113       end
114     else
115       query["_method"] = "GET"
116     end
117     if @@profiling_enabled
118       query["_profile"] = "true"
119     end
120
121     header = {"Accept" => "application/json"}
122
123     profile_checkpoint { "Prepare request #{url} #{query[:uuid]} #{query[:where]} #{query[:filters]}" }
124     msg = @client_mtx.synchronize do
125       begin
126         @api_client.post(url, query, header: header)
127       rescue => exception
128         raise NoApiResponseException.new(url, exception)
129       end
130     end
131     profile_checkpoint 'API transaction'
132
133     begin
134       resp = Oj.load(msg.content, :symbol_keys => true)
135     rescue Oj::ParseError
136       resp = nil
137     end
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)
143     end
144
145     if resp[:_profile]
146       Rails.logger.info "API client: " \
147       "#{resp.delete(:_profile)[:request_time]} request_time"
148     end
149     profile_checkpoint 'Parse response'
150     resp
151   end
152
153   def self.patch_paging_vars(ary, items_available, offset, limit, links=nil)
154     if items_available
155       (class << ary; self; end).class_eval { attr_accessor :items_available }
156       ary.items_available = items_available
157     end
158     if offset
159       (class << ary; self; end).class_eval { attr_accessor :offset }
160       ary.offset = offset
161     end
162     if limit
163       (class << ary; self; end).class_eval { attr_accessor :limit }
164       ary.limit = limit
165     end
166     if links
167       (class << ary; self; end).class_eval { attr_accessor :links }
168       ary.links = links
169     end
170     ary
171   end
172
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]
179       end
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])
183       if oclass
184         j.keys.each do |k|
185           childkind = j["#{k.to_s}_kind".to_sym]
186           if childkind
187             j[k] = self.unpack_api_response(j[k], childkind)
188           end
189         end
190         oclass.new.private_reload(j)
191       else
192         j
193       end
194     else
195       j
196     end
197   end
198
199   def arvados_login_url(params={})
200     if Rails.configuration.respond_to? :arvados_login_base
201       uri = Rails.configuration.arvados_login_base
202     else
203       uri = self.arvados_v1_base.sub(%r{/arvados/v\d+.*}, '/login')
204     end
205     if params.size > 0
206       uri += '?' << params.collect { |k,v|
207         CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s)
208       }.join('&')
209     end
210   end
211
212   def arvados_logout_url(params={})
213     arvados_login_url(params).sub('/login','/logout')
214   end
215
216   def arvados_v1_base
217     Rails.configuration.arvados_v1_base
218   end
219
220   def discovery
221     @@discovery ||= api '../../discovery/v1/apis/arvados/v1/rest', ''
222   end
223
224   def kind_class(kind)
225     kind.match(/^arvados\#(.+?)(_list|List)?$/)[1].pluralize.classify.constantize rescue nil
226   end
227
228   def class_kind(resource_class)
229     resource_class.to_s.underscore
230   end
231
232   def self.class_kind(resource_class)
233     resource_class.to_s.underscore
234   end
235
236   protected
237   def profile_checkpoint label=nil
238     return if !@@profiling_enabled
239     label = yield if block_given?
240     t = Time.now
241     if label and @profile_t0
242       Rails.logger.info "API client: #{t - @profile_t0} #{label}"
243     end
244     @profile_t0 = t
245   end
246 end