2 class NotLoggedInException < StandardError
4 class InvalidApiResponseException < StandardError
6 def api(resources_kind, action, data=nil)
7 arvados_api_token = Thread.current[:arvados_api_token]
8 arvados_api_token = '' if arvados_api_token.nil?
9 dataargs = ['--data-urlencode',
10 "api_token=#{arvados_api_token}",
12 'Accept:application/json']
15 dataargs << '--data-urlencode'
16 if v.is_a? String or v.nil?
17 dataargs << "#{k}=#{v}"
18 elsif v == true or v == false
19 dataargs << "#{k}=#{v ? 1 : 0}"
21 dataargs << "#{k}=#{JSON.dump(v)}"
25 dataargs << '--data-urlencode' << '_method=GET'
28 resources_kind = class_kind(resources_kind).pluralize if resources_kind.is_a? Class
29 url = "#{self.arvados_v1_base}/#{resources_kind}#{action}"
32 "-s#{'k' if Rails.configuration.arvados_insecure_https}",
39 resp = Oj.load(json, :symbol_keys => true)
41 raise InvalidApiResponseException.new json
43 if not resp.is_a? Hash
44 raise InvalidApiResponseException.new json
47 if resp[:errors][0] == 'Not logged in'
48 raise NotLoggedInException.new
50 errors = resp[:errors]
51 errors = errors.join("\n\n") if errors.is_a? Array
52 raise "API errors:\n\n#{errors}\n"
58 def unpack_api_response(j, kind=nil)
59 if j.is_a? Hash and j[:items].is_a? Array and j[:kind].match(/(_list|List)$/)
60 j[:items].collect { |x| unpack_api_response x, j[:kind] }
61 elsif j.is_a? Hash and (kind || j[:kind])
62 oclass = self.kind_class(kind || j[:kind])
65 childkind = j["#{k.to_s}_kind".to_sym]
67 j[k] = self.unpack_api_response(j[k], childkind)
70 oclass.new.private_reload(j)
79 def arvados_login_url(params={})
80 if Rails.configuration.respond_to? :arvados_login_base
81 uri = Rails.configuration.arvados_login_base
83 uri = self.arvados_v1_base.sub(%r{/arvados/v\d+.*}, '/login')
86 uri += '?' << params.collect { |k,v|
87 CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s)
92 def arvados_logout_url(params={})
93 arvados_login_url(params).sub('/login','/logout')
97 Rails.configuration.arvados_v1_base
101 @arvados_schema ||= api 'schema', ''
105 kind.match(/^arvados\#(.+?)(_list|List)?$/)[1].pluralize.classify.constantize rescue nil
108 def class_kind(resource_class)
109 resource_class.to_s.underscore